An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve content preservation when converting headings to documents https://github.com/siyuan-note/siyuan/issues/19235
Daniel5c5af8aeed06
diff --git a/kernel/model/heading.go b/kernel/model/heading.goindex 098d57af07..8c1edf2e90 100644--- a/kernel/model/heading.go+++ b/kernel/model/heading.go@@ -428,6 +428,9 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string, toTluteEngine := util.NewLute()newTree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument, ID: srcHeadingID}, Context: &parse.Context{ParseOption: luteEngine.ParseOptions}}+ if paragraph := headingDocParagraph(headingNode, headingText); nil != paragraph {+ newTree.Root.AppendChild(paragraph)+ }for _, c := range children {newTree.Root.AppendChild(c)}@@ -478,3 +481,31 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string, toT}()return}++// headingDocParagraph 在文档名无法完整表达标题内容时,将原标题的行内节点移入新段落。+func headingDocParagraph(heading *ast.Node, title string) *ast.Node {+ var text strings.Builder+ preserve := false+ for c := heading.FirstChild; nil != c; c = c.Next {+ if ast.NodeHeadingC8hMarker == c.Type || ast.NodeHeadingID == c.Type {+ continue+ }+ if ast.NodeText != c.Type {+ preserve = true+ }+ text.Write(c.Tokens)+ }+ if !preserve && util.EscapeHTML(util.UnescapeHTML(text.String())) == title {+ return nil+ }++ paragraph := treenode.NewParagraph("")+ for c := heading.FirstChild; nil != c; {+ next := c.Next+ if ast.NodeHeadingC8hMarker != c.Type && ast.NodeHeadingID != c.Type {+ paragraph.AppendChild(c)+ }+ c = next+ }+ return paragraph+}diff --git a/kernel/model/heading_doc_test.go b/kernel/model/heading_doc_test.gonew file mode 100644index 0000000000..0554034b59--- /dev/null+++ b/kernel/model/heading_doc_test.go@@ -0,0 +1,122 @@+// 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.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package model++import (+ "encoding/json"+ "strings"+ "testing"++ "github.com/88250/lute"+ "github.com/88250/lute/ast"+ "github.com/88250/lute/parse"+ "github.com/siyuan-note/siyuan/kernel/treenode"+)++func TestHeadingDocParagraph(t *testing.T) {+ for _, tc := range []struct {+ name string+ markdown string+ title string+ preserve bool+ }{+ {"plain", "Test", "Test", false},+ {"escaped text", "A & B", "A & B", false},+ {"slash", "A/B", "A_B", true},+ {"truncated", strings.Repeat("标题", 20), "标题...", true},+ {"image", "Test ", "Test image", true},+ {"image only", "", "image", true},+ {"link", "[Test](https://example.com)", "Test", true},+ {"mixed", "Before  [link](https://example.com) after", "Before image link after", true},+ {"strong", "**Test**", "Test", true},+ {"code", "`Test`", "Test", true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ engine := lute.New()+ tree := parse.Parse("", []byte("# "+tc.markdown+"\n\n123\n\n## Child\n\nBody\n\n# Next\n"), engine.ParseOptions)+ heading := tree.Root.FirstChild+ heading.ID = "20260909120000-heading"+ heading.SetIALAttr("id", heading.ID)+ var inlines []*ast.Node+ var before [][]byte+ for c := heading.FirstChild; c != nil; c = c.Next {+ if c.Type == ast.NodeHeadingC8hMarker || c.Type == ast.NodeHeadingID {+ continue+ }+ inlines = append(inlines, c)+ data, err := json.Marshal(c)+ if err != nil {+ t.Fatal(err)+ }+ before = append(before, data)+ }+ children := treenode.HeadingChildren(heading)+ paragraph := headingDocParagraph(heading, tc.title)+ if (paragraph != nil) != tc.preserve {+ t.Fatalf("preserve = %v, expected %v", paragraph != nil, tc.preserve)+ }+ if paragraph == nil {+ if heading.ChildByType(ast.NodeText) != inlines[0] {+ t.Fatal("plain heading content changed")+ }+ return+ }+ if paragraph.Type != ast.NodeParagraph || paragraph.ID == "" || paragraph.ID == heading.ID || paragraph.IALAttr("id") != paragraph.ID {+ t.Fatal("expected a paragraph with its own block ID")+ }+ i := 0+ for c := paragraph.FirstChild; c != nil; c = c.Next {+ if i >= len(inlines) || c != inlines[i] || c.Parent != paragraph {+ t.Fatal("inline order or parent changed")+ }+ data, err := json.Marshal(c)+ if err != nil || string(data) != string(before[i]) {+ t.Fatalf("inline content changed: %s, error: %v", data, err)+ }+ i+++ }+ if i != len(inlines) {+ t.Fatal("inline content lost")+ }+ if heading.ID != "20260909120000-heading" || heading.IALAttr("id") != heading.ID {+ t.Fatal("source heading ID changed")+ }+ after := treenode.HeadingChildren(heading)+ if len(after) != len(children) || len(after) != 3 {+ t.Fatal("heading section changed")+ }+ for i, child := range children {+ if after[i] != child {+ t.Fatal("section order changed")+ }+ }+ if children[1].HeadingLevel != 2 {+ t.Fatal("child heading level changed")+ }+ })+ }+}++func TestHeadingDocParagraphTextMark(t *testing.T) {+ heading := &ast.Node{Type: ast.NodeHeading}+ link := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "a", TextMarkTextContent: "Test", TextMarkAHref: "https://example.com"}+ heading.AppendChild(link)+ paragraph := headingDocParagraph(heading, "Test")+ if paragraph == nil || paragraph.FirstChild != link || link.TextMarkAHref != "https://example.com" {+ t.Fatal("text mark link lost")+ }+}