Skip to content

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

:bug: Stabilize block Kramdown IAL attribute order https://github.com/siyuan-note/siyuan/issues/18483

Danielc98668bf3b59
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 7774d902e7..be727227fa 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -1230,6 +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)
node := treenode.GetNodeInTree(tree, id)
if nil == node {
@@ -1250,6 +1251,65 @@ func getBlockKramdown0(tree *parse.Tree, id, mode string, luteEngine *lute.Lute)
return
}
+var blockKramdownIALAttrPriority = map[string]int{
+ "id": 0,
+ "updated": 1,
+ "type": 2,
+ "title": 3,
+ "name": 4,
+ "alias": 5,
+ "memo": 6,
+ "bookmark": 7,
+ "tags": 8,
+ "icon": 9,
+ "title-img": 10,
+ "style": 11,
+ "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
+ }
+
+ slices.SortStableFunc(n.KramdownIAL, func(a, b []string) int {
+ return compareBlockKramdownIALAttrNames(a[0], b[0])
+ })
+ return ast.WalkContinue
+ })
+}
+
+func compareBlockKramdownIALAttrNames(a, b string) int {
+ aPriority, aBuiltIn := blockKramdownIALAttrPriority[a]
+ bPriority, bBuiltIn := blockKramdownIALAttrPriority[b]
+ if aBuiltIn && bBuiltIn {
+ return aPriority - bPriority
+ }
+ if aBuiltIn {
+ return -1
+ }
+ if bBuiltIn {
+ return 1
+ }
+
+ aSystemManaged := isSystemManagedBlockKramdownIALAttr(a)
+ bSystemManaged := isSystemManagedBlockKramdownIALAttr(b)
+ if aSystemManaged && !bSystemManaged {
+ return -1
+ }
+ if !aSystemManaged && bSystemManaged {
+ return 1
+ }
+ return strings.Compare(a, b)
+}
+
+func isSystemManagedBlockKramdownIALAttr(name string) bool {
+ return "custom-avs" == name || "custom-heading-mode" == name || "custom-reminder-wechat" == name ||
+ strings.HasPrefix(name, "custom-riff-") || strings.HasPrefix(name, "custom-sy-")
+}
+
type ChildBlock struct {
ID string `json:"id"`
Type string `json:"type"`
diff --git a/kernel/model/block_kramdown_test.go b/kernel/model/block_kramdown_test.go
new file mode 100644
index 0000000000..3ddffcc561
--- /dev/null
+++ b/kernel/model/block_kramdown_test.go
@@ -0,0 +1,70 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "slices"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+)
+
+func TestCanonicalizeBlockKramdownIAL(t *testing.T) {
+ root := &ast.Node{Type: ast.NodeDocument, KramdownIAL: 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}
+
+ canonicalizeBlockKramdownIAL(tree)
+
+ assertBlockKramdownIALAttrNames(t, root.KramdownIAL, []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"})
+}
+
+func testBlockKramdownIAL(names ...string) (ret [][]string) {
+ for _, name := range names {
+ ret = append(ret, []string{name, name + "-value"})
+ }
+ return
+}
+
+func assertBlockKramdownIALAttrNames(t *testing.T, ial [][]string, expected []string) {
+ t.Helper()
+ var actual []string
+ for _, attr := range ial {
+ actual = append(actual, attr[0])
+ }
+ if !slices.Equal(actual, expected) {
+ t.Fatalf("unexpected IAL attribute order: got %v, want %v", actual, expected)
+ }
+}