Skip to content

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

:bug: Fix math and code rendering in database text fields https://github.com/siyuan-note/siyuan/issues/19423

Danielc275fdd3a91d
diff --git a/app/src/protyle/render/av/richText.test.ts b/app/src/protyle/render/av/richText.test.ts
index 223ac02901..0a38e6886e 100644
--- a/app/src/protyle/render/av/richText.test.ts
+++ b/app/src/protyle/render/av/richText.test.ts
@@ -514,6 +514,30 @@ describe("attribute view text value creation", () => {
});
describe("attribute view rich text DOM policy", () => {
+ it("preserves code content and prepares block and inline math for preview rendering", {
+ skip: hasDOM && typeof Lute !== "undefined" ? false :
+ "The Node test environment does not provide DOM and Lute globals",
+ }, async () => {
+ Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
+ const richText = await import("./richText");
+ const {genRenderFrame} = await import("../util");
+ const template = document.createElement("template");
+ template.innerHTML = richText.getAVRichTextPreviewHTML([
+ "```go", "package main", "```", "", "$$", "x^2", "$$", "", "$a^2 + b^2$",
+ ].join("\n"));
+
+ const code = template.content.querySelector("pre.code-block > code");
+ assert.equal(code?.textContent, "package main\n");
+ assert.equal(code.parentElement.dataset.language, "go");
+ assert.equal(template.content.querySelector(".protyle-action"), null);
+ const blockMath = template.content.querySelector<HTMLElement>('div[data-subtype="math"]');
+ assert.equal(blockMath?.dataset.content, "x^2");
+ assert.doesNotThrow(() => genRenderFrame(blockMath));
+ assert.ok(blockMath.firstElementChild.firstElementChild);
+ assert.equal(template.content.querySelector<HTMLElement>('span[data-subtype="math"]')?.dataset.content,
+ "a^2 + b^2");
+ });
+
it("preserves supported headings and inline marks while dropping unsupported blocks", {
skip: hasDOM ? false : "The Node test environment does not provide a DOM implementation",
}, async () => {
diff --git a/app/src/protyle/render/av/richText.ts b/app/src/protyle/render/av/richText.ts
index 3fd75daeb7..1f5a9dc060 100644
--- a/app/src/protyle/render/av/richText.ts
+++ b/app/src/protyle/render/av/richText.ts
@@ -340,9 +340,7 @@ export const getAVRichTextBlockDOM = (markdown: string, images = false) => markd
const getAVRichTextPreviewBlockDOM = (blockDOM: string) => {
const template = document.createElement("template");
template.innerHTML = cleanAVRichTextBlockDOMStructure(blockDOM);
- // 待办操作节点携带勾选状态,需要保留到 HTML 转换完成。
- template.content.querySelectorAll(".protyle-action:not(.protyle-action--task)")
- .forEach((element) => element.remove());
+ // 代码块操作节点和待办操作节点参与内容解析,需要保留到 HTML 转换完成。
return (template.innerHTML || "").trim();
};
@@ -359,7 +357,7 @@ const prepareAVRichTextPreviewHTML = (html: string) => {
element.className = "render-node";
element.dataset.subtype = "math";
element.dataset.content = content;
- element.textContent = "";
+ element.replaceChildren(document.createElement("div"));
});
template.content.querySelectorAll<HTMLElement>("pre > code").forEach((element) => {
const languageClass = Array.from(element.classList).find((className) => className.startsWith("language-"));
diff --git a/app/src/protyle/render/av/richTextValue.ts b/app/src/protyle/render/av/richTextValue.ts
index 9ab6d88190..c362b6edd8 100644
--- a/app/src/protyle/render/av/richTextValue.ts
+++ b/app/src/protyle/render/av/richTextValue.ts
@@ -25,6 +25,8 @@ export const AV_RICH_TEXT_PREVIEW_SANITIZE_OPTIONS = {
ALLOWED_ATTR: AV_RICH_TEXT_PREVIEW_ALLOWED_ATTRIBUTES,
ALLOW_ARIA_ATTR: false,
ALLOW_DATA_ATTR: false,
+ // 公式源码是纯文本,不按链接地址过滤;链接属性仍使用独立的地址校验。
+ ADD_URI_SAFE_ATTR: ["data-content"],
ALLOWED_URI_REGEXP: /^(?:(?:https?|mailto|siyuan|tel|web\+siyuan):|[#/?]|\.\.?\/|[^a-z]|[a-z0-9._~-]+(?:[/?#]|$))/i,
};
const BUILTIN_INLINE_COLOR_COUNT = 13;