Skip to content

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

:bug: Preserve database rich text formatting in exports https://github.com/siyuan-note/siyuan/issues/19188

Daniela46c7a243878
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 6d8a55fac1..80eab01aff 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -3651,6 +3651,7 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
unlinks = nil
// Attribute View export https://github.com/siyuan-note/siyuan/issues/8710
+ var attrViewRichErr error
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
@@ -3775,6 +3776,12 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
}
} else if av.KeyTypeText == cell.Value.Type {
if nil != cell.Value.Text {
+ if richTableCells && cell.Value.Text.IsRich() {
+ if attrViewRichErr = appendAttributeViewRichTextExport(mdTableCell, cell.Value.Text); attrViewRichErr != nil {
+ return ast.WalkStop
+ }
+ continue
+ }
val = cell.Value.Text.Content
if !wysiwyg {
val = string(lex.EscapeProtyleMarkers([]byte(val)))
@@ -3942,6 +3949,15 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
if nil == v {
continue
}
+ if richTableCells && av.KeyTypeText == v.Type && v.Text.IsRich() {
+ if attrViewRichErr = appendAttributeViewRichTextExport(mdTableCell, v.Text); attrViewRichErr != nil {
+ return ast.WalkStop
+ }
+ if i < len(cell.Value.Rollup.Contents)-1 {
+ mdTableCell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(", ")})
+ }
+ continue
+ }
if av.KeyTypeBlock == v.Type {
if nil != v.Block {
@@ -4006,6 +4022,9 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
unlinks = append(unlinks, n)
return ast.WalkContinue
})
+ if attrViewRichErr != nil {
+ return nil, attrViewRichErr
+ }
for _, n := range unlinks {
n.Unlink()
}
diff --git a/kernel/model/table_cell_rich_export.go b/kernel/model/table_cell_rich_export.go
index 6903496739..ece508eb5f 100644
--- a/kernel/model/table_cell_rich_export.go
+++ b/kernel/model/table_cell_rich_export.go
@@ -4,9 +4,37 @@ import (
"github.com/88250/lute"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/treenode"
)
+// appendAttributeViewRichTextExport 将数据库富文本源作为单元格子块导出,保留列表和行级样式。
+func appendAttributeViewRichTextExport(cell *ast.Node, value *av.ValueText) error {
+ fragment, err := av.ParseValueTextRich(value.Rich)
+ if err != nil {
+ return err
+ }
+ var unlinks []*ast.Node
+ ast.Walk(fragment.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering {
+ node.ID = ""
+ node.RemoveIALAttr("id")
+ node.RemoveIALAttr("updated")
+ if node.Type == ast.NodeKramdownBlockIAL {
+ unlinks = append(unlinks, node)
+ }
+ }
+ return ast.WalkContinue
+ })
+ for _, node := range unlinks {
+ node.Unlink()
+ }
+ for fragment.Root.FirstChild != nil {
+ cell.AppendChild(fragment.Root.FirstChild)
+ }
+ return nil
+}
+
// normalizeExportPreviewTree 保留包含块级内容的单元格,避免 Markdown 往返将其拆到表格外。
func normalizeExportPreviewTree(tree *parse.Tree, luteEngine *lute.Lute) *parse.Tree {
tables := map[*ast.Node]struct{}{}
diff --git a/kernel/model/table_cell_rich_test.go b/kernel/model/table_cell_rich_test.go
index a4b7d85d27..e4bead1802 100644
--- a/kernel/model/table_cell_rich_test.go
+++ b/kernel/model/table_cell_rich_test.go
@@ -9,6 +9,7 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
+ "github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/treenode"
@@ -176,6 +177,44 @@ func TestTableCellRichPreviewNormalization(t *testing.T) {
}
}
+func TestAttributeViewRichTextExport(t *testing.T) {
+ luteEngine := util.NewLute()
+ tree := parse.Parse("", []byte("| Key | Text |\n| --- | --- |\n| 111 | |"), luteEngine.ParseOptions)
+ cell := tree.Root.FirstChild.LastChild.LastChild
+ value := &av.ValueText{Content: "foo\n123\n123", Rich: &av.ValueTextRich{
+ Spec: av.ValueTextRichSpec, Format: av.ValueTextRichFormatKramdown,
+ Content: "**foo**\n\n- 123\n - 123",
+ }}
+ if err := appendAttributeViewRichTextExport(cell, value); err != nil {
+ t.Fatal(err)
+ }
+ preview := normalizeExportPreviewTree(tree, luteEngine)
+ for name, output := range map[string]string{
+ "preview": luteEngine.ProtylePreview(preview, luteEngine.RenderOptions, luteEngine.ParseOptions),
+ "html": string(render.NewProtyleExportRenderer(preview, luteEngine.RenderOptions, luteEngine.ParseOptions).Render()),
+ "word": string(render.NewProtyleExportDocxRenderer(preview, luteEngine.RenderOptions, luteEngine.ParseOptions).Render()),
+ } {
+ t.Run(name, func(t *testing.T) {
+ listMarker, boldMarker := "<ul", "<strong"
+ if name == "preview" {
+ boldMarker = `data-type="strong"`
+ }
+ if name == "html" {
+ listMarker, boldMarker = `data-type="NodeList"`, `data-type="strong"`
+ }
+ if strings.Count(output, listMarker) != 2 || !strings.Contains(output, boldMarker) ||
+ !strings.Contains(output, "foo") || strings.Count(output, "123") != 2 {
+ t.Fatalf("database rich text lost nested lists or formatting: %s", output)
+ }
+ })
+ }
+ invalid := &av.ValueText{Rich: &av.ValueTextRich{Spec: 99, Format: av.ValueTextRichFormatKramdown}}
+ empty := &ast.Node{Type: ast.NodeTableCell}
+ if err := appendAttributeViewRichTextExport(empty, invalid); err == nil || empty.FirstChild != nil {
+ t.Fatal("invalid rich text must return an error without populating the cell")
+ }
+}
+
func TestTableCellRichFootnoteExport(t *testing.T) {
fixture, _, _, _ := prepareAssetDownloadDocumentTest(t)
luteEngine := util.NewLute()