From e4519ae935dcd2845f17bedb0c199b55521e3284 Mon Sep 17 00:00:00 2001 From: Jomar Montuya <61284357+jomarmontuya@users.noreply.github.com> Date: Wed, 24 Jan 2024 08:51:00 +0800 Subject: [PATCH] fix: algolia search sorting (#2328) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 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 ❤️❤️❤️ <71545960+darwin808@users.noreply.github.com> --- src/pages/docs/index.js | 48 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/pages/docs/index.js b/src/pages/docs/index.js index 93a71303b..d9616082a 100644 --- a/src/pages/docs/index.js +++ b/src/pages/docs/index.js @@ -207,8 +207,52 @@ export function DocSearchModal() { return ( { - 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}