Skip to content

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

:art: 重复(克隆)文档后自身内部块引不应该指向原文档中的块 https://github.com/siyuan-note/siyuan/issues/5673

Liang Ding39041c8e9401
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 55768d75b1..194ccd4acc 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -944,17 +944,45 @@ func DuplicateDoc(rootID string) (err error) {
p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
tree.Path = p
tree.HPath = tree.HPath + " " + titleSuffix
+
+ // 收集所有引用
+ refIDs := map[string]string{}
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering || ast.NodeBlockRefID != n.Type {
+ return ast.WalkContinue
+ }
+ refIDs[n.TokensStr()] = "1"
+ return ast.WalkContinue
+ })
+
+ // 重置块 ID
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || ast.NodeDocument == n.Type {
return ast.WalkContinue
}
if n.IsBlock() && "" != n.ID {
- n.ID = ast.NewNodeID()
+ newID := ast.NewNodeID()
+ if "1" == refIDs[n.ID] {
+ // 如果是文档自身的内部引用
+ refIDs[n.ID] = newID
+ }
+ n.ID = newID
n.SetIALAttr("id", n.ID)
}
return ast.WalkContinue
})
+ // 重置内部引用
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering || ast.NodeBlockRefID != n.Type {
+ return ast.WalkContinue
+ }
+ if "1" != refIDs[n.TokensStr()] {
+ n.Tokens = []byte(refIDs[n.TokensStr()])
+ }
+ return ast.WalkContinue
+ })
+
transaction := &Transaction{DoOperations: []*Operation{{Action: "create", Data: tree}}}
err = PerformTransactions(&[]*Transaction{transaction})
if nil != err {