Skip to content

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

:bug: Preserve HTML block source when pasting https://github.com/siyuan-note/siyuan/issues/18941

Daniel93b861ca3be2
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index 28f8f8784a..d992c73cb2 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -26,7 +26,11 @@ import {updateTransaction} from "../wysiwyg/transaction";
import * as dayjs from "dayjs";
import {updateListOrder} from "../wysiwyg/list";
import {refreshSbAndPersistWidth} from "../../block/util";
-import {extractCrossBlockPasteContext, shouldPreservePastedBlockStructure} from "./pasteSource";
+import {
+ extractCrossBlockPasteContext,
+ serializePastedBlockDOM,
+ shouldPreservePastedBlockStructure
+} from "./pasteSource";
import {normalizePasteResponse} from "./pasteResponse";
import {applyLuteMarkdownSyntax} from "../render/luteMarkdownSyntax";
import {convertOfficeLists} from "./officeList";
@@ -894,7 +898,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
return;
}
- let tempInnerHTML = tempElement.innerHTML;
+ // innerHTML 已按属性上下文转义 HTML 块源码,保留序列化结果才能避免 data-content 被内层引号截断。
+ const tempInnerHTML = serializePastedBlockDOM(tempElement);
if (!nodeElement.classList.contains("av") && tempInnerHTML.startsWith("[[{") && tempInnerHTML.endsWith("}]]")) {
try {
@@ -908,11 +913,6 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
insertHTML(tempInnerHTML, protyle, isBlock);
}
} else {
- if (-1 < tempInnerHTML.indexOf("NodeHTMLBlock")) {
- // 复制 HTML 块粘贴出来的不是 HTML 块 https://github.com/siyuan-note/siyuan/issues/12994
- tempInnerHTML = Lute.UnEscapeHTMLStr(tempInnerHTML);
- }
-
insertHTML(tempInnerHTML, protyle, isBlock, false, true);
}
blockRender(protyle, protyle.wysiwyg.element);
diff --git a/app/src/protyle/util/pasteSource.test.ts b/app/src/protyle/util/pasteSource.test.ts
index 94b4f6177c..32b8b12f6e 100644
--- a/app/src/protyle/util/pasteSource.test.ts
+++ b/app/src/protyle/util/pasteSource.test.ts
@@ -4,6 +4,7 @@ import {
extractCrossBlockPasteContext,
isNestedListCrossBlockSelection,
NESTED_LIST_PASTE_MARKER,
+ serializePastedBlockDOM,
shouldPreservePastedBlockStructure,
} from "./pasteSource";
@@ -85,3 +86,12 @@ describe("extractCrossBlockPasteContext", () => {
});
});
});
+
+describe("serializePastedBlockDOM", () => {
+ it("preserves escaped HTML block attributes", () => {
+ const html = '<div data-type="NodeHTMLBlock"><protyle-html data-content="&lt;div&gt;\n' +
+ '&lt;div style=&quot;page-break-after: always;&quot;&gt;&lt;/div&gt;\n&lt;/div&gt;"></protyle-html></div>';
+
+ assert.equal(serializePastedBlockDOM({innerHTML: html}), html);
+ });
+});
diff --git a/app/src/protyle/util/pasteSource.ts b/app/src/protyle/util/pasteSource.ts
index c99349dbba..1bf14540ae 100644
--- a/app/src/protyle/util/pasteSource.ts
+++ b/app/src/protyle/util/pasteSource.ts
@@ -1,5 +1,7 @@
export const NESTED_LIST_PASTE_MARKER = "<!--data-siyuan-paste='nested-list'-->";
+export const serializePastedBlockDOM = (container: Pick<HTMLElement, "innerHTML">) => container.innerHTML;
+
export const isNestedListCrossBlockSelection = (startListItem?: Element | false, endListItem?: Element | false) => {
return !!startListItem && !!endListItem && startListItem !== endListItem && startListItem.contains(endListItem);
};