Skip to content

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

:sparkles: Convert tabs to lists in export preview https://github.com/siyuan-note/siyuan/issues/19541

Daniel59a275a28177
diff --git a/kernel/model/table_cell_rich_export.go b/kernel/model/table_cell_rich_export.go
index ece508eb5f..5549ce8bb0 100644
--- a/kernel/model/table_cell_rich_export.go
+++ b/kernel/model/table_cell_rich_export.go
@@ -37,6 +37,7 @@ func appendAttributeViewRichTextExport(cell *ast.Node, value *av.ValueText) erro
// normalizeExportPreviewTree 保留包含块级内容的单元格,避免 Markdown 往返将其拆到表格外。
func normalizeExportPreviewTree(tree *parse.Tree, luteEngine *lute.Lute) *parse.Tree {
+ convertExportPreviewTabs(tree.Root)
tables := map[*ast.Node]struct{}{}
ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
if !entering || node.Type != ast.NodeTableCell {
diff --git a/kernel/model/tabs_export.go b/kernel/model/tabs_export.go
new file mode 100644
index 0000000000..13371ae1d5
--- /dev/null
+++ b/kernel/model/tabs_export.go
@@ -0,0 +1,53 @@
+package model
+
+import (
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+)
+
+// convertExportPreviewTabs 将页签转换为列表,使外部平台保留全部内容和嵌套层级。
+func convertExportPreviewTabs(root *ast.Node) {
+ ast.Walk(root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if !entering || node.Type != ast.NodeTabs {
+ return ast.WalkContinue
+ }
+ tasks := node.IALAttr("tabs-task") == "true"
+ node.Type = ast.NodeList
+ node.ListData = &ast.ListData{Typ: 0, BulletChar: '*', Marker: []byte("*")}
+ for item := node.FirstChild; item != nil; item = item.Next {
+ if item.Type != ast.NodeTabItem {
+ continue
+ }
+ title := item.TabTitleBlock()
+ if title == nil {
+ title = treenode.TabTitleParagraph(item)
+ if title != nil {
+ title.Parent = nil
+ }
+ }
+ if title == nil {
+ title = &ast.Node{Type: ast.NodeParagraph}
+ }
+ title.RemoveIALAttr("tabs-title")
+ item.PrependChild(title)
+ item.Type = ast.NodeListItem
+ item.ListData = &ast.ListData{Typ: 0, BulletChar: '*', Marker: []byte("*")}
+ marker := item.IALAttr("tabs-task")
+ if marker == "" && tasks {
+ marker = " "
+ }
+ if marker != "" {
+ node.ListData.Typ = 3
+ item.ListData.Typ = 3
+ task := &ast.Node{Type: ast.NodeTaskListItemMarker}
+ task.ReviveFromMarker(marker[0])
+ title.PrependChild(task)
+ }
+ item.RemoveIALAttr("tabs-task")
+ }
+ node.RemoveIALAttr("tabs-task")
+ node.RemoveIALAttr("tabs-active-id")
+ node.RemoveIALAttr("tabs-position")
+ return ast.WalkContinue
+ })
+}
diff --git a/kernel/model/tabs_export_test.go b/kernel/model/tabs_export_test.go
new file mode 100644
index 0000000000..b851ed30dd
--- /dev/null
+++ b/kernel/model/tabs_export_test.go
@@ -0,0 +1,45 @@
+package model
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestExportPreviewTabsAsLists(t *testing.T) {
+ l := util.NewLute()
+ for _, marker := range []string{"", " ", "x", "/"} {
+ t.Run("marker="+marker, func(t *testing.T) {
+ attrs := ""
+ if marker != "" {
+ attrs = "{: tabs-task=\"" + marker + "\"}\n"
+ }
+ source := ":::: tabs\n@tab **First**\n" + attrs + "\nFirst body\n\n::: tabs\n@tab Nested\n\nNested body\n:::\n\n@tab Second\n\nSecond body\n::::\n"
+ tree := normalizeExportPreviewTree(parse.Parse("", []byte(source), l.ParseOptions), l)
+ html := l.ProtylePreview(tree, l.RenderOptions, l.ParseOptions)
+ for _, want := range []string{"<ul", "<li", "data-type=\"strong\">First</span>", "First body", "Nested body", "Second body"} {
+ if !strings.Contains(html, want) {
+ t.Fatalf("missing %q: %s", want, html)
+ }
+ }
+ nested := 0
+ ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && node.Type == ast.NodeList && node.ParentIs(ast.NodeListItem) {
+ nested++
+ }
+ return ast.WalkContinue
+ })
+ if strings.Contains(html, "tab-item") || nested != 1 {
+ t.Fatalf("unexpected tab structure: %s", html)
+ }
+ md := treenode.FormatNode(tree.Root, l)
+ if marker != "" && !strings.Contains(strings.ToLower(md), "["+marker+"]") {
+ t.Fatalf("missing task state: %s", md)
+ }
+ })
+ }
+}