An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve mind map blocks when optimizing typography https://github.com/siyuan-note/siyuan/issues/20543
Daniel42ba8cc84d33
diff --git a/kernel/model/format.go b/kernel/model/format.goindex 7fed55cf0f..70ee6dbdac 100644--- a/kernel/model/format.go+++ b/kernel/model/format.go@@ -21,6 +21,7 @@ import ("github.com/88250/lute/ast""github.com/88250/lute/editor"+ "github.com/88250/lute/parse""github.com/88250/lute/render""github.com/siyuan-note/logging""github.com/siyuan-note/siyuan/kernel/treenode"@@ -40,13 +41,28 @@ func AutoSpace(rootID string) (err error) {FlushTxQueue()generateOpTypeHistory(tree, HistoryOpFormat)+ newTree := autoSpaceTree(tree)+ err = writeTreeUpsertQueue(newTree)+ if err != nil {+ return+ }+ invalidateDocumentHistory(newTree.ID)+ logging.LogInfof("formatted tree [%s]", rootID)+ util.RandomSleep(500, 700)+ return+}++func autoSpaceTree(tree *parse.Tree) *parse.Tree {luteEngine := NewLute()+ mindmapTypes := map[string]ast.NodeType{}ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {if !entering {return ast.WalkContinue}switch n.Type {+ case ast.NodeMindmap, ast.NodeMindmapItem:+ mindmapTypes[n.ID] = n.Typecase ast.NodeTextMark:luteEngine.MergeSameTextMark(n) // 合并相邻的同类行级节点case ast.NodeCodeBlockCode:@@ -56,6 +72,17 @@ func AutoSpace(rootID string) (err error) {}return ast.WalkContinue})+ // Markdown 使用列表语法表示脑图,每次解析后按块 ID 恢复原生类型,正文中的普通列表保持不变。+ restoreMindmapTypes := func(tree *parse.Tree) {+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {+ if entering && (n.Type == ast.NodeList || n.Type == ast.NodeListItem) {+ if typ, ok := mindmapTypes[n.ID]; ok {+ n.Type = typ+ }+ }+ return ast.WalkContinue+ })+ }rootIAL := tree.Root.KramdownIALaddBlockIALNodes(tree, false)@@ -64,12 +91,14 @@ func AutoSpace(rootID string) (err error) {formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)md := formatRenderer.Render()newTree := parseKTree(md)+ restoreMindmapTypes(newTree)newTree.Root.Spec = treenode.CurrentSpec// 第二次格式化启用自动空格luteEngine.SetAutoSpace(true)formatRenderer = render.NewFormatRenderer(newTree, luteEngine.RenderOptions, luteEngine.ParseOptions)md = formatRenderer.Render()newTree = parseKTree(md)+ restoreMindmapTypes(newTree)newTree.Root.Spec = treenode.CurrentSpecnewTree.Root.ID = tree.IDnewTree.Root.KramdownIAL = rootIAL@@ -77,12 +106,5 @@ func AutoSpace(rootID string) (err error) {newTree.Path = tree.PathnewTree.HPath = tree.HPathnewTree.Box = tree.Box- err = writeTreeUpsertQueue(newTree)- if err != nil {- return- }- invalidateDocumentHistory(newTree.ID)- logging.LogInfof("formatted tree [%s]", rootID)- util.RandomSleep(500, 700)- return+ return newTree}diff --git a/kernel/model/format_mindmap_test.go b/kernel/model/format_mindmap_test.gonew file mode 100644index 0000000000..c54f34a1ad--- /dev/null+++ b/kernel/model/format_mindmap_test.go@@ -0,0 +1,88 @@+package model++import (+ "strings"+ "testing"++ "github.com/88250/lute/ast"+ "github.com/88250/lute/render"+ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/treenode"+)++func TestAutoSpacePreservesMindmapBlocks(t *testing.T) {+ previousConf := Conf+ Conf = NewAppConf()+ Conf.Editor.Store(conf.NewEditor())+ Conf.Export.Store(conf.NewExport())+ t.Cleanup(func() { Conf = previousConf })+ for _, markdown := range []string{+ "- 中文SiYuan\n - [x] 子节点SiYuan\n\n > 引述SiYuan\n >\n > - 普通列表SiYuan\n\nOutside\n\n- Ordinary list\n",+ "- \n",+ } {+ t.Run(markdown, func(t *testing.T) {+ tree := treenode.NewTree("20261014000000-box0001", "/20261014000000-doc0001.sy", "/Mindmap", "Mindmap")+ engine := NewLute()+ content := engine.BlockDOM2Tree(engine.Md2BlockDOM(markdown, false))+ for content.Root.FirstChild != nil {+ tree.Root.AppendChild(content.Root.FirstChild)+ }+ list := tree.Root.ChildByType(ast.NodeList)+ retagLegacyMindmapList(list)+ metadata := `{"version":2,"rootTitle":"Root & title","viewLocked":true,"nodes":{},"relations":[],"summaries":[],"extension":9007199254740993}`+ list.SetIALAttr(listMindmapMetadataAttr, metadata)+ list.SetIALAttr("style", "height: 480px;")+ list.FirstChild.SetIALAttr("fold", "1")+ listID, itemID := list.ID, list.FirstChild.ID+ types := map[string]ast.NodeType{}+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {+ if entering && n.IsBlock() && n.Type != ast.NodeKramdownBlockIAL {+ types[n.ID] = n.Type+ }+ return ast.WalkContinue+ })+ for pass := 0; pass < 2; pass++ {+ // 已保存的 .sy 树只在属性中保存 IAL,移除 Markdown 解析使用的辅助节点。+ var ialNodes []*ast.Node+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {+ if entering && n.Type == ast.NodeKramdownBlockIAL {+ ialNodes = append(ialNodes, n)+ }+ return ast.WalkContinue+ })+ for _, n := range ialNodes {+ n.Unlink()+ }+ tree = autoSpaceTree(tree)+ found := map[string]ast.NodeType{}+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {+ if !entering || !n.IsBlock() || n.Type == ast.NodeKramdownBlockIAL {+ return ast.WalkContinue+ }+ found[n.ID] = n.Type+ if expected, ok := types[n.ID]; !ok || n.Type != expected {+ t.Errorf("pass %d: block %s changed from %s to %s", pass, n.ID, expected, n.Type)+ }+ if n.ID == listID && (n.IALAttr(listMindmapMetadataAttr) != metadata || n.IALAttr("style") != "height: 480px;") {+ t.Error("mindmap metadata or height changed")+ }+ if n.ID == itemID && n.IALAttr("fold") != "1" {+ t.Error("fold state changed")+ }+ return ast.WalkContinue+ })+ if len(found) != len(types) {+ t.Fatalf("pass %d: block count changed from %d to %d", pass, len(types), len(found))+ }+ if strings.Contains(markdown, "中文") {+ engine := NewLute()+ formatted := string(render.NewFormatRenderer(tree, engine.RenderOptions, engine.ParseOptions).Render())+ if !strings.Contains(formatted, "中文 SiYuan") || !strings.Contains(formatted, "子节点 SiYuan") ||+ !strings.Contains(formatted, "普通列表 SiYuan") || !strings.Contains(formatted, "[X]") {+ t.Fatalf("text or task marker was not formatted: %s", formatted)+ }+ }+ }+ })+ }+}