Skip to content

Commit 8d4d2b1

Browse files
authored
resolves #45 preserve original order of hits (PR #103)
- qualify unique occurrences of the same lvl0 and lvl1 values
1 parent 9dc3e86 commit 8d4d2b1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/js/vendor/docsearch.bundle.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
inputSelector: searchFieldSelector + ' .query',
2525
autocompleteOptions: { autoselect: false, debug: true, hint: false, keyboardShortcuts: [], minLength: 2 },
2626
algoliaOptions: algoliaOptions,
27+
transformData: protectHitOrder,
2728
queryHook:
2829
filterInput &&
2930
function (query) {
@@ -117,4 +118,28 @@
117118
this.close()
118119
this.setVal()
119120
}
121+
122+
// preserves the original order of results by qualifying unique occurrences of the same lvl0 and lvl1 values
123+
function protectHitOrder (hits) {
124+
var prevLvl0
125+
var lvl0Qualifiers = {}
126+
var lvl1Qualifiers = {}
127+
return hits.map(function (hit) {
128+
var lvl0 = hit.hierarchy.lvl0
129+
var lvl0Qualifier = lvl0Qualifiers[lvl0]
130+
if (lvl0 !== prevLvl0) {
131+
lvl0Qualifiers[lvl0] = lvl0Qualifier == null ? (lvl0Qualifier = '') : (lvl0Qualifier += ' ')
132+
lvl1Qualifiers = {}
133+
}
134+
if (lvl0Qualifier) hit.hierarchy.lvl0 = lvl0 + lvl0Qualifier
135+
var lvl1 = hit.hierarchy.lvl1
136+
if (lvl1 in lvl1Qualifiers) {
137+
hit.hierarchy.lvl1 = lvl1 + (lvl1Qualifiers[lvl1] += ' ')
138+
} else {
139+
lvl1Qualifiers[lvl1] = ''
140+
}
141+
prevLvl0 = lvl0
142+
return hit
143+
})
144+
}
120145
})()

0 commit comments

Comments
 (0)