Skip to content

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

:bug: Remove search highlights from block reference names

Daniele0315dd84de5
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts
index 7895bdc349..ac95de7072 100644
--- a/app/src/protyle/hint/extend.ts
+++ b/app/src/protyle/hint/extend.ts
@@ -14,7 +14,7 @@ import {transaction} from "../wysiwyg/transaction";
import {getAssetExtension, getAssetName, getDisplayName, isEncryptedBox} from "../../util/pathName";
import {genEmptyElement} from "../../block/util";
import {updateListOrder} from "../wysiwyg/list";
-import {escapeHtml} from "../../util/escape";
+import {escapeHtml, stripSearchMark} from "../../util/escape";
import {zoomOut} from "../../menus/protyle";
import {hideElements} from "../ui/hideElements";
import {genAssetHTML} from "../../asset/renderAssets";
@@ -449,13 +449,13 @@ export const genHintItemHTML = (item: IBlock) => {
}
let attrHTML = "";
if (item.name) {
- attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span>${escapeHtml(item.name)}</span></span><span class="fn__space"></span>`;
+ attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span>${item.name}</span></span><span class="fn__space"></span>`;
}
if (item.alias) {
- attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span>${escapeHtml(item.alias)}</span></span><span class="fn__space"></span>`;
+ attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span>${item.alias}</span></span><span class="fn__space"></span>`;
}
if (item.memo) {
- attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconM"></use></svg><span>${escapeHtml(item.memo)}</span></span>`;
+ attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconM"></use></svg><span>${item.memo}</span></span>`;
}
if (attrHTML) {
attrHTML = `<div class="fn__flex b3-list-item__meta b3-list-item__showall">${attrHTML}</div>`;
@@ -520,7 +520,7 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH
createItemCount++;
}
response.data.blocks.forEach((item: IBlock) => {
- const name = item.name ? escapeHtml(item.name) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
+ const name = item.name ? stripSearchMark(item.name) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
let value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="d">${name}</span>`;
if (source === "search") {
value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${key}${Constants.ZWSP}${name}</span>`;
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index d4f16fcd32..3e103e536f 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -33,7 +33,7 @@ import {highlightRender} from "../render/highlightRender";
import {assetMenu, imgMenu} from "../../menus/protyle";
import {hideElements} from "../ui/hideElements";
import {fetchPost} from "../../util/fetch";
-import {escapeHtml} from "../../util/escape";
+import {stripSearchMark} from "../../util/escape";
import {getDisplayName, isEncryptedBox, pathPosix} from "../../util/pathName";
import {
addEmoji,
@@ -469,7 +469,7 @@ export class Hint {
let blockRefHTML;
if (source === "av") {
// av 搜索时需要获取值 https://github.com/siyuan-note/siyuan/issues/12020
- let refText = item.name ? escapeHtml(item.name) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
+ let refText = item.name ? stripSearchMark(item.name) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
if (nodeElement) {
refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
}
diff --git a/app/src/util/escape.test.ts b/app/src/util/escape.test.ts
new file mode 100644
index 0000000000..09f37be26b
--- /dev/null
+++ b/app/src/util/escape.test.ts
@@ -0,0 +1,10 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {stripSearchMark} from "./escape";
+
+describe("strip search mark", () => {
+ it("removes search highlights without decoding escaped block names", () => {
+ assert.equal(stripSearchMark("<mark>xw</mark>"), "xw");
+ assert.equal(stripSearchMark("&lt;mark&gt;<mark>xw</mark>&lt;/mark&gt;"), "&lt;mark&gt;xw&lt;/mark&gt;");
+ });
+});
diff --git a/app/src/util/escape.ts b/app/src/util/escape.ts
index ed168f03aa..179df56307 100644
--- a/app/src/util/escape.ts
+++ b/app/src/util/escape.ts
@@ -5,6 +5,10 @@ export const escapeHtml = (html: string) => {
return html.replace(/&/g, "&amp;").replace(/</g, "&lt;");
};
+export const stripSearchMark = (html: string) => {
+ return html.replace(/<\/?mark>/g, "");
+};
+
export const escapeLessThans = (html: string) => {
return html.replace(/</g, "&lt;");
};