From 0bea81578f0bd06bff12c37bc66f8751c5e382cf Mon Sep 17 00:00:00 2001 From: Vanessa Date: Thu, 7 Aug 2025 12:01:20 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/15399 --- app/src/protyle/util/insertHTML.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts index fc7f260be..28f5274b3 100644 --- a/app/src/protyle/util/insertHTML.ts +++ b/app/src/protyle/util/insertHTML.ts @@ -307,12 +307,18 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false, (isNodeCodeBlock || protyle.toolbar.getCurrentType(range).includes("code"))) { range.deleteContents(); // 代码块需保持至少一个 \n https://github.com/siyuan-note/siyuan/pull/13271#issuecomment-2502672155 + let codeBlockIsEmpty = false; if (isNodeCodeBlock && editableElement.textContent === "") { - html += "\n"; + codeBlockIsEmpty = true; } range.insertNode(document.createTextNode(html.replace(/\r\n|\r|\u2028|\u2029/g, "\n"))); range.collapse(false); range.insertNode(document.createElement("wbr")); + if (codeBlockIsEmpty) { + // 代码块为空添加的 \n 需放在最后 https://github.com/siyuan-note/siyuan/issues/15399 + range.collapse(false); + range.insertNode(document.createTextNode("\n")); + } if (isNodeCodeBlock) { blockElement.querySelector('[data-render="true"]')?.removeAttribute("data-render"); highlightRender(blockElement);