Skip to content

Commit

Permalink
feat: Improve search function (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Oct 8, 2024
1 parent cd73cee commit 3a4e5b2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/components/common/Header/searchComp/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ const docsearchTranslations: DocSearchTranslationProps = {

<script>
import { ALGOLIA } from "@/constant";

const getRootUrl = (url: string) => {
if (!url) {
return url;
}
const schemeIndex = url.indexOf("://");
const firstSlashIndex = url.indexOf(
"/",
schemeIndex === -1 ? 0 : schemeIndex + "://".length,
);
return firstSlashIndex === -1 ? url : url.substring(0, firstSlashIndex);
};

let url = "";
class StarlightDocSearch extends HTMLElement {
constructor() {
Expand All @@ -132,6 +145,37 @@ const docsearchTranslations: DocSearchTranslationProps = {
const options: Parameters<typeof docsearch>[0] = {
...ALGOLIA,
container: "sl-doc-search",
transformItems(items) {
if (!items || !items.length) {
return items;
}
const filteredItems = [];
const pageRootUrl = getRootUrl(url);
const isPageEnglish = url
.substring(pageRootUrl.length)
.startsWith("/en");
for (const item of items) {
const itemUrl = item.url;
if (!itemUrl) {
continue;
}
const itemRootUrl = getRootUrl(itemUrl);
const itemPath = itemUrl.substring(itemRootUrl.length);
if (itemPath.startsWith("/en") !== isPageEnglish) {
continue;
}
if (itemRootUrl === pageRootUrl) {
filteredItems.push(item);
} else {
filteredItems.push(
Object.assign({}, item, {
url: pageRootUrl + itemPath,
}),
);
}
}
return filteredItems;
},
};
try {
const translations = JSON.parse(
Expand Down

0 comments on commit 3a4e5b2

Please sign in to comment.