Skip to content

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

:art: Parsing YAML Front Matter as document custom attributes when importing Markdown files https://github.com/siyuan-note/siyuan/issues/10878

Daniel91f74a85460e
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 1c0b4fa1f5..20f1304bbd 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -32,6 +32,7 @@ import (
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/html"
+ "github.com/88250/lute/lex"
"github.com/88250/lute/parse"
"github.com/facette/natsort"
"github.com/siyuan-note/filelock"
@@ -43,6 +44,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
+ "gopkg.in/yaml.v3"
)
// Box 笔记本。
@@ -435,6 +437,7 @@ func normalizeTree(tree *parse.Tree) {
tree.Root.AppendChild(treenode.NewParagraph())
}
+ var unlinks []*ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
@@ -496,9 +499,45 @@ func normalizeTree(tree *parse.Tree) {
n.Tokens = html.UnescapeBytes(n.Tokens)
}
+ if ast.NodeYamlFrontMatterContent == n.Type {
+ // Parsing YAML Front Matter as document custom attributes when importing Markdown files https://github.com/siyuan-note/siyuan/issues/10878
+ attrs := map[string]interface{}{}
+ parseErr := yaml.Unmarshal(n.Tokens, &attrs)
+ if parseErr != nil {
+ logging.LogWarnf("parse YAML front matter [%s] failed: %s", n.Tokens, parseErr)
+ } else {
+ for attrK, attrV := range attrs {
+ validKeyName := true
+ for i := 0; i < len(attrK); i++ {
+ if !lex.IsASCIILetterNumHyphen(attrK[i]) {
+ validKeyName = false
+ break
+ }
+ }
+ if !validKeyName {
+ logging.LogWarnf("invalid YAML key [%s] in [%s]", attrK, n.ID)
+ continue
+ }
+
+ tree.Root.SetIALAttr("custom-"+attrK, fmt.Sprint(attrV))
+ }
+ }
+ }
+
+ if ast.NodeYamlFrontMatter == n.Type {
+ unlinks = append(unlinks, n)
+ }
+
return ast.WalkContinue
})
- tree.Root.KramdownIAL = parse.Tokens2IAL(tree.Root.LastChild.Tokens)
+ for _, n := range unlinks {
+ n.Unlink()
+ }
+
+ rootIAL := parse.Tokens2IAL(tree.Root.LastChild.Tokens)
+ for _, kv := range rootIAL {
+ tree.Root.SetIALAttr(kv[0], kv[1])
+ }
return
}
diff --git a/kernel/model/import.go b/kernel/model/import.go
index cdfc183a64..e88c62e24d 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -979,6 +979,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
func parseStdMd(markdown []byte) (ret *parse.Tree) {
luteEngine := util.NewStdLute()
+ luteEngine.SetYamlFrontMatter(true) // 解析 YAML Front Matter https://github.com/siyuan-note/siyuan/issues/10878
ret = parse.Parse("", markdown, luteEngine.ParseOptions)
if nil == ret {
return