From c2bea8a4c4aeba440b8a7b043d7ece6343a9d263 Mon Sep 17 00:00:00 2001
From: derfalx <kschneider@falx.tech>
Date: Thu, 08 Jan 2026 01:30:42 +0000
Subject: [PATCH] fix(citation): Language parameter for non en-US settings (#2075)
---
quartz/components/scripts/search.inline.ts | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 50 insertions(+), 6 deletions(-)
diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts
index 27f74ec..717f17f 100644
--- a/quartz/components/scripts/search.inline.ts
+++ b/quartz/components/scripts/search.inline.ts
@@ -1,4 +1,4 @@
-import FlexSearch from "flexsearch"
+import FlexSearch, { DefaultDocumentSearchResults } from "flexsearch"
import { ContentDetails } from "../../plugins/emitters/contentIndex"
import { registerEscapeHandler, removeAllChildren } from "./util"
import { FullSlug, normalizeRelativeURLs, resolveRelative } from "../../util/path"
@@ -9,15 +9,59 @@
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(/([^a-z]|[^\x00-\x7F])/)
+const encoder = (str: string): string[] => {
+ const tokens: string[] = []
+ let bufferStart = -1
+ let bufferEnd = -1
+ const lower = str.toLowerCase()
+
+ let i = 0
+ for (const char of lower) {
+ const code = char.codePointAt(0)!
+
+ const isCJK =
+ (code >= 0x3040 && code <= 0x309f) ||
+ (code >= 0x30a0 && code <= 0x30ff) ||
+ (code >= 0x4e00 && code <= 0x9fff) ||
+ (code >= 0xac00 && code <= 0xd7af) ||
+ (code >= 0x20000 && code <= 0x2a6df)
+
+ const isWhitespace = code === 32 || code === 9 || code === 10 || code === 13
+
+ if (isCJK) {
+ if (bufferStart !== -1) {
+ tokens.push(lower.slice(bufferStart, bufferEnd))
+ bufferStart = -1
+ }
+ tokens.push(char)
+ } else if (isWhitespace) {
+ if (bufferStart !== -1) {
+ tokens.push(lower.slice(bufferStart, bufferEnd))
+ bufferStart = -1
+ }
+ } else {
+ if (bufferStart === -1) bufferStart = i
+ bufferEnd = i + char.length
+ }
+
+ i += char.length
+ }
+
+ if (bufferStart !== -1) {
+ tokens.push(lower.slice(bufferStart))
+ }
+
+ return tokens
+}
+
let index = new FlexSearch.Document<Item>({
- charset: "latin:extra",
encode: encoder,
document: {
id: "id",
@@ -220,7 +264,7 @@
// If search is active, then we will render the first result and display accordingly
if (!container.classList.contains("active")) return
- if (e.key === "Enter") {
+ if (e.key === "Enter" && !e.isComposing) {
// If result has focus, navigate to that one, otherwise pick first result
if (results.contains(document.activeElement)) {
const active = document.activeElement as HTMLInputElement
@@ -397,7 +441,7 @@
searchLayout.classList.toggle("display-results", currentSearchTerm !== "")
searchType = currentSearchTerm.startsWith("#") ? "tags" : "basic"
- let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[]
+ let searchResults: DefaultDocumentSearchResults<Item>
if (searchType === "tags") {
currentSearchTerm = currentSearchTerm.substring(1).trim()
const separatorIndex = currentSearchTerm.indexOf(" ")
@@ -410,7 +454,7 @@
// 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)
--
Gitblit v1.10.0