Skip to content

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

:bug: Reuse plain-text escaping for inbox titles https://github.com/siyuan-note/siyuan/issues/19771

Daniel148bea492b8e
diff --git a/app/src/layout/dock/Inbox.test.ts b/app/src/layout/dock/Inbox.test.ts
index 0baa7c79e4..6d09198540 100644
--- a/app/src/layout/dock/Inbox.test.ts
+++ b/app/src/layout/dock/Inbox.test.ts
@@ -4,6 +4,7 @@ import {readFileSync} from "node:fs";
import {join} from "node:path";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
+import {escapeMarkdownPlainText} from "../../util/escape";
const loadInbox = (responses: {code: number; data?: Record<string, string>}[]) => {
const calls: {url: string; data: Record<string, string>}[] = [];
@@ -25,6 +26,7 @@ const loadInbox = (responses: {code: number; data?: Record<string, string>}[]) =
}};
}
if (name.endsWith("/message")) { return {showMessage: (message: string) => messages.push(message)}; }
+ if (name.endsWith("/escape")) { return {escapeMarkdownPlainText}; }
return {};
},
});
@@ -73,10 +75,10 @@ test("inbox insertion preserves link-only entries as Markdown links", async () =
test("inbox insertion keeps Markdown punctuation in the title as plain text", async () => {
const {inbox, calls, removed} = loadInbox([
- {code: 0, data: {shorthandMd: "Body", shorthandTitle: "A *bold* [note] $x$ ==mark== &amp; #tag#\ncontinued"}},
+ {code: 0, data: {shorthandMd: "Body", shorthandTitle: "A *bold* [note] $x$ ==mark== &amp; #tag# 1+2 50%?\ncontinued"}},
{code: 0},
]);
await inbox.insertToCurrentDoc(["entry"], "doc-id");
- assert.equal(calls[1].data.data, "# A \\*bold\\* \\[note\\] \\$x\\$ \\=\\=mark\\=\\= \\&amp\\; \\#tag\\# continued\n\nBody");
+ assert.equal(calls[1].data.data, "# A \\*bold\\* \\[note\\] \\$x\\$ \\=\\=mark\\=\\= \\&amp; \\#tag\\# 1+2 50%? continued\n\nBody");
assert.deepEqual(removed, [["entry"]]);
});
diff --git a/app/src/layout/dock/Inbox.ts b/app/src/layout/dock/Inbox.ts
index 1238e788d0..8ad6a4fc0e 100644
--- a/app/src/layout/dock/Inbox.ts
+++ b/app/src/layout/dock/Inbox.ts
@@ -15,7 +15,7 @@ import {getDisplayName, movePathTo, pathPosix} from "../../util/pathName";
import type {App} from "../../index";
import {getCloudURL} from "../../config/util/about";
import {hasClosestByClassName} from "../../protyle/util/hasClosest";
-import {escapeHtml} from "../../util/escape";
+import {escapeHtml, escapeMarkdownPlainText} from "../../util/escape";
import {emitOpenMenu} from "../../plugin/EventBus";
import {sanitizeKernelHTML} from "../../util/hostCapabilities";
import {showMessage} from "../../dialog/message";
@@ -421,9 +421,7 @@ ${data.shorthandContent}
if (!md && !shorthand.data.shorthandContent && shorthand.data.shorthandURL) {
md = `[${shorthand.data.shorthandTitle}](${shorthand.data.shorthandURL})`;
}
- // 转义 ASCII 标点,避免剪藏标题中的符号被解析为行内语法。
- const title = shorthand.data.shorthandTitle.replace(/[\r\n]+/g, " ").trim()
- .replace(/[\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]/g, "\\$&");
+ const title = escapeMarkdownPlainText(shorthand.data.shorthandTitle.replace(/[\r\n]+/g, " ").trim());
if (title) {
md = `# ${title}\n\n${md}`;
}
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index 30f9e29062..c35a7f5ace 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -1,5 +1,5 @@
import {Constants} from "../../constants";
-import {escapeHtml} from "../../util/escape";
+import {escapeHtml, escapeMarkdownPlainText} from "../../util/escape";
import {getTableCellPlainText} from "./tableCellRich";
import {uploadFiles, uploadLocalFiles} from "../upload";
import type {IUploadInsertOptions} from "../upload";
@@ -284,27 +284,7 @@ export const pasteEscaped = async (protyle: IProtyle, nodeElement: Element, prep
// 这里必须多加一个反斜杆,因为 Lute 在进行 Markdown 嵌套节点转换平铺标记节点时会剔除 Backslash 节点,
// 多加入的一个反斜杆会作为文本节点保留下来,后续 Spin 时刚好用于转义标记符
- clipText = clipText.replace(/\\/g, "\\\\")
- .replace(/\*/g, "\\*")
- .replace(/_/g, "\\_")
- .replace(/\[/g, "\\[")
- .replace(/]/g, "\\]")
- .replace(/!/g, "\\!")
- .replace(/`/g, "\\`")
- .replace(/</g, "\\<")
- .replace(/>/g, "\\>")
- .replace(/&/g, "\\&")
- .replace(/~/g, "\\~")
- .replace(/\{/g, "\\{")
- .replace(/}/g, "\\}")
- .replace(/\(/g, "\\(")
- .replace(/\)/g, "\\)")
- .replace(/=/g, "\\=")
- .replace(/#/g, "\\#")
- .replace(/\$/g, "\\$")
- .replace(/\^/g, "\\^")
- .replace(/\|/g, "\\|")
- .replace(/\./g, "\\.");
+ clipText = escapeMarkdownPlainText(clipText);
// 转义文本不能使用 DOM 结构 https://github.com/siyuan-note/siyuan/issues/11778
paste(protyle, {textPlain: clipText, textHTML: "", target: nodeElement as HTMLElement});
} catch (e) {
diff --git a/app/src/util/escape.ts b/app/src/util/escape.ts
index fb6d8ba38d..7fc575fd8f 100644
--- a/app/src/util/escape.ts
+++ b/app/src/util/escape.ts
@@ -5,6 +5,31 @@ export const escapeHtml = (html: string) => {
return html.replace(/&/g, "&amp;").replace(/</g, "&lt;");
};
+// 将纯文本中的 Markdown 标记转义,保留编辑器重新解析时的字面内容。
+export const escapeMarkdownPlainText = (text: string) => {
+ return text.replace(/\\/g, "\\\\")
+ .replace(/\*/g, "\\*")
+ .replace(/_/g, "\\_")
+ .replace(/\[/g, "\\[")
+ .replace(/]/g, "\\]")
+ .replace(/!/g, "\\!")
+ .replace(/`/g, "\\`")
+ .replace(/</g, "\\<")
+ .replace(/>/g, "\\>")
+ .replace(/&/g, "\\&")
+ .replace(/~/g, "\\~")
+ .replace(/\{/g, "\\{")
+ .replace(/}/g, "\\}")
+ .replace(/\(/g, "\\(")
+ .replace(/\)/g, "\\)")
+ .replace(/=/g, "\\=")
+ .replace(/#/g, "\\#")
+ .replace(/\$/g, "\\$")
+ .replace(/\^/g, "\\^")
+ .replace(/\|/g, "\\|")
+ .replace(/\./g, "\\.");
+};
+
export const stripSearchMark = (html: string) => {
return html.replace(/<\/?mark>/g, "");
};