From 0d7ade180bebe3237f6d8f06fb952e48c4ddcf86 Mon Sep 17 00:00:00 2001 From: Amir Pourmand Date: Thu, 28 Aug 2025 11:26:09 +0330 Subject: [PATCH] refactor(search): enhance search encoder and update search results type - Improved the encoder function to filter out empty tokens. - Updated the search results type from a specific FlexSearch type to a more generic 'any' type for flexibility. - Removed redundant rtl property from the index configuration. --- quartz/components/scripts/search.inline.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts index 137cb447c..008e911a9 100644 --- a/quartz/components/scripts/search.inline.ts +++ b/quartz/components/scripts/search.inline.ts @@ -9,20 +9,27 @@ interface Item { title: string content: string tags: string[] + [key: string]: any } // Can be expanded with things like "term" in the future type SearchType = "basic" | "tags" let searchType: SearchType = "basic" let currentSearchTerm: string = "" -const encoder = (str: string) => str.toLowerCase().split(/\s+/) +// Encoder text +const encoder = (str: string) => { + return str + .toLowerCase() + .split(/\s+/) + .filter(token => token.length > 0) +} + let index = new FlexSearch.Document({ - charset: "Default", encode: encoder, + rtl: true, document: { id: "id", tag: "tags", - rtl: true, index: [ { field: "title", @@ -398,7 +405,7 @@ async function setupSearch(searchElement: Element, currentSlug: FullSlug, data: searchLayout.classList.toggle("display-results", currentSearchTerm !== "") searchType = currentSearchTerm.startsWith("#") ? "tags" : "basic" - let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[] + let searchResults: any[] if (searchType === "tags") { currentSearchTerm = currentSearchTerm.substring(1).trim() const separatorIndex = currentSearchTerm.indexOf(" ") @@ -411,7 +418,7 @@ async function setupSearch(searchElement: Element, currentSlug: FullSlug, data: // return at least 10000 documents, so it is enough to filter them by tag (implemented in flexsearch) limit: Math.max(numSearchResults, 10000), index: ["title", "content"], - tag: tag, + tag: { tags: tag }, }) for (let searchResult of searchResults) { searchResult.result = searchResult.result.slice(0, numSearchResults)