diff --git a/packages/share-theme/package.json b/packages/share-theme/package.json index 13dac7f02..9a6d36128 100644 --- a/packages/share-theme/package.json +++ b/packages/share-theme/package.json @@ -2,6 +2,7 @@ "name": "@triliumnext/share-theme", "version": "1.0.3", "description": "Adaptation of the trilium.rocks share theme", + "type": "module", "main": "index.js", "scripts": { "build": "esrun scripts/build.ts", diff --git a/packages/share-theme/src/scripts/modules/search.ts b/packages/share-theme/src/scripts/modules/search.ts index 67bf0c22b..a396cfaed 100644 --- a/packages/share-theme/src/scripts/modules/search.ts +++ b/packages/share-theme/src/scripts/modules/search.ts @@ -1,6 +1,9 @@ import debounce from "../common/debounce"; import parents from "../common/parents"; import parseHTML from "../common/parsehtml"; +import type { default as Fuse } from "fuse.js"; + +let fuseInstance: Fuse | null = null; interface SearchResults { results: SearchResult[]; @@ -61,25 +64,30 @@ export default function setupSearch() { } async function fetchResults(query: string): Promise { - if ((window as any).glob.isStatic) { - const linkHref = document.head.querySelector("link[rel=stylesheet]")?.getAttribute("href"); - const rootUrl = linkHref?.split("/").slice(0, -2).join("/") || "."; - const searchIndex = await (await fetch(`${rootUrl}/search-index.json`)).json(); - const Fuse = (await import("fuse.js")).default; - const fuse = new Fuse(searchIndex, { - keys: [ - "title", - "content" - ], - includeScore: true, - threshold: 0.65, - ignoreDiacritics: true, - ignoreLocation: true, - ignoreFieldNorm: true, - useExtendedSearch: true - }); + const linkHref = document.head.querySelector("link[rel=stylesheet]")?.getAttribute("href"); + const rootUrl = linkHref?.split("/").slice(0, -2).join("/") || "."; - const results = fuse.search(query, { limit: 5 }); + if ((window as any).glob.isStatic) { + // Load the search index. + if (!fuseInstance) { + const searchIndex = await (await fetch(`${rootUrl}/search-index.json`)).json(); + const Fuse = (await import("fuse.js")).default; + fuseInstance = new Fuse(searchIndex, { + keys: [ + "title", + "content" + ], + includeScore: true, + threshold: 0.65, + ignoreDiacritics: true, + ignoreLocation: true, + ignoreFieldNorm: true, + useExtendedSearch: true + }); + } + + // Do the search. + const results = fuseInstance.search(query, { limit: 5 }); console.debug("Search results:", results); const processedResults = results.map(({ item, score }) => ({ ...item,