Skip to content

Commit 03ccac2

Browse files
authored
feat: Update FlexSearch and Add Support for All Languages (#2108)
* chore(deps): update flexsearch to version 0.8.205 and adjust search encoder. * 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. * refactor(search): remove rtl property from search index configuration * refactor(search): improve encoder function formatting - Updated the encoder function to use consistent arrow function syntax for better readability. * refactor(search): update search results type to DefaultDocumentSearchResults - Imported DefaultDocumentSearchResults from FlexSearch for improved type safety. - Changed the type of searchResults from 'any' to DefaultDocumentSearchResults<Item> for better clarity and maintainability.
1 parent 6add0c8 commit 03ccac2

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

package-lock.json

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"cli-spinner": "^0.2.10",
4747
"d3": "^7.9.0",
4848
"esbuild-sass-plugin": "^3.3.1",
49-
"flexsearch": "0.7.43",
49+
"flexsearch": "^0.8.205",
5050
"github-slugger": "^2.0.0",
5151
"globby": "^14.1.0",
5252
"gray-matter": "^4.0.3",

quartz/components/scripts/search.inline.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import FlexSearch from "flexsearch"
1+
import FlexSearch, { DefaultDocumentSearchResults } from "flexsearch"
22
import { ContentDetails } from "../../plugins/emitters/contentIndex"
33
import { registerEscapeHandler, removeAllChildren } from "./util"
44
import { FullSlug, normalizeRelativeURLs, resolveRelative } from "../../util/path"
@@ -9,15 +9,21 @@ interface Item {
99
title: string
1010
content: string
1111
tags: string[]
12+
[key: string]: any
1213
}
1314

1415
// Can be expanded with things like "term" in the future
1516
type SearchType = "basic" | "tags"
1617
let searchType: SearchType = "basic"
1718
let currentSearchTerm: string = ""
18-
const encoder = (str: string) => str.toLowerCase().split(/([^a-z]|[^\x00-\x7F])/)
19+
const encoder = (str: string) => {
20+
return str
21+
.toLowerCase()
22+
.split(/\s+/)
23+
.filter((token) => token.length > 0)
24+
}
25+
1926
let index = new FlexSearch.Document<Item>({
20-
charset: "latin:extra",
2127
encode: encoder,
2228
document: {
2329
id: "id",
@@ -397,7 +403,7 @@ async function setupSearch(searchElement: Element, currentSlug: FullSlug, data:
397403
searchLayout.classList.toggle("display-results", currentSearchTerm !== "")
398404
searchType = currentSearchTerm.startsWith("#") ? "tags" : "basic"
399405

400-
let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[]
406+
let searchResults: DefaultDocumentSearchResults<Item>
401407
if (searchType === "tags") {
402408
currentSearchTerm = currentSearchTerm.substring(1).trim()
403409
const separatorIndex = currentSearchTerm.indexOf(" ")
@@ -410,7 +416,7 @@ async function setupSearch(searchElement: Element, currentSlug: FullSlug, data:
410416
// return at least 10000 documents, so it is enough to filter them by tag (implemented in flexsearch)
411417
limit: Math.max(numSearchResults, 10000),
412418
index: ["title", "content"],
413-
tag: tag,
419+
tag: { tags: tag },
414420
})
415421
for (let searchResult of searchResults) {
416422
searchResult.result = searchResult.result.slice(0, numSearchResults)

0 commit comments

Comments
 (0)