Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(escapedResults): _highlightResult is undefined #1003

Merged
merged 1 commit into from Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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