Skip to content

Commit 4c7bc9f

Browse files
committed
feat(algolia): refactor heading resolver
* take levels 0 and 1 * add the least significant level of the rest, if possible and not similar to 1 or 0 closes #219
1 parent aca41c8 commit 4c7bc9f

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/functions/autocomplete/algoliaAutoComplete.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ function headingIsSimilar(one: string, other: string) {
2828
export function resolveHitToNamestring(hit: AlgoliaHit) {
2929
const { hierarchy } = hit;
3030

31-
const [lvl0, lvl1, lvl2, lvl3] = [hierarchy.lvl0, hierarchy.lvl1, hierarchy.lvl2, hierarchy.lvl3].map((heading) =>
32-
removeDtypesPrefix(heading),
33-
);
31+
const [lvl0, lvl1, ...restLevels] = Object.values(hierarchy).map((heading) => removeDtypesPrefix(heading));
3432

35-
let value = headingIsSimilar(lvl0, lvl1) ? lvl1 : `${lvl0}${lvl1 ? `: ${lvl1}` : ''}`;
33+
const headingParts = [];
3634

37-
if (lvl2.length) {
38-
value += ` - ${lvl2}`;
35+
if (headingIsSimilar(lvl0, lvl1)) {
36+
headingParts.push(lvl1);
37+
} else {
38+
headingParts.push(`${lvl0}:`, lvl1);
3939
}
4040

41-
if (lvl3.length) {
42-
value += ` > ${lvl3}`;
41+
const mostSpecific = restLevels.filter(Boolean).at(-1);
42+
if (mostSpecific?.length && !headingIsSimilar(lvl0, mostSpecific) && !headingIsSimilar(lvl1, mostSpecific)) {
43+
headingParts.push(`- ${mostSpecific}`);
4344
}
4445

45-
return decode(value)!;
46+
return decode(headingParts.join(' '))!;
4647
}
4748

4849
function autoCompleteMap(elements: AlgoliaHit[]) {

0 commit comments

Comments
 (0)