From 0b7ad009e02bcb98f30447d884c90f80fca2a944 Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Tue, 20 Jun 2023 09:35:06 +0330 Subject: [PATCH] fix: fix indentation of search results, refs #58 --- .../SearchResultGroup.module.scss | 4 -- .../SearchResultItem.module.scss | 11 ++-- .../SearchResultItem/SearchResultItem.tsx | 10 ++-- .../SearchBar/SearchResults/SearchResults.tsx | 1 + .../src/client/theme/SearchBar/types.ts | 1 + .../SearchBar/utils/groupSearchResult.ts | 52 +++++++++++++------ 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultGroup/SearchResultGroup.module.scss b/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultGroup/SearchResultGroup.module.scss index 6663287..654a3e3 100644 --- a/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultGroup/SearchResultGroup.module.scss +++ b/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultGroup/SearchResultGroup.module.scss @@ -17,8 +17,4 @@ list-style: none; margin-top: 1rem; } - - ul a[href*='#'] li { - padding-left: 56px; - } } diff --git a/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.module.scss b/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.module.scss index ee93faa..522b0c0 100644 --- a/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.module.scss +++ b/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.module.scss @@ -2,16 +2,15 @@ text-decoration: none !important; } +.root.level1 { + opacity: 0.1; + padding-left: 32px; +} + .icon { width: 16px; height: auto; - &.l1 { - } - - &.l2 { - } - &.fill { &, & * { diff --git a/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.tsx b/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.tsx index 662b659..ce9f69b 100644 --- a/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.tsx +++ b/packages/logos-docusaurus-theme/src/client/theme/SearchBar/SearchResultItem/SearchResultItem.tsx @@ -9,13 +9,13 @@ import styles from './SearchResultItem.module.scss' const icons: Record = { [SearchDocumentType.Title]: ( - + ), [SearchDocumentType.Heading]: ( - + ), [SearchDocumentType.Paragraph]: ( - + ), } @@ -24,11 +24,13 @@ export type SearchResultItemProps = React.HTMLProps & { title: string href: string content?: string + level?: number linkProps?: LinkProps } export const SearchResultItem: React.FC = ({ type, + level = 0, href, title, content, @@ -41,7 +43,7 @@ export const SearchResultItem: React.FC = ({ return ( = ({ - Object.entries( +): GroupedSearchResult => { + const grouped = Object.entries( groupBy( results.map((item) => convertSearchResult(item)), 'category', @@ -84,21 +87,38 @@ export const groupSearchResult = ( .map(([category, items]) => [ category, Object.entries(groupBy(items, 'url')) - .map( - ([url, items]) => - [ - url, - items.sort((a, b) => - a.type === SearchDocumentType.Title - ? -1 - : a.score > b.score - ? -1 - : 1, - ), - ] as [string, SearchResultGroupItem[]], - ) + .map(([url, value]) => { + let items: SearchResultGroupItem[] = [...value].sort((a, b) => + a.type === SearchDocumentType.Title + ? -1 + : a.score > b.score + ? -1 + : 1, + ) + + const hasTitle = items[0]?.type === SearchDocumentType.Title + + items = hasTitle + ? items.filter( + (item) => + !( + item.type === SearchDocumentType.Heading && item.hash === '' + ), + ) + : items + + items = items.map((item, index) => ({ + ...item, + level: hasTitle ? (index === 0 ? 0 : 1) : 0, + })) + + return [url, items] as [string, SearchResultGroupItem[]] + }) .sort((a, b) => (a[1][0]?.score ?? 0) > (b[1][0]?.score ?? 0) ? -1 : 1, ) .flatMap(([url, items]) => items), ]) + + return grouped as GroupedSearchResult +}