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 adjacent characters when creating block references https://github.com/siyuan-note/siyuan/issues/18626
Daniel32230ecb6c96
diff --git a/app/src/protyle/hint/blockHintRange.test.ts b/app/src/protyle/hint/blockHintRange.test.tsnew file mode 100644index 0000000000..ff11154bbe--- /dev/null+++ b/app/src/protyle/hint/blockHintRange.test.ts@@ -0,0 +1,37 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {getBlockHintCloseLength} from "./blockHintRange";++describe("getBlockHintCloseLength", () => {+ it("keeps the outer closing bracket when creating a reference inside nested brackets", () => {+ const text = "[[[]]";+ const triggerOffset = 1;+ const caretOffset = 3;+ const closeLength = getBlockHintCloseLength(text.substring(0, triggerOffset),+ text.substring(caretOffset), "[[", "]]");++ assert.equal(text.substring(0, triggerOffset) + "reference" ++ text.substring(caretOffset + closeLength), "[reference]");+ });++ it("removes an automatically completed closing marker", () => {+ assert.equal(getBlockHintCloseLength("text", "]]tail", "[[", "]]"), 2);+ assert.equal(getBlockHintCloseLength("text", "))tail", "((", "))"), 2);+ });++ it("preserves closing markers belonging to outer pairs", () => {+ assert.equal(getBlockHintCloseLength("[", "]]", "[[", "]]"), 1);+ assert.equal(getBlockHintCloseLength("[[", "]]]", "[[", "]]"), 1);+ assert.equal(getBlockHintCloseLength("【", "】】", "【【", "】】"), 1);+ });++ it("does not remove unrelated closing markers", () => {+ assert.equal(getBlockHintCloseLength("text", "tail]]", "[[", "]]"), 0);+ assert.equal(getBlockHintCloseLength("text))", "tail", "((", "))"), 0);+ });++ it("accounts for balanced pairs before the trigger", () => {+ assert.equal(getBlockHintCloseLength("[done]", "]]", "[[", "]]"), 2);+ assert.equal(getBlockHintCloseLength("(done)", "))", "((", "))"), 2);+ });+});diff --git a/app/src/protyle/hint/blockHintRange.ts b/app/src/protyle/hint/blockHintRange.tsnew file mode 100644index 0000000000..d2efd16932--- /dev/null+++ b/app/src/protyle/hint/blockHintRange.ts@@ -0,0 +1,24 @@+export const getBlockHintCloseLength = (textBeforeTrigger: string, textAfterCaret: string,+ splitChar: string, endSplit: string) => {+ const openChar = splitChar.substring(0, 1);+ const closeChar = endSplit.substring(0, 1);+ if (!openChar || !closeChar || splitChar !== openChar.repeat(splitChar.length) ||+ endSplit !== closeChar.repeat(endSplit.length) || !textAfterCaret.startsWith(endSplit)) {+ return 0;+ }++ let unmatchedOpenCount = 0;+ for (const char of textBeforeTrigger) {+ if (char === openChar) {+ unmatchedOpenCount++;+ } else if (char === closeChar && unmatchedOpenCount > 0) {+ unmatchedOpenCount--;+ }+ }++ let closeRunLength = 0;+ while (textAfterCaret.substring(closeRunLength, closeRunLength + 1) === closeChar) {+ closeRunLength++;+ }+ return Math.min(endSplit.length, Math.max(0, closeRunLength - unmatchedOpenCount));+};diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.tsindex 3e103e536f..ceaf634eda 100644--- a/app/src/protyle/hint/index.ts+++ b/app/src/protyle/hint/index.ts@@ -57,6 +57,7 @@ import {updateAttrViewCellAnimation} from "../render/av/action";import {setFold} from "../util/blockFold";import {getIconValueKind} from "../../emoji/iconValue";import {getCreateTargetContext, isSameCreateTargetContext} from "./createTargetContext";+import {getBlockHintCloseLength} from "./blockHintRange";const genEmojiInsertHTML = (value: string) => {const kind = getIconValueKind(value);@@ -81,6 +82,32 @@ type TCreateTargetSession = {targets: Partial<Record<TCreateTargetType, TCreateTargetState>>;};+const getWholeTextOffset = (textNode: Text, offset: number) => {+ let previousSibling = textNode.previousSibling;+ while (previousSibling?.nodeType === 3) {+ offset += previousSibling.textContent.length;+ previousSibling = previousSibling.previousSibling;+ }+ return offset;+};++const setRangeEndByWholeTextOffset = (range: Range, textNode: Text, offset: number) => {+ while (textNode.previousSibling?.nodeType === 3) {+ textNode = textNode.previousSibling as Text;+ }+ while (textNode) {+ if (offset <= textNode.textContent.length) {+ range.setEnd(textNode, offset);+ return;+ }+ offset -= textNode.textContent.length;+ if (textNode.nextSibling?.nodeType !== 3) {+ return;+ }+ textNode = textNode.nextSibling as Text;+ }+};+export class Hint {public timeId: number;public element: HTMLDivElement;@@ -633,29 +660,16 @@ ${genHintItemHTML(item)}// QQ 拼音输入法自动补全需移除补全内容 https://github.com/siyuan-note/siyuan/issues/320// 前后有标记符的情况 https://github.com/siyuan-note/siyuan/issues/2511const endSplit = Constants.BLOCK_HINT_CLOSE_KEYS[this.splitChar];- if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && endSplit && range.startContainer.nodeType === 3- && (range.startContainer as Text).wholeText.indexOf(endSplit) > -1- // 在包含 )) 的块中引用时会丢失字符 https://ld246.com/article/1679980200782- && (range.startContainer as Text).wholeText.indexOf(this.splitChar) > -1) {- let matchEndChar = 0;- let textNode = range.startContainer;- while (textNode && matchEndChar < 2) {- const index = textNode.textContent.indexOf(endSplit);- const startIndex = textNode.textContent.indexOf(this.splitChar);- if (index > -1 && (index < startIndex || startIndex < 0)) {- matchEndChar = 2;- range.setEnd(textNode, index + 2);- break;- }- const indexOne = textNode.textContent.indexOf(endSplit.substr(1));- if (indexOne > -1) {- matchEndChar += 1;- }- if (matchEndChar === 2) {- range.setEnd(textNode, indexOne + 1);- break;- }- textNode = textNode.nextSibling;+ // 仅移除当前提示所属的闭合符号,保留外层配对和后续文本中的同名符号+ if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && endSplit && this.lastIndex > -1 &&+ range.startContainer.nodeType === 3) {+ const textNode = range.startContainer as Text;+ const caretOffset = getWholeTextOffset(textNode, range.startOffset);+ const triggerOffset = getWholeTextOffset(textNode, this.lastIndex);+ const closeLength = getBlockHintCloseLength(textNode.wholeText.substring(0, triggerOffset),+ textNode.wholeText.substring(caretOffset), this.splitChar, endSplit);+ if (closeLength > 0) {+ setRangeEndByWholeTextOffset(range, textNode, caretOffset + closeLength);}}