Skip to content

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

:art: Refine block Kramdown IAL canonicalization https://github.com/siyuan-note/siyuan/issues/18483

Daniel2114078ded0a
diff --git a/kernel/model/block.go b/kernel/model/block.go
index be727227fa..eb286d7fe1 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -1230,8 +1230,7 @@ func GetBlockKramdownsInBox(ids []string, mode, boxID string) (ret map[string]st
}
func getBlockKramdown0(tree *parse.Tree, id, mode string, luteEngine *lute.Lute) (ret string) {
- canonicalizeBlockKramdownIAL(tree)
- addBlockIALNodes(tree, false)
+ addCanonicalBlockIALNodes(tree, false)
node := treenode.GetNodeInTree(tree, id)
if nil == node {
return
@@ -1267,18 +1266,17 @@ var blockKramdownIALAttrPriority = map[string]int{
"fold": 12,
}
-// canonicalizeBlockKramdownIAL 规范化 Kramdown API 导出树中的块级 IAL 属性顺序。
-func canonicalizeBlockKramdownIAL(tree *parse.Tree) {
- ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering || !n.IsBlock() || 2 > len(n.KramdownIAL) {
- return ast.WalkContinue
- }
+// canonicalBlockKramdownIAL 返回按 Kramdown API 输出规则排序的块级 IAL 副本。
+func canonicalBlockKramdownIAL(ial [][]string) (ret [][]string) {
+ ret = slices.Clone(ial)
+ if 2 > len(ret) {
+ return
+ }
- slices.SortStableFunc(n.KramdownIAL, func(a, b []string) int {
- return compareBlockKramdownIALAttrNames(a[0], b[0])
- })
- return ast.WalkContinue
+ slices.SortStableFunc(ret, func(a, b []string) int {
+ return compareBlockKramdownIALAttrNames(a[0], b[0])
})
+ return
}
func compareBlockKramdownIALAttrNames(a, b string) int {
diff --git a/kernel/model/block_kramdown_test.go b/kernel/model/block_kramdown_test.go
index 3ddffcc561..063f390fbf 100644
--- a/kernel/model/block_kramdown_test.go
+++ b/kernel/model/block_kramdown_test.go
@@ -18,37 +18,74 @@ package model
import (
"slices"
+ "strings"
"testing"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
-func TestCanonicalizeBlockKramdownIAL(t *testing.T) {
- root := &ast.Node{Type: ast.NodeDocument, KramdownIAL: testBlockKramdownIAL(
+func TestCanonicalBlockKramdownIAL(t *testing.T) {
+ ial := testBlockKramdownIAL(
"custom-z", "title-img", "fold", "style", "icon", "tags", "bookmark", "memo", "alias", "name", "title",
"type", "updated", "id", "custom-sy-readonly", "custom-riff-decks", "custom-reminder-wechat",
"custom-heading-mode", "custom-avs", "unknown", "heading-fold", "custom-a",
- )}
- paragraph := &ast.Node{Type: ast.NodeParagraph, KramdownIAL: testBlockKramdownIAL(
- "custom-b", "updated", "id", "custom-sy-heading-number", "custom-a",
- )}
- textMark := &ast.Node{Type: ast.NodeTextMark, KramdownIAL: testBlockKramdownIAL("z", "a")}
- paragraph.AppendChild(textMark)
- root.AppendChild(paragraph)
- tree := &parse.Tree{Root: root}
+ )
+ originalNames := blockKramdownIALAttrNames(ial)
- canonicalizeBlockKramdownIAL(tree)
+ canonical := canonicalBlockKramdownIAL(ial)
- assertBlockKramdownIALAttrNames(t, root.KramdownIAL, []string{
+ assertBlockKramdownIALAttrNames(t, canonical, []string{
"id", "updated", "type", "title", "name", "alias", "memo", "bookmark", "tags", "icon", "title-img", "style",
"fold", "custom-avs", "custom-heading-mode", "custom-reminder-wechat", "custom-riff-decks", "custom-sy-readonly",
"custom-a", "custom-z", "heading-fold", "unknown",
})
- assertBlockKramdownIALAttrNames(t, paragraph.KramdownIAL, []string{
- "id", "updated", "custom-sy-heading-number", "custom-a", "custom-b",
- })
- assertBlockKramdownIALAttrNames(t, textMark.KramdownIAL, []string{"z", "a"})
+ assertBlockKramdownIALAttrNames(t, ial, originalNames)
+}
+
+func TestGetBlockKramdownCanonicalBlockIAL(t *testing.T) {
+ for _, mode := range []string{"md", "textmark"} {
+ t.Run(mode, func(t *testing.T) {
+ id := "20260730230437-v4c93el"
+ ial := [][]string{
+ {"custom-z", "z"},
+ {"updated", "20260731125712"},
+ {"custom-sy-readonly", "true"},
+ {"id", id},
+ {"custom-a", "a"},
+ }
+ originalNames := blockKramdownIALAttrNames(ial)
+ root := &ast.Node{Type: ast.NodeDocument}
+ paragraph := &ast.Node{Type: ast.NodeParagraph, ID: id, KramdownIAL: ial}
+ paragraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("content")})
+ root.AppendChild(paragraph)
+ tree := &parse.Tree{Root: root}
+
+ kramdown := getBlockKramdown0(tree, id, mode, util.NewLute())
+
+ expectedIAL := `{: id="20260730230437-v4c93el" updated="20260731125712" ` +
+ `custom-sy-readonly="true" custom-a="a" custom-z="z"}`
+ if !strings.Contains(kramdown, expectedIAL) {
+ t.Fatalf("unexpected Kramdown IAL: %q", kramdown)
+ }
+ assertBlockKramdownIALAttrNames(t, paragraph.KramdownIAL, originalNames)
+ })
+ }
+}
+
+func TestAddCanonicalBlockIALNodesIncludesChildBlocks(t *testing.T) {
+ root := &ast.Node{Type: ast.NodeDocument}
+ blockquote := &ast.Node{Type: ast.NodeBlockquote, KramdownIAL: testBlockKramdownIAL("custom-z", "updated", "id")}
+ paragraph := &ast.Node{Type: ast.NodeParagraph, KramdownIAL: testBlockKramdownIAL("custom-b", "updated", "id", "custom-a")}
+ blockquote.AppendChild(paragraph)
+ root.AppendChild(blockquote)
+ tree := &parse.Tree{Root: root}
+
+ addCanonicalBlockIALNodes(tree, false)
+
+ assertBlockKramdownIALAttrNames(t, parse.Tokens2IAL(blockquote.Next.Tokens), []string{"id", "updated", "custom-z"})
+ assertBlockKramdownIALAttrNames(t, parse.Tokens2IAL(paragraph.Next.Tokens), []string{"id", "updated", "custom-a", "custom-b"})
}
func testBlockKramdownIAL(names ...string) (ret [][]string) {
@@ -58,12 +95,16 @@ func testBlockKramdownIAL(names ...string) (ret [][]string) {
return
}
-func assertBlockKramdownIALAttrNames(t *testing.T, ial [][]string, expected []string) {
- t.Helper()
- var actual []string
+func blockKramdownIALAttrNames(ial [][]string) (ret []string) {
for _, attr := range ial {
- actual = append(actual, attr[0])
+ ret = append(ret, attr[0])
}
+ return
+}
+
+func assertBlockKramdownIALAttrNames(t *testing.T, ial [][]string, expected []string) {
+ t.Helper()
+ actual := blockKramdownIALAttrNames(ial)
if !slices.Equal(actual, expected) {
t.Fatalf("unexpected IAL attribute order: got %v, want %v", actual, expected)
}
diff --git a/kernel/model/template.go b/kernel/model/template.go
index 4c7fc19464..cfccf8945e 100644
--- a/kernel/model/template.go
+++ b/kernel/model/template.go
@@ -592,6 +592,14 @@ func RenderTemplate(p, id string, preview bool) (tree *parse.Tree, dom string, e
}
func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) {
+ addBlockIALNodes0(tree, removeUpdated, false)
+}
+
+func addCanonicalBlockIALNodes(tree *parse.Tree, removeUpdated bool) {
+ addBlockIALNodes0(tree, removeUpdated, true)
+}
+
+func addBlockIALNodes0(tree *parse.Tree, removeUpdated, canonical bool) {
var blocks []*ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || !n.IsBlock() {
@@ -622,7 +630,11 @@ func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) {
return ast.WalkContinue
})
for _, block := range blocks {
- block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)})
+ ial := block.KramdownIAL
+ if canonical {
+ ial = canonicalBlockKramdownIAL(ial)
+ }
+ block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(ial)})
}
}