From 5260d1e5c280468bb08a20918c7858e695d11cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 27 Sep 2024 16:11:06 +0200 Subject: [PATCH] fix(hits): default to hierarchy lvl0 if highlights present (#2309) --- examples/demo/src/App.js | 6 +++--- .../src/utils/removeHighlightTags.ts | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/demo/src/App.js b/examples/demo/src/App.js index 1800ed701..0c94bd7cc 100644 --- a/examples/demo/src/App.js +++ b/examples/demo/src/App.js @@ -9,9 +9,9 @@ function App() {

DocSearch v3 - React

diff --git a/packages/docsearch-react/src/utils/removeHighlightTags.ts b/packages/docsearch-react/src/utils/removeHighlightTags.ts index 5ba739286..e2c553afd 100644 --- a/packages/docsearch-react/src/utils/removeHighlightTags.ts +++ b/packages/docsearch-react/src/utils/removeHighlightTags.ts @@ -12,13 +12,15 @@ export function removeHighlightTags( return hit.hierarchy.lvl0; } - const { value } = - (internalDocSearchHit.__docsearch_parent - ? internalDocSearchHit.__docsearch_parent?._highlightResult?.hierarchy - ?.lvl0 - : hit._highlightResult?.hierarchy?.lvl0) || {}; + const lvl0 = internalDocSearchHit.__docsearch_parent + ? internalDocSearchHit.__docsearch_parent?._highlightResult?.hierarchy?.lvl0 + : hit._highlightResult?.hierarchy?.lvl0; - return value && regexHasHighlightTags.test(value) - ? value.replace(regexHighlightTags, '') - : value; + if (!lvl0) { + return hit.hierarchy.lvl0; + } + + return lvl0.value && regexHasHighlightTags.test(lvl0.value) + ? lvl0.value.replace(regexHighlightTags, '') + : lvl0.value; }