Merge pull request #60 from algolia/fix/53
fix(url): Removes concatenation of URL with hash if it contains a hash
This commit is contained in:
commit
0f20b7c0e6
2 changed files with 80 additions and 1 deletions
|
|
@ -132,7 +132,7 @@ class DocSearch {
|
|||
|
||||
// Translate hits into smaller objects to be send to the template
|
||||
return groupedHits.map((hit) => {
|
||||
let url = hit.anchor ? `${hit.url}#${hit.anchor}` : hit.url;
|
||||
let url = DocSearch.formatURL(hit);
|
||||
let category = utils.getHighlightedValue(hit, 'lvl0');
|
||||
let subcategory = utils.getHighlightedValue(hit, 'lvl1') || category;
|
||||
let displayTitle = utils.compact([
|
||||
|
|
@ -156,6 +156,21 @@ class DocSearch {
|
|||
});
|
||||
}
|
||||
|
||||
static formatURL(hit) {
|
||||
const {url, anchor} = hit;
|
||||
if (url) {
|
||||
const containsAnchor = url.indexOf('#') !== -1;
|
||||
if (containsAnchor) return url;
|
||||
else if (anchor) return `${hit.url}#${hit.anchor}`;
|
||||
return url;
|
||||
}
|
||||
else if (anchor) return `#${hit.anchor}`;
|
||||
/* eslint-disable */
|
||||
console.warn('no anchor nor url for : ', JSON.stringify(hit));
|
||||
/* eslint-enable */
|
||||
return null;
|
||||
}
|
||||
|
||||
static getSuggestionTemplate() {
|
||||
const template = Hogan.compile(templates.suggestion);
|
||||
return (suggestion) => {
|
||||
|
|
|
|||
|
|
@ -734,6 +734,70 @@ describe('DocSearch', () => {
|
|||
// Then
|
||||
expect(actual[0].url).toEqual('http://foo.bar/#anchor');
|
||||
});
|
||||
it('should not add the anchor to the url if one is set but it is already in the URL', () => {
|
||||
// Given
|
||||
let input = [{
|
||||
hierarchy: {
|
||||
lvl0: 'Ruby',
|
||||
lvl1: 'API',
|
||||
lvl2: null,
|
||||
lvl3: null,
|
||||
lvl4: null,
|
||||
lvl5: null
|
||||
},
|
||||
content: 'foo bar',
|
||||
url: 'http://foo.bar/#anchor',
|
||||
anchor: 'anchor'
|
||||
}];
|
||||
|
||||
// When
|
||||
let actual = DocSearch.formatHits(input);
|
||||
|
||||
// Then
|
||||
expect(actual[0].url).toEqual('http://foo.bar/#anchor');
|
||||
});
|
||||
it('should just use the URL if no anchor is provided', () => {
|
||||
// Given
|
||||
let input = [{
|
||||
hierarchy: {
|
||||
lvl0: 'Ruby',
|
||||
lvl1: 'API',
|
||||
lvl2: null,
|
||||
lvl3: null,
|
||||
lvl4: null,
|
||||
lvl5: null
|
||||
},
|
||||
content: 'foo bar',
|
||||
url: 'http://foo.bar/'
|
||||
}];
|
||||
|
||||
// When
|
||||
let actual = DocSearch.formatHits(input);
|
||||
|
||||
// Then
|
||||
expect(actual[0].url).toEqual(input[0].url);
|
||||
});
|
||||
it('should return the anchor if there is no URL', () => {
|
||||
// Given
|
||||
let input = [{
|
||||
hierarchy: {
|
||||
lvl0: 'Ruby',
|
||||
lvl1: 'API',
|
||||
lvl2: null,
|
||||
lvl3: null,
|
||||
lvl4: null,
|
||||
lvl5: null
|
||||
},
|
||||
content: 'foo bar',
|
||||
anchor: 'anchor'
|
||||
}];
|
||||
|
||||
// When
|
||||
let actual = DocSearch.formatHits(input);
|
||||
|
||||
// Then
|
||||
expect(actual[0].url).toEqual('#' + input[0].anchor);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSuggestionTemplate', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue