Skip to content

Commit

Permalink
fix(escapedResults): _highlightResult is undefined (#1003)
Browse files Browse the repository at this point in the history
fix(escapedResults): _highlightResult is undefined
  • Loading branch information
shortcuts committed Apr 13, 2021
1 parent f8f3edd commit 16e6558
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 1 addition & 3 deletions packages/docsearch-react/src/DocSearchModal.tsx
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions packages/docsearch-react/src/ResultsScreen.tsx
Expand Up @@ -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 (
<Results
Expand Down
18 changes: 17 additions & 1 deletion packages/docsearch-react/src/utils/removeHighlightTags.ts
@@ -1,7 +1,23 @@
import { DocSearchHit, InternalDocSearchHit } from './../types';

const regexHighlightTags = /(<mark>|<\/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;
Expand Down

0 comments on commit 16e6558

Please sign in to comment.