An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve Markdown formatting in AI editing selections https://github.com/siyuan-note/siyuan/issues/19023
Daniel597732e55911
diff --git a/app/src/ai/editor.ts b/app/src/ai/editor.tsindex f9483fd1d8..88c95f90e4 100644--- a/app/src/ai/editor.ts+++ b/app/src/ai/editor.ts@@ -154,11 +154,22 @@ const getSourceConflict = (task: IAIEditorTask) => {return {missing, changed};};-const sourcePartText = (protyle: IProtyle, range: Range) => {+const isMarkdownInlineElement = (element: Element) => {+ return element.matches("span[data-type], code, strong, em, b, i, s, del, u, mark, sub, sup, kbd, a");+};++const sourcePartMarkdown = (protyle: IProtyle, range: Range, editableElement: Element) => {const text = range.toString().replace(new RegExp(Constants.ZWSP, "g"), "");- const fragment = range.cloneContents();- if (!fragment.querySelector('.img, [data-type~="inline-math"], [data-type~="a"], [data-type~="block-ref"]')) {- return text;+ let fragment: Node = range.cloneContents();+ let ancestor = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE ?+ range.commonAncestorContainer as Element : range.commonAncestorContainer.parentElement;+ while (ancestor && ancestor !== editableElement && editableElement.contains(ancestor)) {+ if (isMarkdownInlineElement(ancestor)) {+ const clone = ancestor.cloneNode(false) as Element;+ clone.append(fragment);+ fragment = clone;+ }+ ancestor = ancestor.parentElement;}const element = document.createElement("div");element.append(fragment);@@ -180,7 +191,8 @@ const buildSelectionSource = (protyle: IProtyle, range: Range, action: string):kind: "selection",parts,ids: [],- input: blockRanges.map(item => sourcePartText(protyle, item.range)).filter(Boolean).join("\n\n"),+ input: blockRanges.map(item => sourcePartMarkdown(protyle, item.range, item.editableElement))+ .filter(Boolean).join("\n\n"),action,insertSupported: !protyle.disabled,};