Skip to content

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

:bug: Guard unsupported YAML tag types during Markdown import https://github.com/siyuan-note/siyuan/issues/20967

Daniel65e76d262926
diff --git a/kernel/model/box.go b/kernel/model/box.go
index fc11d68c17..a09615b139 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -860,7 +860,12 @@ func normalizeTree(tree *parse.Tree) (yfmRootID, yfmTitle, yfmUpdated string) {
continue
}
- for _, tag := range attrV.([]any) {
+ values, ok := attrV.([]any)
+ if !ok {
+ logging.LogWarnf("unsupported YAML tags type [%T] in [%s]", attrV, n.ID)
+ continue
+ }
+ for _, tag := range values {
tagStr := fmt.Sprintf("%v", tag)
if "" == tag {
continue
diff --git a/kernel/model/import_front_matter_test.go b/kernel/model/import_front_matter_test.go
index e884751640..808dd20bb5 100644
--- a/kernel/model/import_front_matter_test.go
+++ b/kernel/model/import_front_matter_test.go
@@ -55,3 +55,31 @@ func TestParseStdMdValidFrontMatterCompatibility(t *testing.T) {
t.Fatal("valid front matter was not preserved as YAML code")
}
}
+
+func TestParseStdMdTagsTypes(t *testing.T) {
+ for _, tc := range []struct {
+ name, yaml, tags string
+ }{
+ {"string", "tags: ' book, note '", "book, note"},
+ {"list", "tags: [note, 1111]", "note,1111"},
+ {"empty", "tags:", ""},
+ {"integer", "tags: 2026", ""},
+ {"boolean", "tags: true", ""},
+ {"mapping", "tags: {name: note}", ""},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ source := "---\ntitle: Note\n" + tc.yaml + "\n---\n\nBody\n"
+ tree, _, title, _ := parseStdMd([]byte(source), false)
+ if title != "Note" || tree.Root.IALAttr("tags") != tc.tags {
+ t.Fatalf("unexpected metadata: title=%q, tags=%q", title, tree.Root.IALAttr("tags"))
+ }
+ code := tree.Root.FirstChild.ChildByType(ast.NodeCodeBlockCode)
+ if code == nil || !strings.Contains(code.TokensStr(), tc.yaml) {
+ t.Fatal("original metadata was not preserved")
+ }
+ if body := tree.Root.ChildByType(ast.NodeParagraph); body == nil || body.Text() != "Body" {
+ t.Fatalf("body was not preserved: %v", body)
+ }
+ })
+ }
+}