Skip to content

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

:bug: Repair invalid list children when loading trees

Daniel186d04431fc5
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index 3e7492c902..9275f9bcba 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -579,6 +579,9 @@ func fixTreeJSONData(boxID, p string, jsonData []byte, luteEngine *lute.Lute, de
ret.ID = pathID
ret.Root.SetIALAttr("id", ret.ID)
}
+ if treenode.FixInvalidListChildren(ret.Root) {
+ needFix = true
+ }
if !needFix {
return jsonData, false, nil
diff --git a/kernel/filesys/tree_fix_test.go b/kernel/filesys/tree_fix_test.go
new file mode 100644
index 0000000000..02826c0a66
--- /dev/null
+++ b/kernel/filesys/tree_fix_test.go
@@ -0,0 +1,80 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+package filesys
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/render"
+ "github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestLoadTreeWithFixRepairsInvalidListChildren(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ defer func() {
+ cache.ClearTreeCache()
+ util.DataDir = originalDataDir
+ }()
+
+ boxID := "20260814000000-box0001"
+ rootID := "20260814000000-root001"
+ treePath := "/" + rootID + ".sy"
+ luteEngine := util.NewLute()
+ tree := treenode.NewTree(boxID, treePath, "/Test", "Test")
+ paragraph := tree.Root.FirstChild
+ paragraph.Unlink()
+ list := &ast.Node{Type: ast.NodeList, ID: "20260814000000-list001", ListData: &ast.ListData{Typ: 1}}
+ list.SetIALAttr("id", list.ID)
+ list.AppendChild(paragraph)
+ tree.Root.AppendChild(list)
+
+ data := render.NewJSONRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions).Render()
+ absPath := filepath.Join(util.DataDir, boxID, treePath)
+ if err := os.MkdirAll(filepath.Dir(absPath), 0755); nil != err {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(absPath, data, 0600); nil != err {
+ t.Fatal(err)
+ }
+
+ loaded, needFix, err := LoadTreeWithFix(boxID, treePath, luteEngine)
+ if nil != err {
+ t.Fatal(err)
+ }
+ if !needFix {
+ t.Fatal("expected invalid list structure to be fixed")
+ }
+ requireFixedListChild(t, loaded.Root.FirstChild, paragraph.ID)
+
+ cache.RemoveTreeDataInBox(rootID, boxID)
+ reloaded, needFix, err := LoadTreeWithFix(boxID, treePath, luteEngine)
+ if nil != err {
+ t.Fatal(err)
+ }
+ if needFix {
+ t.Fatal("expected repaired list structure to persist")
+ }
+ requireFixedListChild(t, reloaded.Root.FirstChild, paragraph.ID)
+}
+
+func requireFixedListChild(t *testing.T, list *ast.Node, paragraphID string) {
+ t.Helper()
+ if nil == list || ast.NodeList != list.Type || nil == list.FirstChild || ast.NodeListItem != list.FirstChild.Type {
+ t.Fatal("expected a list item wrapper")
+ }
+ if nil == list.FirstChild.FirstChild || paragraphID != list.FirstChild.FirstChild.ID {
+ t.Fatal("expected the original content block inside the list item")
+ }
+}
diff --git a/kernel/treenode/block_structure.go b/kernel/treenode/block_structure.go
index 2692e2a17d..4721375ced 100644
--- a/kernel/treenode/block_structure.go
+++ b/kernel/treenode/block_structure.go
@@ -18,6 +18,7 @@ package treenode
import (
"fmt"
+ "strconv"
"github.com/88250/lute/ast"
)
@@ -91,6 +92,142 @@ func ValidateBlockReplacement(oldNode, newNode *ast.Node) error {
return ValidateBlockSubtree(newNode)
}
+// FixInvalidListChildren 将列表下直属的非列表项内容块包装为列表项。
+func FixInvalidListChildren(root *ast.Node) (fixed bool) {
+ if nil == root {
+ return
+ }
+
+ var invalidChildren []*ast.Node
+ ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && nil != n.Parent && ast.NodeList == n.Parent.Type &&
+ ast.NodeListItem != n.Type && isContentBlock(n) {
+ invalidChildren = append(invalidChildren, n)
+ }
+ return ast.WalkContinue
+ })
+
+ fixedLists := map[*ast.Node]struct{}{}
+ for _, child := range invalidChildren {
+ list := child.Parent
+ itemID := ast.NewNodeID()
+ item := &ast.Node{
+ Type: ast.NodeListItem,
+ ID: itemID,
+ ListData: newListItemData(list, child),
+ }
+ item.SetIALAttr("id", itemID)
+ item.SetIALAttr("updated", itemID[:14])
+ var taskMarker *ast.Node
+ if 3 == item.ListData.Typ {
+ taskMarker = takeTaskListItemMarker(child)
+ }
+ child.InsertBefore(item)
+ child.Unlink()
+ if nil != taskMarker {
+ item.AppendChild(taskMarker)
+ }
+ item.AppendChild(child)
+ fixedLists[list] = struct{}{}
+ fixed = true
+ }
+ for list := range fixedLists {
+ normalizeOrderedListItems(list)
+ }
+ return
+}
+
+func newListItemData(list, child *ast.Node) (ret *ast.ListData) {
+ listType := 0
+ if nil != list.ListData {
+ listType = list.ListData.Typ
+ }
+
+ for sibling := child.Previous; nil != sibling; sibling = sibling.Previous {
+ if ast.NodeListItem == sibling.Type && nil != sibling.ListData {
+ ret = cloneListData(sibling.ListData)
+ ret.Typ = listType
+ ret.Checked = false
+ return
+ }
+ }
+ for sibling := child.Next; nil != sibling; sibling = sibling.Next {
+ if ast.NodeListItem == sibling.Type && nil != sibling.ListData {
+ ret = cloneListData(sibling.ListData)
+ ret.Typ = listType
+ ret.Checked = false
+ return
+ }
+ }
+
+ ret = &ast.ListData{Typ: listType}
+ if 0 == listType || 3 == listType {
+ ret.BulletChar = '*'
+ ret.Marker = []byte{'*'}
+ }
+ return
+}
+
+func cloneListData(data *ast.ListData) (ret *ast.ListData) {
+ ret = &ast.ListData{}
+ *ret = *data
+ ret.Marker = append([]byte(nil), data.Marker...)
+ return
+}
+
+func takeTaskListItemMarker(child *ast.Node) *ast.Node {
+ for marker := child.FirstChild; nil != marker; marker = marker.Next {
+ if ast.NodeTaskListItemMarker == marker.Type {
+ marker.Unlink()
+ return marker
+ }
+ }
+ if nil != child.Previous && ast.NodeTaskListItemMarker == child.Previous.Type {
+ marker := child.Previous
+ marker.Unlink()
+ return marker
+ }
+ return &ast.Node{Type: ast.NodeTaskListItemMarker}
+}
+
+func normalizeOrderedListItems(list *ast.Node) {
+ if nil == list.ListData || 1 != list.ListData.Typ {
+ return
+ }
+
+ start, delimiter, position := 1, byte('.'), 0
+ for item := list.FirstChild; nil != item; item = item.Next {
+ if ast.NodeListItem != item.Type {
+ continue
+ }
+ if nil != item.ListData {
+ if 0 != item.ListData.Delimiter {
+ delimiter = item.ListData.Delimiter
+ }
+ if 0 < item.ListData.Num || 0 != item.ListData.Delimiter || 0 < len(item.ListData.Marker) {
+ start = max(0, item.ListData.Num-position)
+ break
+ }
+ }
+ position++
+ }
+
+ num := start
+ for item := list.FirstChild; nil != item; item = item.Next {
+ if ast.NodeListItem != item.Type {
+ continue
+ }
+ if nil == item.ListData {
+ item.ListData = &ast.ListData{}
+ }
+ item.ListData.Typ = 1
+ item.ListData.Num = num
+ item.ListData.Delimiter = delimiter
+ item.ListData.Marker = []byte(strconv.Itoa(num) + string(delimiter))
+ num++
+ }
+}
+
func isContentBlock(node *ast.Node) bool {
return nil != node && node.IsBlock() && ast.NodeKramdownBlockIAL != node.Type
}
diff --git a/kernel/treenode/block_structure_test.go b/kernel/treenode/block_structure_test.go
index 6bb63bac54..b191fe34d7 100644
--- a/kernel/treenode/block_structure_test.go
+++ b/kernel/treenode/block_structure_test.go
@@ -113,3 +113,103 @@ func TestValidateBlockReplacement(t *testing.T) {
t.Fatal("expected paragraph replacing a list item to be rejected")
}
}
+
+func TestFixInvalidListChildren(t *testing.T) {
+ document := &ast.Node{Type: ast.NodeDocument, ID: "document"}
+ list := &ast.Node{Type: ast.NodeList, ID: "list", ListData: &ast.ListData{Typ: 1, Start: 4}}
+ firstItem := &ast.Node{Type: ast.NodeListItem, ID: "first-item", ListData: &ast.ListData{
+ Typ: 1, Delimiter: '.', Marker: []byte("4."), Num: 4,
+ }}
+ paragraph := &ast.Node{Type: ast.NodeParagraph, ID: "paragraph"}
+ heading := &ast.Node{Type: ast.NodeHeading, ID: "heading"}
+ lastItem := &ast.Node{Type: ast.NodeListItem, ID: "last-item", ListData: &ast.ListData{
+ Typ: 1, Delimiter: '.', Marker: []byte("5."), Num: 5,
+ }}
+ document.AppendChild(list)
+ list.AppendChild(firstItem)
+ list.AppendChild(paragraph)
+ list.AppendChild(heading)
+ list.AppendChild(lastItem)
+
+ if !FixInvalidListChildren(document) {
+ t.Fatal("expected invalid list children to be fixed")
+ }
+ paragraphItem := firstItem.Next
+ if nil == paragraphItem || ast.NodeListItem != paragraphItem.Type {
+ t.Fatal("expected paragraph to be wrapped by a list item")
+ }
+ headingItem := paragraphItem.Next
+ if nil == headingItem || ast.NodeListItem != headingItem.Type {
+ t.Fatal("expected heading to be wrapped by a list item")
+ }
+ if paragraph != paragraphItem.FirstChild || heading != headingItem.FirstChild || lastItem != headingItem.Next {
+ t.Fatal("expected content and sibling order to be preserved")
+ }
+ if nil == paragraphItem.ListData || list.ListData.Typ != paragraphItem.ListData.Typ {
+ t.Fatal("expected wrapped item to inherit the list type")
+ }
+ if 5 != paragraphItem.ListData.Num || 6 != headingItem.ListData.Num || 7 != lastItem.ListData.Num {
+ t.Fatal("expected ordered list numbers to include the wrapped items")
+ }
+ if "5." != string(paragraphItem.ListData.Marker) || "6." != string(headingItem.ListData.Marker) ||
+ "7." != string(lastItem.ListData.Marker) {
+ t.Fatal("expected ordered list markers to include the wrapped items")
+ }
+ if "" == paragraphItem.ID || paragraphItem.ID != paragraphItem.IALAttr("id") {
+ t.Fatal("expected wrapped item to have a persisted block ID")
+ }
+ if err := ValidateBlockSubtree(list); nil != err {
+ t.Fatalf("expected repaired list structure to be valid, got [%s]", err)
+ }
+ if FixInvalidListChildren(document) {
+ t.Fatal("expected a second repair pass to be idempotent")
+ }
+}
+
+func TestFixInvalidTaskListChild(t *testing.T) {
+ list := &ast.Node{Type: ast.NodeList, ID: "list", ListData: &ast.ListData{Typ: 3}}
+ marker := &ast.Node{Type: ast.NodeTaskListItemMarker, TaskListItemChecked: true}
+ heading := &ast.Node{Type: ast.NodeHeading, ID: "heading"}
+ list.AppendChild(marker)
+ list.AppendChild(heading)
+
+ if !FixInvalidListChildren(list) {
+ t.Fatal("expected invalid task list child to be fixed")
+ }
+ item := list.FirstChild
+ if nil == item || nil == item.ListData || 3 != item.ListData.Typ {
+ t.Fatal("expected a task list item wrapper")
+ }
+ if marker != item.FirstChild || heading != item.LastChild || !marker.TaskListItemChecked {
+ t.Fatal("expected the existing task marker before the content block")
+ }
+ if '*' != item.ListData.BulletChar || "*" != string(item.ListData.Marker) {
+ t.Fatal("expected the task item to have a list marker")
+ }
+}
+
+func TestFixInvalidOrderedListStartingAtZero(t *testing.T) {
+ list := &ast.Node{Type: ast.NodeList, ID: "list", ListData: &ast.ListData{Typ: 1}}
+ firstItem := &ast.Node{Type: ast.NodeListItem, ID: "first-item", ListData: &ast.ListData{
+ Typ: 1, Delimiter: '.', Marker: []byte("0."), Num: 0,
+ }}
+ paragraph := &ast.Node{Type: ast.NodeParagraph, ID: "paragraph"}
+ lastItem := &ast.Node{Type: ast.NodeListItem, ID: "last-item", ListData: &ast.ListData{
+ Typ: 1, Delimiter: '.', Marker: []byte("1."), Num: 1,
+ }}
+ list.AppendChild(firstItem)
+ list.AppendChild(paragraph)
+ list.AppendChild(lastItem)
+
+ if !FixInvalidListChildren(list) {
+ t.Fatal("expected invalid ordered list child to be fixed")
+ }
+ wrappedItem := firstItem.Next
+ if 0 != firstItem.ListData.Num || 1 != wrappedItem.ListData.Num || 2 != lastItem.ListData.Num {
+ t.Fatal("expected ordered list numbering to preserve a zero start")
+ }
+ if "0." != string(firstItem.ListData.Marker) || "1." != string(wrappedItem.ListData.Marker) ||
+ "2." != string(lastItem.ListData.Marker) {
+ t.Fatal("expected ordered list markers to preserve a zero start")
+ }
+}