diff --git a/packages/docsearch-react/src/DocSearchModal.tsx b/packages/docsearch-react/src/DocSearchModal.tsx index 7352b23d5..300f7569c 100644 --- a/packages/docsearch-react/src/DocSearchModal.tsx +++ b/packages/docsearch-react/src/DocSearchModal.tsx @@ -27,6 +27,10 @@ interface DocSearchModalProps extends DocSearchProps { onClose?(): void; } +function defaultTransformItems(x: DocSearchHit[]) { + return x; +} + export function DocSearchModal({ appId = 'BH4D9OD16A', apiKey, @@ -34,7 +38,7 @@ export function DocSearchModal({ placeholder = 'Search docs', searchParameters, onClose = noop, - transformItems = (x) => x, + transformItems = defaultTransformItems, hitComponent = Hit, resultsFooterComponent = () => null, navigator, diff --git a/packages/website/src/theme/SearchBar/index.js b/packages/website/src/theme/SearchBar/index.js index f0e0a3943..0f394c87b 100644 --- a/packages/website/src/theme/SearchBar/index.js +++ b/packages/website/src/theme/SearchBar/index.js @@ -22,6 +22,21 @@ function ResultsFooter({ state }) { ); } +function transformItems(items) { + return items.map((item) => { + // We transform the absolute URL into a relative URL. + // Alternatively, we can use `new URL(item.url)` but it's not + // supported in IE. + const a = document.createElement('a'); + a.href = item.url; + + return { + ...item, + url: `${a.pathname}${a.hash}`, + }; + }); +} + function DocSearch({ indexName, appId, apiKey, searchParameters }) { const history = useHistory(); const [isOpen, setIsOpen] = useState(false); @@ -85,20 +100,7 @@ function DocSearch({ indexName, appId, apiKey, searchParameters }) { history.push(suggestionUrl); }, }} - transformItems={(items) => { - return items.map((item) => { - // We transform the absolute URL into a relative URL. - // Alternatively, we can use `new URL(item.url)` but it's not - // supported in IE. - const a = document.createElement('a'); - a.href = item.url; - - return { - ...item, - url: `${a.pathname}${a.hash}`, - }; - }); - }} + transformItems={transformItems} hitComponent={Hit} resultsFooterComponent={ResultsFooter} />,