Skip to content

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

:bug: Preserve rich table cell structure in export previews https://github.com/siyuan-note/siyuan/issues/19188

Daniel3b5391e35764
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 8b695fc08f..6d8a55fac1 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -1075,8 +1075,7 @@ func ExportPreview(id string, fillCSSVar bool, accessChecker ...EmbedBlockAccess
return ast.WalkContinue
})
- md := treenode.FormatNode(tree.Root, luteEngine)
- tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
+ tree = normalizeExportPreviewTree(tree, luteEngine)
// 使用实际主题样式值替换样式变量 Use real theme style value replace var in preview mode https://github.com/siyuan-note/siyuan/issues/11458
if fillCSSVar {
fillThemeStyleVar(tree)
diff --git a/kernel/model/table_cell_rich_export.go b/kernel/model/table_cell_rich_export.go
new file mode 100644
index 0000000000..6903496739
--- /dev/null
+++ b/kernel/model/table_cell_rich_export.go
@@ -0,0 +1,55 @@
+package model
+
+import (
+ "github.com/88250/lute"
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+)
+
+// normalizeExportPreviewTree 保留包含块级内容的单元格,避免 Markdown 往返将其拆到表格外。
+func normalizeExportPreviewTree(tree *parse.Tree, luteEngine *lute.Lute) *parse.Tree {
+ tables := map[*ast.Node]struct{}{}
+ ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if !entering || node.Type != ast.NodeTableCell {
+ return ast.WalkContinue
+ }
+ for child := node.FirstChild; child != nil; child = child.Next {
+ if child.IsBlock() {
+ table := node.Parent
+ for table != nil && table.Type != ast.NodeTable {
+ table = table.Parent
+ }
+ if table != nil {
+ tables[table] = struct{}{}
+ }
+ break
+ }
+ }
+ return ast.WalkContinue
+ })
+ // 使用独立占位节点让其他导出内容继续经过原有格式化流程。
+ replacements := map[string]*ast.Node{}
+ for table := range tables {
+ marker := "table-cell-preview-" + ast.NewNodeID()
+ replacements[marker] = table
+ placeholder := &ast.Node{Type: ast.NodeParagraph}
+ placeholder.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(marker)})
+ table.InsertBefore(placeholder)
+ table.Unlink()
+ }
+ md := treenode.FormatNode(tree.Root, luteEngine)
+ ret := parse.Parse("", []byte(md), luteEngine.ParseOptions)
+ var placeholders []*ast.Node
+ ast.Walk(ret.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && node.Type == ast.NodeParagraph && replacements[node.Text()] != nil {
+ placeholders = append(placeholders, node)
+ }
+ return ast.WalkContinue
+ })
+ for _, placeholder := range placeholders {
+ placeholder.InsertBefore(replacements[placeholder.Text()])
+ placeholder.Unlink()
+ }
+ return ret
+}
diff --git a/kernel/model/table_cell_rich_test.go b/kernel/model/table_cell_rich_test.go
index 1e7964e19e..a4b7d85d27 100644
--- a/kernel/model/table_cell_rich_test.go
+++ b/kernel/model/table_cell_rich_test.go
@@ -122,6 +122,60 @@ func TestTableCellRichHTMLAndMarkdownExports(t *testing.T) {
}
}
+func TestTableCellRichPreviewNormalization(t *testing.T) {
+ luteEngine := util.NewLute()
+ tree := parse.Parse("", []byte("| 1 | 2 | 3 |\n| --- | --- | --- |\n| 123 | text | next |\n\n| Key | Text |\n| --- | --- |\n| 111 | value |"), luteEngine.ParseOptions)
+ table := tree.Root.FirstChild
+ var cell *ast.Node
+ index := 0
+ ast.Walk(table, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && node.Type == ast.NodeTableCell {
+ if index == 2 {
+ cell = node
+ }
+ index++
+ }
+ return ast.WalkContinue
+ })
+ cell.TableCellRich = &ast.TableCellRich{Spec: 1, Format: "kramdown", Content: "3\n\n## 4\n\n5"}
+ var lastTable *ast.Node
+ ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && node.Type == ast.NodeTable {
+ lastTable = node
+ }
+ return ast.WalkContinue
+ })
+ second := lastTable.LastChild.LastChild
+ second.TableCellRich = &ast.TableCellRich{Spec: 1, Format: "kramdown", Content: "- first\n- second"}
+ if err := treenode.MaterializeTableCellRichExport(tree.Root); err != nil {
+ t.Fatal(err)
+ }
+ previewTree := normalizeExportPreviewTree(tree, luteEngine)
+ tableCount := 0
+ ast.Walk(previewTree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && node.Type == ast.NodeTable {
+ tableCount++
+ }
+ if entering && node.Type == ast.NodeHeading && node.Parent.Type != ast.NodeTableCell {
+ t.Fatal("cell heading escaped the table")
+ }
+ return ast.WalkContinue
+ })
+ if tableCount != 2 || table.Parent != previewTree.Root || cell.ChildByType(ast.NodeHeading) == nil ||
+ second.ChildByType(ast.NodeList) == nil {
+ t.Fatalf("preview normalization changed rich table structure: tables=%d parent=%v heading=%v list=%v html=%s", tableCount, table.Parent == previewTree.Root, cell.ChildByType(ast.NodeHeading) != nil, second.ChildByType(ast.NodeList) != nil, luteEngine.ProtylePreview(previewTree, luteEngine.RenderOptions, luteEngine.ParseOptions))
+ }
+ html := luteEngine.ProtylePreview(previewTree, luteEngine.RenderOptions, luteEngine.ParseOptions)
+ for _, expected := range []string{"<table", "<h2", "<ul", "first", "second", "123"} {
+ if !strings.Contains(html, expected) {
+ t.Fatalf("preview missing %q: %s", expected, html)
+ }
+ }
+ if strings.Contains(html, "table-cell-preview-") || strings.Contains(html, "## 4") {
+ t.Fatalf("preview leaked normalization markers: %s", html)
+ }
+}
+
func TestTableCellRichFootnoteExport(t *testing.T) {
fixture, _, _, _ := prepareAssetDownloadDocumentTest(t)
luteEngine := util.NewLute()