Skip to content

Commit

Permalink
fix(results): unescaped lvl0 (#1001)
Browse files Browse the repository at this point in the history
* fix(results): unescaped lvl0

* upgrade bundlesize
  • Loading branch information
shortcuts authored Apr 7, 2021
1 parent 2c0962e commit 117228b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
},
{
"path": "packages/docsearch-react/dist/umd/index.js",
"maxSize": "18.2 kB"
"maxSize": "18.3 kB"
},
{
"path": "packages/docsearch-js/dist/umd/index.js",
"maxSize": "25.8 kB"
"maxSize": "25.9 kB"
}
]
}
6 changes: 4 additions & 2 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { useSearchClient } from './useSearchClient';
import { useTouchEvents } from './useTouchEvents';
import { useTrapFocus } from './useTrapFocus';
import { groupBy, identity, noop } from './utils';
import { groupBy, identity, noop, removeHighlightTags } from './utils';

export interface DocSearchModalProps extends DocSearchProps {
initialScrollY: number;
Expand Down Expand Up @@ -224,7 +224,9 @@ export function DocSearchModal({
.then((results) => {
const hits = results[0].hits;
const nbHits: number = results[0].nbHits;
const sources = groupBy(hits, (hit) => hit.hierarchy.lvl0);
const sources = groupBy(hits, (hit) =>
removeHighlightTags(hit.hierarchy.lvl0)
);

// We store the `lvl0`s to display them as search suggestions
// in the “no results“ screen.
Expand Down
5 changes: 4 additions & 1 deletion packages/docsearch-react/src/ResultsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SelectIcon, SourceIcon } from './icons';
import { Results } from './Results';
import { ScreenStateProps } from './ScreenState';
import { InternalDocSearchHit } from './types';
import { removeHighlightTags } from './utils';

type ResultsScreenProps = ScreenStateProps<InternalDocSearchHit>;

Expand All @@ -15,7 +16,9 @@ export function ResultsScreen(props: ResultsScreenProps) {
return null;
}

const title = collection.items[0].hierarchy.lvl0;
const title = removeHighlightTags(
collection.items[0]._highlightResult.hierarchy.lvl0.value
);

return (
<Results
Expand Down
1 change: 1 addition & 0 deletions packages/docsearch-react/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './groupBy';
export * from './identity';
export * from './noop';
export * from './removeHighlightTags';
8 changes: 8 additions & 0 deletions packages/docsearch-react/src/utils/removeHighlightTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const regexHighlightTags = /(<mark>|<\/mark>)/g;
const regexHasHighlightTags = RegExp(regexHighlightTags.source);

export function removeHighlightTags(value: string): string {
return value && regexHasHighlightTags.test(value)
? value.replace(regexHighlightTags, '')
: value;
}

0 comments on commit 117228b

Please sign in to comment.