Skip to content

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

:bug: Preserve Markdown mistaken for invalid front matter https://github.com/siyuan-note/siyuan/issues/20653

Daniela377525a120a
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 869e44f3b9..a1b7b9672e 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -60,6 +60,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp"
+ "gopkg.in/yaml.v3"
)
var (
@@ -1992,6 +1993,16 @@ func parseStdMd(markdown []byte, footnotes bool) (ret *parse.Tree, yfmRootID, yf
if nil == ret {
return
}
+ if first := ret.Root.FirstChild; first != nil && first.Type == ast.NodeYamlFrontMatter {
+ attrs := map[string]any{}
+ if content := first.ChildByType(ast.NodeYamlFrontMatterContent); content != nil {
+ if err := yaml.Unmarshal(content.Tokens, &attrs); err != nil {
+ // 无效的元数据按普通 Markdown 解析,保留开头分隔线之间的正文。
+ luteEngine.SetYamlFrontMatter(false)
+ ret = parse.Parse("", markdown, luteEngine.ParseOptions)
+ }
+ }
+ }
lute.NormalizeMarkdownTableCells(ret)
if footnotes {
convertMarkdownFootnotes(ret)
diff --git a/kernel/model/import_front_matter_test.go b/kernel/model/import_front_matter_test.go
new file mode 100644
index 0000000000..e884751640
--- /dev/null
+++ b/kernel/model/import_front_matter_test.go
@@ -0,0 +1,57 @@
+package model
+
+import (
+ "os"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+)
+
+func TestParseStdMdInvalidFrontMatterPreservesMarkdown(t *testing.T) {
+ fixture, err := os.ReadFile("testdata/import_invalid_front_matter.md")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, source := range []string{string(fixture), strings.ReplaceAll(string(fixture), "\n", "\r\n")} {
+ tree, id, title, updated := parseStdMd([]byte(source), true)
+ if id != "" || title != "" || updated != "" {
+ t.Fatalf("Markdown was treated as metadata: %q, %q, %q", id, title, updated)
+ }
+ var text strings.Builder
+ breaks, quotes := 0, 0
+ ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if !entering {
+ return ast.WalkContinue
+ }
+ switch node.Type {
+ case ast.NodeThematicBreak:
+ breaks++
+ case ast.NodeBlockquote:
+ quotes++
+ case ast.NodeText, ast.NodeTextMark:
+ text.WriteString(node.TokensStr())
+ text.WriteString(node.TextMarkTextContent)
+ case ast.NodeYamlFrontMatter:
+ t.Fatal("invalid YAML remained in the imported tree")
+ }
+ return ast.WalkContinue
+ })
+ if breaks != 3 || quotes == 0 || !strings.Contains(text.String(), "Deepseek API搜索关键词") ||
+ !strings.Contains(text.String(), "Claude网页版提示词") || !strings.Contains(text.String(), "此处为站点表格") {
+ t.Fatalf("lost Markdown content: breaks=%d, quotes=%d, text=%q", breaks, quotes, text.String())
+ }
+ }
+}
+
+func TestParseStdMdValidFrontMatterCompatibility(t *testing.T) {
+ tree, id, title, updated := parseStdMd([]byte("---\ntitle: Note\ndate: 2020-01-24\nlastmod: 2023-08-27\ntag: book\n---\n\nBody\n"), false)
+ if !strings.HasPrefix(id, "20200124000000-") || title != "Note" || updated != "20230827000000" ||
+ tree.Root.IALAttr("custom-tag") != "book" {
+ t.Fatalf("front matter metadata changed: %q, %q, %q, %v", id, title, updated, tree.Root.KramdownIAL)
+ }
+ if tree.Root.FirstChild.Type != ast.NodeCodeBlock ||
+ !strings.Contains(tree.Root.FirstChild.ChildByType(ast.NodeCodeBlockCode).TokensStr(), "title: Note") {
+ t.Fatal("valid front matter was not preserved as YAML code")
+ }
+}
diff --git a/kernel/model/testdata/import_invalid_front_matter.md b/kernel/model/testdata/import_invalid_front_matter.md
new file mode 100644
index 0000000000..d43a40fe6e
--- /dev/null
+++ b/kernel/model/testdata/import_invalid_front_matter.md
@@ -0,0 +1,15 @@
+---
+
+> 利用Deepseek API搜索获得的原始信息,再通过网页版Claude Haiku5.5(max) 模型整理得出。
+>
+> Deepseek API搜索关键词:​`把可以免费下载书籍的网站全部返回给我`,连续搜4次。
+>
+> Claude网页版提示词:这 4 份内容都是浏览器的搜索图书下载网站的搜索结果,你帮我把图书下载网站全部整理进一个 Markdown 表格,第一列是序号,第二列个是站点名称,第三列是网站链接,第四列是站点说明,请返回 Markdown 表格给我,不允许返回其他内容,网站不允许遗漏。
+
+---
+
+此处为站点表格,省略!
+
+---
+
+‍