Skip to content

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

:bug: Exclude inline memos from dynamic block reference text https://github.com/siyuan-note/siyuan/issues/20788

Daniel98814ce84efa
diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go
index 30abc83315..919cac9e03 100644
--- a/kernel/model/blockinfo.go
+++ b/kernel/model/blockinfo.go
@@ -307,18 +307,6 @@ func GetBlockRefText(id string) string {
return ErrBlockNotFound.Error()
}
- ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering {
- return ast.WalkContinue
- }
-
- if n.IsTextMarkType("inline-memo") {
- // Block ref anchor text no longer contains contents of inline-level memos https://github.com/siyuan-note/siyuan/issues/9363
- n.TextMarkInlineMemoContent = ""
- return ast.WalkContinue
- }
- return ast.WalkContinue
- })
return getNodeRefText(node)
}
@@ -408,11 +396,7 @@ func getNodeRefText0(node *ast.Node, maxLen int, removeLineBreak bool) string {
return "Audio..."
case ast.NodeTabItem:
if title := treenode.TabTitleParagraph(node); nil != title {
- ret := renderBlockText(title, nil, removeLineBreak)
- if maxLen < utf8.RuneCountInString(ret) {
- ret = gulu.Str.SubStr(ret, maxLen) + "..."
- }
- return ret
+ return getNodeRefText0(title, maxLen, removeLineBreak)
}
case ast.NodeAttributeView:
ret, _ := av.GetAttributeViewName(node.AttributeViewID)
@@ -425,6 +409,26 @@ func getNodeRefText0(node *ast.Node, maxLen int, removeLineBreak bool) string {
if ast.NodeDocument != node.Type && node.IsContainerBlock() {
node = treenode.FirstLeafBlock(node)
}
+ if nil != node && ast.NodeDocument != node.Type {
+ hasMemo := false
+ ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && n.IsTextMarkType("inline-memo") && "" != n.TextMarkInlineMemoContent {
+ hasMemo = true
+ return ast.WalkStop
+ }
+ return ast.WalkContinue
+ })
+ if hasMemo {
+ // 锚文本只保留可见文字,在副本上过滤备注以保留源块内容。
+ node = cloneRenderNode(node)
+ ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && n.IsTextMarkType("inline-memo") {
+ n.TextMarkInlineMemoContent = ""
+ }
+ return ast.WalkContinue
+ })
+ }
+ }
ret := renderBlockText(node, nil, removeLineBreak)
if maxLen < utf8.RuneCountInString(ret) {
ret = gulu.Str.SubStr(ret, maxLen) + "..."
diff --git a/kernel/model/blockinfo_test.go b/kernel/model/blockinfo_test.go
index 6b77b03ead..c3edbc2b07 100644
--- a/kernel/model/blockinfo_test.go
+++ b/kernel/model/blockinfo_test.go
@@ -8,6 +8,7 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -185,3 +186,112 @@ func newDocBlocksOrdersTestTree(boxID, docID string, childIDs []string) *parse.T
}
return tree
}
+
+func TestNodeRefTextExcludesInlineMemo(t *testing.T) {
+ oldConf := Conf
+ Conf = &AppConf{Editor: NewConfSection(conf.NewEditor())}
+ t.Cleanup(func() { Conf = oldConf })
+ for _, markType := range []string{"inline-memo", "strong inline-memo", "inline-math inline-memo"} {
+ t.Run(markType, func(t *testing.T) {
+ paragraph := &ast.Node{Type: ast.NodeParagraph}
+ paragraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("before ")})
+ memo := &ast.Node{Type: ast.NodeTextMark, TextMarkType: markType,
+ TextMarkTextContent: "visible", TextMarkInlineMathContent: "visible",
+ TextMarkInlineMemoContent: "private memo"}
+ paragraph.AppendChild(memo)
+ paragraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(" after")})
+ for _, container := range []ast.NodeType{ast.NodeParagraph, ast.NodeBlockquote, ast.NodeTabItem} {
+ t.Run(container.String(), func(t *testing.T) {
+ node := paragraph
+ if container == ast.NodeBlockquote {
+ node = &ast.Node{Type: container}
+ node.AppendChild(cloneRenderNode(paragraph))
+ } else if container == ast.NodeTabItem {
+ node = &ast.Node{Type: container, TabItemTitle: `<span data-type="inline-memo" data-inline-memo-content="private memo">before visible after</span>`}
+ }
+ if text := getNodeRefText(node); text != "before visible after" {
+ t.Fatalf("unexpected reference text: %q", text)
+ }
+ if text := getNodeRefText0(node, 10, true); text != "before vis..." {
+ t.Fatalf("unexpected truncated reference text: %q", text)
+ }
+ })
+ }
+ if memo.TextMarkInlineMemoContent != "private memo" {
+ t.Fatal("reference text extraction modified the source memo")
+ }
+ })
+ }
+}
+
+func TestUpdateRefTextExcludesInlineMemo(t *testing.T) {
+ oldConf := Conf
+ Conf = &AppConf{Editor: NewConfSection(conf.NewEditor())}
+ t.Cleanup(func() { Conf = oldConf })
+ const defID = "20261021000000-def0001"
+ t.Cleanup(func() { treenode.DynamicRefTexts.Delete(defID) })
+ def := &ast.Node{Type: ast.NodeParagraph, ID: defID}
+ memo := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "inline-memo", TextMarkTextContent: "visible",
+ TextMarkInlineMemoContent: "private memo"}
+ def.AppendChild(memo)
+ refs := &ast.Node{Type: ast.NodeParagraph}
+ dynamic := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref", TextMarkBlockRefID: defID,
+ TextMarkBlockRefSubtype: "d", TextMarkTextContent: "old"}
+ static := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref", TextMarkBlockRefID: defID,
+ TextMarkBlockRefSubtype: "s", TextMarkTextContent: "manual anchor"}
+ refs.AppendChild(dynamic)
+ refs.AppendChild(static)
+ if changed, defs := updateRefText(refs, map[string]*ast.Node{defID: def}); !changed || len(defs) != 1 || defs[0].refText != "visible" {
+ t.Fatalf("unexpected dynamic reference update: changed=%v, definitions=%+v", changed, defs)
+ }
+ if dynamic.TextMarkTextContent != "visible" || static.TextMarkTextContent != "manual anchor" {
+ t.Fatalf("unexpected anchors: dynamic=%q, static=%q", dynamic.TextMarkTextContent, static.TextMarkTextContent)
+ }
+ memo.TextMarkInlineMemoContent = "edited memo"
+ if changed, _ := updateRefText(refs, map[string]*ast.Node{defID: def}); changed {
+ t.Fatal("editing only the memo changed the reference text")
+ }
+ if text := getNodeRefText(def); text != dynamic.TextMarkTextContent || memo.TextMarkInlineMemoContent != "edited memo" {
+ t.Fatalf("inconsistent reference text or modified source: text=%q, memo=%q", text, memo.TextMarkInlineMemoContent)
+ }
+}
+
+func TestGetBlockRefTextKeepsInlineMemo(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ Conf.Editor.Store(conf.NewEditor())
+ tree, err := LoadTreeByBlockID(fixture.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ paragraph := tree.Root.FirstChild
+ paragraph.AppendChild(&ast.Node{Type: ast.NodeTextMark, TextMarkType: "inline-memo strong",
+ TextMarkTextContent: "visible", TextMarkInlineMemoContent: "private memo"})
+ if _, err = filesys.WriteTree(tree); err != nil {
+ t.Fatal(err)
+ }
+ treenode.UpsertBlockTree(tree)
+ for _, getter := range []struct {
+ name string
+ text func() string
+ }{
+ {"default", func() string { return GetBlockRefText(paragraph.ID) }},
+ {"in box", func() string { return GetBlockRefTextInBox(paragraph.ID, tree.Box) }},
+ } {
+ t.Run(getter.name, func(t *testing.T) {
+ for range 2 {
+ cache.RemoveTreeDataInBox(tree.ID, tree.Box)
+ if text := getter.text(); text != "visible" {
+ t.Fatalf("unexpected reference text: %q", text)
+ }
+ loaded, loadErr := LoadTreeByBlockID(paragraph.ID)
+ if loadErr != nil {
+ t.Fatal(loadErr)
+ }
+ memo := treenode.GetNodeInTree(loaded, paragraph.ID).FirstChild
+ if memo.TextMarkInlineMemoContent != "private memo" {
+ t.Fatalf("source memo changed after reading reference text: %q", memo.TextMarkInlineMemoContent)
+ }
+ }
+ })
+ }
+}
diff --git a/kernel/model/encrypted_ops.go b/kernel/model/encrypted_ops.go
index 2af339e0d5..7d0d4fd96d 100644
--- a/kernel/model/encrypted_ops.go
+++ b/kernel/model/encrypted_ops.go
@@ -17,7 +17,6 @@
package model
import (
- "github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
)
@@ -45,17 +44,6 @@ func GetBlockRefTextInBox(id, boxID string) string {
return ErrBlockNotFound.Error()
}
- ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering {
- return ast.WalkContinue
- }
- if n.IsTextMarkType("inline-memo") {
- n.TextMarkInlineMemoContent = ""
- return ast.WalkContinue
- }
- return ast.WalkContinue
- })
-
return getNodeRefText(node)
}