Skip to content

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

:bug: Preserve descendant block IDs on external block updates https://github.com/siyuan-note/siyuan/issues/18544

Daniel3719a1636135
diff --git a/kernel/model/block_update.go b/kernel/model/block_update.go
index 3708b1c3d5..ea21bfe298 100644
--- a/kernel/model/block_update.go
+++ b/kernel/model/block_update.go
@@ -158,6 +158,7 @@ func buildBlockUpdateOperations(inputs []BlockUpdateInput, resolveTree blockUpda
}
updatedNode.SetIALAttr("id", input.ID)
+ pinDescendantBlockIDs(oldNode, updatedNode)
data = luteEngine.Tree2BlockDOM(normalizedTree, luteEngine.RenderOptions, luteEngine.ParseOptions)
operations = append(operations, &Operation{
Action: "update",
@@ -252,6 +253,50 @@ func resolveBlockUpdateNode(oldNode, root *ast.Node) (updatedNode *ast.Node, err
return
}
+// pinDescendantBlockIDs 把旧块子树中对应位置的子块 ID 钉回新块,避免更新容器块时 Lute 重新生成
+// 子块 ID,导致指向子块的块引用、反链、闪卡等失效。
+// 匹配规则:按同级内容块顺序对齐,类型一致才沿用旧 ID;类型不一致时向后查找同类型的旧子块重新
+// 对齐,这样插入或删除子块后其余子块仍能匹配上旧 ID,新增的子块保持新生成的 ID。
+func pinDescendantBlockIDs(oldNode, updatedNode *ast.Node) {
+ oldChildren := blockChildrenOf(oldNode)
+ oldIndex := 0
+ for _, newChild := range blockChildrenOf(updatedNode) {
+ if oldIndex >= len(oldChildren) {
+ break
+ }
+ if oldChildren[oldIndex].Type != newChild.Type {
+ if aligned := alignBlockUpdateChild(oldChildren, oldIndex, newChild.Type); 0 > aligned {
+ continue
+ } else {
+ oldIndex = aligned
+ }
+ }
+ newChild.SetIALAttr("id", oldChildren[oldIndex].ID)
+ pinDescendantBlockIDs(oldChildren[oldIndex], newChild)
+ oldIndex++
+ }
+}
+
+// alignBlockUpdateChild 从 start 起向后查找第一个指定类型的旧子块,返回其下标;找不到返回 -1。
+func alignBlockUpdateChild(oldChildren []*ast.Node, start int, childType ast.NodeType) int {
+ for i := start; i < len(oldChildren); i++ {
+ if oldChildren[i].Type == childType {
+ return i
+ }
+ }
+ return -1
+}
+
+// blockChildrenOf 返回节点的直接内容块子节点,不含行级块 IAL。
+func blockChildrenOf(parent *ast.Node) (ret []*ast.Node) {
+ for child := parent.FirstChild; nil != child; child = child.Next {
+ if child.IsBlock() && ast.NodeKramdownBlockIAL != child.Type {
+ ret = append(ret, child)
+ }
+ }
+ return
+}
+
func firstContentBlock(parent *ast.Node) *ast.Node {
if nil == parent {
return nil
diff --git a/kernel/model/block_update_test.go b/kernel/model/block_update_test.go
index c7af1ecfb9..9ecc0e1584 100644
--- a/kernel/model/block_update_test.go
+++ b/kernel/model/block_update_test.go
@@ -170,3 +170,122 @@ func TestDataBlockDOMEmptyData(t *testing.T) {
t.Fatal("empty markdown should produce a blank paragraph")
}
}
+
+const (
+ updateTestBoxID = "20260804000000-box0001"
+ updateTestRoot = "20260804000001-root001"
+ updateTestList = "20260804000002-list001"
+ updateTestItem1 = "20260804000003-item001"
+ updateTestPara1 = "20260804000004-para001"
+ updateTestItem2 = "20260804000005-item002"
+ updateTestPara2 = "20260804000006-para002"
+)
+
+func TestBuildBlockUpdateOperationsPinsDescendantIDs(t *testing.T) {
+ testCases := []struct {
+ name string
+ id string
+ data string
+ wantKeep map[string]bool
+ wantAll bool
+ }{
+ {
+ name: "structure preserved on text edit",
+ id: updateTestList,
+ data: "- edited a\n- edited b",
+ wantKeep: map[string]bool{
+ updateTestList: true, updateTestItem1: true, updateTestPara1: true,
+ updateTestItem2: true, updateTestPara2: true,
+ },
+ wantAll: true,
+ },
+ {
+ name: "appended item keeps surviving IDs",
+ id: updateTestList,
+ data: "- a\n- b\n- c",
+ wantKeep: map[string]bool{
+ updateTestList: true, updateTestItem1: true, updateTestPara1: true,
+ updateTestItem2: true, updateTestPara2: true,
+ },
+ wantAll: false,
+ },
+ {
+ name: "nested list inserted mid keeps surviving IDs",
+ id: updateTestList,
+ data: "- a\n - a1\n- b",
+ wantKeep: map[string]bool{
+ updateTestList: true, updateTestItem1: true, updateTestPara1: true,
+ updateTestItem2: true, updateTestPara2: true,
+ },
+ wantAll: false,
+ },
+ {
+ name: "list item update pins item and its paragraph",
+ id: updateTestItem1,
+ data: "- edited a",
+ wantKeep: map[string]bool{
+ updateTestItem1: true, updateTestPara1: true,
+ },
+ wantAll: false,
+ },
+ }
+ for _, tc := range testCases {
+ operations, _, err := buildTestBlockUpdateOperations(tc.id, tc.data)
+ if err != nil {
+ t.Fatalf("%s: build block update operations failed: %s", tc.name, err)
+ }
+ if 1 != len(operations) {
+ t.Fatalf("%s: expected one operation, got [%d]", tc.name, len(operations))
+ }
+ data, ok := operations[0].Data.(string)
+ if !ok {
+ t.Fatalf("%s: operation data is not a string", tc.name)
+ }
+ ids := parseBlockUpdateDOMIDs(t, tc.name, data)
+ if tc.wantAll && len(ids) != len(tc.wantKeep) {
+ t.Fatalf("%s: expected [%d] block IDs, got [%d] [%v]", tc.name, len(tc.wantKeep), len(ids), ids)
+ }
+ for wantID := range tc.wantKeep {
+ if _, ok := ids[wantID]; !ok {
+ t.Fatalf("%s: block ID [%s] was not preserved, got [%v]", tc.name, wantID, ids)
+ }
+ }
+ }
+}
+
+func buildTestBlockUpdateOperations(id, data string) ([]*Operation, []string, error) {
+ root := &ast.Node{Type: ast.NodeDocument, ID: updateTestRoot}
+ list := &ast.Node{Type: ast.NodeList, ID: updateTestList}
+ firstItem := &ast.Node{Type: ast.NodeListItem, ID: updateTestItem1}
+ firstItem.AppendChild(&ast.Node{Type: ast.NodeParagraph, ID: updateTestPara1})
+ secondItem := &ast.Node{Type: ast.NodeListItem, ID: updateTestItem2}
+ secondItem.AppendChild(&ast.Node{Type: ast.NodeParagraph, ID: updateTestPara2})
+ list.AppendChild(firstItem)
+ list.AppendChild(secondItem)
+ root.AppendChild(list)
+ tree := &parse.Tree{ID: updateTestRoot, Box: updateTestBoxID, Root: root}
+
+ return buildBlockUpdateOperations([]BlockUpdateInput{{
+ ID: id, Data: data, DataType: "markdown",
+ }}, func(blockID string) *treenode.BlockTree {
+ return &treenode.BlockTree{ID: blockID, RootID: updateTestRoot, BoxID: updateTestBoxID}
+ }, func(blockID string) (*parse.Tree, error) {
+ return tree, nil
+ })
+}
+
+func parseBlockUpdateDOMIDs(t *testing.T, name, data string) map[string]bool {
+ tree := util.NewLute().BlockDOM2Tree(data)
+ if nil == tree || nil == tree.Root {
+ t.Fatalf("%s: parse block DOM failed", name)
+ }
+ ids := map[string]bool{}
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering || !n.IsBlock() || ast.NodeKramdownBlockIAL == n.Type || "" == n.ID {
+ return ast.WalkContinue
+ }
+ ids[n.ID] = true
+ return ast.WalkContinue
+ })
+ return ids
+}