diff --git a/packages/docsearch-react/src/DocSearchModal.tsx b/packages/docsearch-react/src/DocSearchModal.tsx index 89c27b269..f8878edad 100644 --- a/packages/docsearch-react/src/DocSearchModal.tsx +++ b/packages/docsearch-react/src/DocSearchModal.tsx @@ -224,9 +224,7 @@ export function DocSearchModal({ .then((results) => { const hits = results[0].hits; const nbHits: number = results[0].nbHits; - const sources = groupBy(hits, (hit) => - removeHighlightTags(hit.hierarchy.lvl0) - ); + const sources = groupBy(hits, (hit) => removeHighlightTags(hit)); // We store the `lvl0`s to display them as search suggestions // in the “no results“ screen. diff --git a/packages/docsearch-react/src/ResultsScreen.tsx b/packages/docsearch-react/src/ResultsScreen.tsx index 2713ec0b1..bcd7528f0 100644 --- a/packages/docsearch-react/src/ResultsScreen.tsx +++ b/packages/docsearch-react/src/ResultsScreen.tsx @@ -16,9 +16,7 @@ export function ResultsScreen(props: ResultsScreenProps) { return null; } - const title = removeHighlightTags( - collection.items[0]._highlightResult.hierarchy.lvl0.value - ); + const title = removeHighlightTags(collection.items[0]); return ( |<\/mark>)/g; const regexHasHighlightTags = RegExp(regexHighlightTags.source); -export function removeHighlightTags(value: string): string { +export function removeHighlightTags( + hit: DocSearchHit | InternalDocSearchHit +): string { + if ( + !(hit as InternalDocSearchHit).__docsearch_parent && + !hit._highlightResult + ) { + return hit.hierarchy.lvl0; + } + + const { value } = hit._highlightResult + ? hit._highlightResult.hierarchy.lvl0 + : (hit as InternalDocSearchHit).__docsearch_parent!._highlightResult + .hierarchy.lvl0; + return value && regexHasHighlightTags.test(value) ? value.replace(regexHighlightTags, '') : value;