Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Highlight matching text in document move search results https://github.com/siyuan-note/siyuan/issues/19317

Danielc792cb5a7f4f
diff --git a/app/src/util/pathName.ts b/app/src/util/pathName.ts
index 7c42075ad2..6ebb7304f0 100644
--- a/app/src/util/pathName.ts
+++ b/app/src/util/pathName.ts
@@ -19,6 +19,7 @@ import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {mergePathSegments} from "./mergePathSegments";
import {expandFileTree} from "../layout/dock/fileTreeAnimation";
import {getHostCapabilities} from "./hostCapabilities";
+import {highlightSearchText} from "./searchHighlight";
export const useShell = (cmd: "showItemInFolder" | "openPath", filePath: string) => {
if (!getHostCapabilities().localFileSystem) {
@@ -341,11 +342,17 @@ export const movePathTo = (options: {
searchTreeElement.classList.add("fn__none");
searchListElement.classList.remove("fn__none");
searchListElement.scrollTo(0, 0);
+ const keyword = inputElement.value;
+ const caseSensitive = window.siyuan.config.search.caseSensitive;
fetchPost("/api/filetree/searchDocs", {
- k: inputElement.value,
+ k: keyword,
flashcard: options.flashcard,
excludeIDs: options.rootIDs,
}, (data) => {
+ if (inputElement.value !== keyword) {
+ return;
+ }
+ const highlight = (text: string) => highlightSearchText(text, keyword, caseSensitive);
let fileHTML = "";
data.data.forEach((item: {
boxIcon: string,
@@ -366,14 +373,14 @@ export const movePathTo = (options: {
}
let attributesHTML = "";
if (item.name) {
- attributesHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%" aria-label="${window.siyuan.languages.name} ${escapeAriaLabel(item.name)}"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span class="b3-list-item__hinttext">${escapeHtml(item.name)}</span></span>`;
+ attributesHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%" aria-label="${window.siyuan.languages.name} ${escapeAriaLabel(item.name)}"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span class="b3-list-item__hinttext">${highlight(item.name)}</span></span>`;
}
if (item.alias) {
- attributesHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%" aria-label="${window.siyuan.languages.alias} ${escapeAriaLabel(item.alias)}"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span class="b3-list-item__hinttext">${escapeHtml(item.alias)}</span></span>`;
+ attributesHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%" aria-label="${window.siyuan.languages.alias} ${escapeAriaLabel(item.alias)}"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span class="b3-list-item__hinttext">${highlight(item.alias)}</span></span>`;
}
fileHTML += `<li class="b3-list-item${fileHTML === "" ? " b3-list-item--focus" : ""}" data-path="${item.path}" data-box="${item.box}"${getFileTreeDefaultIconAttr(item.boxIcon, "notebook")}>
${getFileTreeIconHTML(item.boxIcon, "notebook", "b3-list-item__graphic", true)}
- <span class="b3-list-item__showall fn__flex-1" style="padding: 4px 0; min-width: 0">${escapeHtml(item.hPath)}</span>
+ <span class="b3-list-item__showall fn__flex-1" style="padding: 4px 0; min-width: 0">${highlight(item.hPath)}</span>
${attributesHTML}
${countHTML}
</li>`;
diff --git a/app/src/util/searchHighlight.test.ts b/app/src/util/searchHighlight.test.ts
new file mode 100644
index 0000000000..3398aef02c
--- /dev/null
+++ b/app/src/util/searchHighlight.test.ts
@@ -0,0 +1,16 @@
+import {strict as assert} from "node:assert";
+import {test} from "node:test";
+import {highlightSearchText} from "./searchHighlight";
+
+test("highlights literal terms with the configured case sensitivity", () => {
+ assert.equal(highlightSearchText("Test/Math math", "math", true), "Test/Math <mark>math</mark>");
+ assert.equal(highlightSearchText("Test/Math math", "math", false), "Test/<mark>Math</mark> <mark>math</mark>");
+ assert.equal(highlightSearchText("a+b [x]", "a+b [x]", false), "<mark>a+b</mark> <mark>[x]</mark>");
+ assert.equal(highlightSearchText("数学,数学笔记", "数学笔记 数学", false), "<mark>数学</mark>,<mark>数学笔记</mark>");
+});
+
+test("escapes text without highlighting generated HTML or entities", () => {
+ assert.equal(highlightSearchText("<img> & amp", "img amp", false), "&lt;<mark>img</mark>> &amp; <mark>amp</mark>");
+ assert.equal(highlightSearchText("<mark>&", "<mark>", false), "<mark>&lt;mark></mark>&amp;");
+ assert.equal(highlightSearchText("<>&", " ", false), "&lt;>&amp;");
+});
diff --git a/app/src/util/searchHighlight.ts b/app/src/util/searchHighlight.ts
new file mode 100644
index 0000000000..68c74d7688
--- /dev/null
+++ b/app/src/util/searchHighlight.ts
@@ -0,0 +1,17 @@
+import {escapeHtml} from "./escape";
+
+export const highlightSearchText = (text: string, keyword: string, caseSensitive: boolean) => {
+ const terms = keyword.trim().split(/\s+/).filter(Boolean).sort((a, b) => b.length - a.length);
+ if (terms.length === 0) {
+ return escapeHtml(text);
+ }
+ const pattern = new RegExp(terms.map(term => term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), caseSensitive ? "g" : "gi");
+ let html = "";
+ let offset = 0;
+ // 在原始文本中匹配,分别转义片段,避免高亮破坏实体或插入 HTML。
+ for (const match of text.matchAll(pattern)) {
+ html += escapeHtml(text.slice(offset, match.index)) + `<mark>${escapeHtml(match[0])}</mark>`;
+ offset = match.index + match[0].length;
+ }
+ return html + escapeHtml(text.slice(offset));
+};