From 2ddd2d1f392829ad5fd9817b364daeafde6e50d7 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 6 Dec 2023 21:46:13 +0100 Subject: [PATCH] fix: search links with basehref --- website/src/components/Header/Search.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/website/src/components/Header/Search.tsx b/website/src/components/Header/Search.tsx index d3a935f5..c982d80b 100644 --- a/website/src/components/Header/Search.tsx +++ b/website/src/components/Header/Search.tsx @@ -6,6 +6,8 @@ import './Search.css'; import { createPortal } from 'react-dom'; import * as docSearchReact from '@docsearch/react'; +const { BASE_URL } = import.meta.env; +const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL; /** FIXME: This is still kinda nasty, but DocSearch is not ESM ready. */ const DocSearchModal = docSearchReact.DocSearchModal || (docSearchReact as any).default.DocSearchModal; @@ -89,9 +91,13 @@ export default function Search() { const a = document.createElement('a'); a.href = item.url; const hash = a.hash === '#overview' ? '' : a.hash; + let pathname = a.pathname; + pathname = pathname.startsWith('/') ? pathname.slice(1) : pathname; + pathname = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname; + const url = `${baseNoTrailing}/${pathname}/${hash}`; return { ...item, - url: `${a.pathname}${hash}`, + url, }; }); }}