diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js
index 36e3ab9f..46e8aa2d 100644
--- a/src/lib/DocSearch.js
+++ b/src/lib/DocSearch.js
@@ -98,14 +98,14 @@ 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 category = this.getHighlightedValue(hit, 'lvl0');
- let subcategory = this.getHighlightedValue(hit, 'lvl1') || category;
+ let category = utils.getHighlightedValue(hit, 'lvl0');
+ let subcategory = utils.getHighlightedValue(hit, 'lvl1') || category;
let displayTitle = utils.compact([
- this.getHighlightedValue(hit, 'lvl2') || subcategory,
- this.getHighlightedValue(hit, 'lvl3'),
- this.getHighlightedValue(hit, 'lvl4'),
- this.getHighlightedValue(hit, 'lvl5'),
- this.getHighlightedValue(hit, 'lvl6')
+ utils.getHighlightedValue(hit, 'lvl2') || subcategory,
+ utils.getHighlightedValue(hit, 'lvl3'),
+ utils.getHighlightedValue(hit, 'lvl4'),
+ utils.getHighlightedValue(hit, 'lvl5'),
+ utils.getHighlightedValue(hit, 'lvl6')
]).join(' › ');
let text = this.getSnippettedValue(hit, 'content');
@@ -121,11 +121,6 @@ class DocSearch {
});
}
- getHighlightedValue(object, key) {
- let highlight = object._highlightResult[key];
- return highlight ? highlight.value : object[key];
- }
-
getSnippettedValue(object, key) {
if (!object._snippetResult || !object._snippetResult[key].value) {
return object[key];
diff --git a/src/lib/utils.js b/src/lib/utils.js
index fe18ddce..d6b5aec3 100644
--- a/src/lib/utils.js
+++ b/src/lib/utils.js
@@ -169,6 +169,33 @@ let utils = {
});
return results;
},
+ /*
+ * Returns the highlighted value of the specified key in the specified object.
+ * If no highlighted value is available, will return the key value directly
+ * eg.
+ * getHighlightedValue({
+ * _highlightResult: {
+ * text: {
+ * value: 'foo'
+ * }
+ * },
+ * text: 'foo'
+ * }, 'foo');
+ * =>
+ * 'foo'
+ * @param {object} object Hit object returned by the Algolia API
+ * @param {string} property Object key to look for
+ * @return {string}
+ **/
+ getHighlightedValue(object, property) {
+ if (!object._highlightResult
+ || !object._highlightResult[property]
+ || !object._highlightResult[property].value) {
+ return object[property];
+ }
+ return object._highlightResult[property].value;
+ }
+
};
diff --git a/test/utils-test.js b/test/utils-test.js
index d9acc57a..91366d0a 100644
--- a/test/utils-test.js
+++ b/test/utils-test.js
@@ -3,6 +3,11 @@
import jsdom from 'mocha-jsdom';
import expect from 'expect';
+let ddescribe = describe.only;
+let xdescribe = describe.skip;
+let iit = it.only;
+let xit = it.skip;
+
describe('utils', () => {
var utils;
jsdom();
@@ -214,4 +219,51 @@ describe('utils', () => {
expect(actual).toEqual([42, [], 'foo']);
});
});
+
+ describe('getHighlightedValue', () => {
+ it('should return the highlighted version if exists', () => {
+ // Given
+ let input = {
+ _highlightResult: {
+ text: {
+ value: 'foo'
+ }
+ },
+ text: 'foo'
+ };
+
+ // When
+ let actual = utils.getHighlightedValue(input, 'text');
+
+ // Then
+ expect(actual).toEqual('foo');
+ });
+ it('should return the default key if no highlighted value', () => {
+ // Given
+ let input = {
+ _highlightResult: {
+ text: { }
+ },
+ text: 'foo'
+ };
+
+ // When
+ let actual = utils.getHighlightedValue(input, 'text');
+
+ // Then
+ expect(actual).toEqual('foo');
+ });
+ it('should return the default key if no highlight results', () => {
+ // Given
+ let input = {
+ text: 'foo'
+ };
+
+ // When
+ let actual = utils.getHighlightedValue(input, 'text');
+
+ // Then
+ expect(actual).toEqual('foo');
+ });
+ });
});