Skip to content

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

:bug: Fix adjacent tags merging when inserting tag via hint list #18191

Daniel4ae1797f538a
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index e9593dda38..16a604978f 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -1,7 +1,7 @@
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByTag} from "./hasClosest";
import * as dayjs from "dayjs";
import {transaction, updateTransaction} from "../wysiwyg/transaction";
-import {getContenteditableElement, getParentBlock} from "../wysiwyg/getBlock";
+import {getContenteditableElement, getParentBlock, fixAdjacentTags} from "../wysiwyg/getBlock";
import {
fixTableRange,
focusBlock,
@@ -472,6 +472,8 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
range.insertNode(tempElement.content.cloneNode(true));
range.collapse(false);
blockElement.querySelector("wbr")?.remove();
+ // 相邻标签之间插入空格区隔,避免后续 SpinBlockDOM 解析时合并为一个标签 https://github.com/siyuan-note/siyuan/issues/18191
+ fixAdjacentTags(getContenteditableElement(blockElement));
protyle.wysiwyg.lastHTMLs[id] = oldHTML;
input(protyle, blockElement as HTMLElement, range);
return;
diff --git a/app/src/protyle/wysiwyg/getBlock.ts b/app/src/protyle/wysiwyg/getBlock.ts
index 4a6d02e103..b2b2ee0290 100644
--- a/app/src/protyle/wysiwyg/getBlock.ts
+++ b/app/src/protyle/wysiwyg/getBlock.ts
@@ -343,3 +343,31 @@ export const getPreviousFileLi = (current: Element) => {
}
return false;
};
+
+// 相邻标签之间插入空格区隔,避免 SpinBlockDOM 解析时合并为一个标签 https://github.com/siyuan-note/siyuan/issues/18191
+export const fixAdjacentTags = (editableElement: Element) => {
+ if (!editableElement) {
+ return;
+ }
+ let node: Node = editableElement.firstChild;
+ while (node) {
+ const next: Node = node.nextSibling;
+ if (node.nodeType !== 3) {
+ const tagSpan = node as HTMLElement;
+ if (tagSpan.tagName === "SPAN" &&
+ (tagSpan.getAttribute("data-type") || "").split(" ").includes("tag")) {
+ // 向后查找跳过 ZWSP 文本节点和 <wbr> 后的下一个节点
+ let after = next;
+ while (after && ((after.nodeType === 3 && after.textContent === Constants.ZWSP) ||
+ (after.nodeType === 1 && (after as HTMLElement).tagName === "WBR"))) {
+ after = after.nextSibling;
+ }
+ if (after && after.nodeType !== 3 && (after as HTMLElement).tagName === "SPAN" &&
+ ((after as HTMLElement).getAttribute("data-type") || "").split(" ").includes("tag")) {
+ tagSpan.after(" ");
+ }
+ }
+ }
+ node = next;
+ }
+};
diff --git a/app/src/protyle/wysiwyg/input.ts b/app/src/protyle/wysiwyg/input.ts
index 4069dd5d91..97bab712ea 100644
--- a/app/src/protyle/wysiwyg/input.ts
+++ b/app/src/protyle/wysiwyg/input.ts
@@ -4,7 +4,7 @@ import * as dayjs from "dayjs";
import {transaction, updateTransaction} from "./transaction";
import {mathRender} from "../render/mathRender";
import {highlightRender} from "../render/highlightRender";
-import {getContenteditableElement, hasNextSibling, hasPreviousSibling, isNotEditBlock} from "./getBlock";
+import {getContenteditableElement, fixAdjacentTags, hasNextSibling, hasPreviousSibling, isNotEditBlock} from "./getBlock";
import {genEmptyBlock} from "../../block/util";
import {blockRender} from "../render/blockRender";
import {hideElements} from "../ui/hideElements";
@@ -146,6 +146,8 @@ export const input = async (protyle: IProtyle, blockElement: HTMLElement, range:
refElement.setAttribute("data-subtype", "s");
}
}
+ // 相邻标签之间插入空格区隔,避免 SpinBlockDOM 解析时合并为一个标签 https://github.com/siyuan-note/siyuan/issues/18191
+ fixAdjacentTags(editElement);
let html = blockElement.outerHTML;
let focusHR = false;
if (["---", "___", "***"].includes(editElement.textContent) && type !== "NodeCodeBlock") {
@@ -183,6 +185,16 @@ export const input = async (protyle: IProtyle, blockElement: HTMLElement, range:
html = blockElement.outerHTML;
}
}
+ // 相邻标签之间插入空格区隔,避免 SpinBlockDOM 解析时合并为一个标签 https://github.com/siyuan-note/siyuan/issues/18191
+ // 使用迭代替换处理多个连续相邻标签(全局正则无法匹配重叠情况)
+ // 若中间含有 <wbr>(光标标记),替换后需保留 <wbr>,否则 focusByWbr 无法定位光标
+ let prevHTML: string;
+ do {
+ prevHTML = html;
+ html = html.replace(/(data-type="tag[^"]*">[\s\S]*?<\/span>)((?:\u200b|<wbr>)*)(<span data-type="tag[^"]*">)/, (match, before, between, after) => {
+ return before + (between.indexOf("<wbr>") > -1 ? " <wbr>" : " ") + after;
+ });
+ } while (html !== prevHTML);
html = protyle.lute.SpinBlockDOM(html);
}
// 在数学公式输入框中撤销到最后一步,再继续撤销会撤销编辑器正文内容,从而出发 input 事件