Skip to content

Commit

Permalink
fix: algolia search sorting (#2328)
Browse files Browse the repository at this point in the history
# Description

Algolia DocSearc, prioritize all parents pages over contents and sort
them according to their parents if exist


![image](https://github.com/zesty-io/website/assets/61284357/4733fee6-9bc0-4622-a5e2-7585ed49861c)


![image](https://github.com/zesty-io/website/assets/61284357/3cbf07e6-9dcc-41a9-bf1c-b4fe3e2663bb)


Closes #2207

---------

Co-authored-by: Darwin ❤️❤️❤️ <[email protected]>
  • Loading branch information
jomarmontuya and darwin808 committed Jan 24, 2024
1 parent e05c4af commit e4519ae
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/pages/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,52 @@ export function DocSearchModal() {

return (
<DocSearch
transformItems={(item) => {
return item.reverse();
transformItems={(items) => {
/*
* Group items by hierarchy and remove items without hierarchy
*/
const groupBy = items.reduce((acc, item) => {
if (!item.hierarchy) {
return acc;
}
const list = acc[item.hierarchy.lvl1] || [];

return {
...acc,
[item.hierarchy.lvl1]: list.concat(item),
};
}, {});

/**
* Group based on parent page
*/
const groups = Object.keys(groupBy).map((level) => ({
items: groupBy[level],
}));

const groupItems = groups?.[0]?.items || [];

/**
* Find the parent page from groupItems
*/
const parent = groupItems?.find((item) => {
if (
item.hierarchy.lvl1 !== null &&
item.hierarchy.lvl2 == null &&
item.hierarchy.lvl3 == null &&
item.hierarchy.lvl4 == null &&
item.hierarchy.lvl5 == null &&
item.hierarchy.lvl6 == null
) {
return item;
}
});

/**
* find the index of the parent from groupItems and put it in the first index
*/
const parentIndex = groupItems?.indexOf(parent);
return groupItems.splice(parentIndex, 1).concat(groupItems);
}}
placeholder="Search docs..."
maxResultsPerGroup={100}
Expand Down

0 comments on commit e4519ae

Please sign in to comment.