Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
fix(website): update DocSearch integration
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed May 20, 2020
1 parent 129c1d1 commit d41605d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { DocSearchButton, useDocSearchKeyboardEvents } from '@docsearch/react';

let DocSearchModal = null;

function Hit({ hit, children }) {
return <Link to={hit.url}>{children}</Link>;
}

function SearchBar() {
const { siteConfig = {} } = useDocusaurusContext();
const history = useHistory();
Expand Down Expand Up @@ -59,6 +63,9 @@ function SearchBar() {
return (
<>
<Head>
{/* This hints the browser that the website will load data from Algolia,
and allows it to preconnect to the DocSearch cluster. It makes the first
query faster, especially on mobile. */}
<link
rel="preconnect"
href={`https://${appId}-dsn.algolia.net`}
Expand All @@ -68,6 +75,7 @@ function SearchBar() {

<DocSearchButton
onTouchStart={importDocSearchModalIfNeeded}
onFocus={importDocSearchModalIfNeeded}
onMouseOver={importDocSearchModalIfNeeded}
onClick={onOpen}
/>
Expand All @@ -87,13 +95,15 @@ function SearchBar() {
}}
transformItems={(items) => {
return items.map((item) => {
const url = new URL(item.url);
// 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: item.url
.replace(url.origin, '')
.replace('#__docusaurus', ''),
url: `${a.pathname}${a.hash}`,
};
});
}}
Expand All @@ -105,8 +115,4 @@ function SearchBar() {
);
}

function Hit({ hit, children }) {
return <Link to={hit.url}>{children}</Link>;
}

export default SearchBar;

0 comments on commit d41605d

Please sign in to comment.