Skip to content

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

:bug: Remap block hyperlinks when regenerating document IDs https://github.com/siyuan-note/siyuan/issues/19659

Daniel849cbbc54039
diff --git a/kernel/model/tabs.go b/kernel/model/tabs.go
deleted file mode 100644
index 360f3ba8a8..0000000000
--- a/kernel/model/tabs.go
+++ /dev/null
@@ -1,33 +0,0 @@
-// SiYuan - From thought to insight, with agents
-// Copyright (c) 2020-present, b3log.org
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-package model
-
-import (
- "strings"
-
- "github.com/88250/lute/ast"
- "github.com/siyuan-note/siyuan/kernel/treenode"
-)
-
-// remapTabTitleBlockIDs 改写页签标题内的块引用和块链接,保留对外部块的引用。
-func remapTabTitleBlockIDs(root *ast.Node, ids map[string]string) {
- treenode.WalkWithTabTitles(root, func(node *ast.Node, entering bool) ast.WalkStatus {
- if !entering || nil == node.Parent || !node.ParentIs(ast.NodeTabItem) {
- return ast.WalkContinue
- }
- if treenode.IsBlockRef(node) {
- id, _, _ := treenode.GetBlockRef(node)
- if mapped := ids[id]; "" != mapped {
- node.TextMarkBlockRefID = mapped
- }
- } else if treenode.IsBlockLink(node) {
- id := strings.TrimPrefix(node.TextMarkAHref, "siyuan://blocks/")
- if mapped := ids[id]; "" != mapped {
- node.TextMarkAHref = "siyuan://blocks/" + mapped
- }
- }
- return ast.WalkContinue
- })
-}
diff --git a/kernel/model/tabs_test.go b/kernel/model/tabs_test.go
index 0a78b3b9b5..84d3f49dc7 100644
--- a/kernel/model/tabs_test.go
+++ b/kernel/model/tabs_test.go
@@ -44,3 +44,50 @@ func TestTabTitleAssetScanRewriteAndExport(t *testing.T) {
t.Fatalf("export asset map: %v", oldNew)
}
}
+
+func TestResetTreeRemapsInternalReferences(t *testing.T) {
+ const oldRootID = "20260919120000-doc0001"
+ const oldID = "20260919120000-para001"
+ const externalID = "20260919120000-extern1"
+ tree := treenode.NewTree("20260919120000-box0001", "/"+oldRootID+".sy", "/Links", "Links")
+ paragraph := treenode.NewParagraph(oldID)
+ tree.Root.AppendChild(paragraph)
+ var links []*ast.Node
+ for _, target := range []string{oldRootID, oldID, externalID} {
+ for _, suffix := range []string{"", "?focus=1#anchor"} {
+ link := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "a", TextMarkAHref: "siyuan://blocks/" + target + suffix}
+ paragraph.AppendChild(link)
+ links = append(links, link)
+ }
+ }
+ internalRef := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref", TextMarkBlockRefID: oldID}
+ externalRef := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref", TextMarkBlockRefID: externalID}
+ paragraph.AppendChild(internalRef)
+ paragraph.AppendChild(externalRef)
+ tabs := &ast.Node{Type: ast.NodeTabs, ID: "20260919120000-tabs001"}
+ item := &ast.Node{Type: ast.NodeTabItem, ID: "20260919120000-item001",
+ TabItemTitle: "[link](siyuan://blocks/" + oldID + "?focus=1) ((" + externalID + " 'external'))"}
+ item.AppendChild(treenode.NewParagraph("20260919120000-body001"))
+ tabs.AppendChild(item)
+ tabs.SetIALAttr(treenode.TabsActiveIDAttr, item.ID)
+ tree.Root.AppendChild(tabs)
+
+ resetTree(tree, "", false)
+ if tree.ID == oldRootID || paragraph.ID == oldID {
+ t.Fatal("block IDs were not regenerated")
+ }
+ for i, target := range []string{tree.ID, paragraph.ID, externalID} {
+ for j, suffix := range []string{"", "?focus=1#anchor"} {
+ if links[i*2+j].TextMarkAHref != "siyuan://blocks/"+target+suffix {
+ t.Fatalf("incorrect remapped link: %s", links[i*2+j].TextMarkAHref)
+ }
+ }
+ }
+ if internalRef.TextMarkBlockRefID != paragraph.ID || externalRef.TextMarkBlockRefID != externalID {
+ t.Fatal("internal or external block reference changed incorrectly")
+ }
+ if !strings.Contains(item.TabItemTitle, "siyuan://blocks/"+paragraph.ID+"?focus=1") ||
+ !strings.Contains(item.TabItemTitle, externalID) || tabs.IALAttr(treenode.TabsActiveIDAttr) != item.ID {
+ t.Fatalf("tab title or active item changed incorrectly: %s", item.TabItemTitle)
+ }
+}
diff --git a/kernel/model/tree.go b/kernel/model/tree.go
index 4b517cf8d0..6e63c4eaa6 100644
--- a/kernel/model/tree.go
+++ b/kernel/model/tree.go
@@ -66,20 +66,6 @@ func resetTree(tree *parse.Tree, titleSuffix string, removeAvBinding bool) {
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 || !treenode.IsBlockRef(n) {
- return ast.WalkContinue
- }
- defID, _, _ := treenode.GetBlockRef(n)
- if "" == defID {
- return ast.WalkContinue
- }
- refIDs[defID] = "1"
- return ast.WalkContinue
- })
-
// 重置块 ID
blockIDs := map[string]string{oldRootID: tree.ID}
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
@@ -89,34 +75,13 @@ func resetTree(tree *parse.Tree, titleSuffix string, removeAvBinding bool) {
if n.IsBlock() && "" != n.ID {
newID := ast.NewNodeID()
blockIDs[n.ID] = newID
- if "1" == refIDs[n.ID] {
- // 如果是文档自身的内部引用
- refIDs[n.ID] = newID
- }
n.ID = newID
n.SetIALAttr("id", n.ID)
}
return ast.WalkContinue
})
- treenode.RemapTabsActiveIDs(tree.Root, blockIDs)
- remapTabTitleBlockIDs(tree.Root, blockIDs)
- // 重置内部引用
- treenode.WalkWithTabTitles(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering || !treenode.IsBlockRef(n) {
- return ast.WalkContinue
- }
- defID, _, _ := treenode.GetBlockRef(n)
- if "" == defID {
- return ast.WalkContinue
- }
- if "1" != refIDs[defID] {
- if ast.NodeTextMark == n.Type {
- n.TextMarkBlockRefID = refIDs[defID]
- }
- }
- return ast.WalkContinue
- })
+ remapDuplicateDocTreeReferences(tree.Root, blockIDs)
var attrViewIDs []string
// 绑定镜像数据库