🎨 Improve parsing of YAML Front Matter when importing Markdown https://github.com/siyuan-note/siyuan/issues/12962

This commit is contained in:
Daniel 2024-10-30 11:06:49 +08:00
parent da3a6fee16
commit a1aba9a9df
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 22 additions and 20 deletions

View file

@ -494,7 +494,7 @@ func parseKTree(kramdown []byte) (ret *parse.Tree) {
return return
} }
func normalizeTree(tree *parse.Tree) { func normalizeTree(tree *parse.Tree) (yfmRootID, yfmTitle, yfmUpdated string) {
if nil == tree.Root.FirstChild { if nil == tree.Root.FirstChild {
tree.Root.AppendChild(treenode.NewParagraph()) tree.Root.AppendChild(treenode.NewParagraph())
} }
@ -573,22 +573,24 @@ func normalizeTree(tree *parse.Tree) {
for attrK, attrV := range attrs { for attrK, attrV := range attrs {
// Improve parsing of YAML Front Matter when importing Markdown https://github.com/siyuan-note/siyuan/issues/12962 // Improve parsing of YAML Front Matter when importing Markdown https://github.com/siyuan-note/siyuan/issues/12962
if "title" == attrK { if "title" == attrK {
tree.Root.SetIALAttr("title", fmt.Sprint(attrV)) yfmTitle = fmt.Sprint(attrV)
tree.Root.SetIALAttr("title", yfmTitle)
continue continue
} }
if "date" == attrK { if "date" == attrK {
created, parseTimeErr := dateparse.ParseIn(fmt.Sprint(attrV), time.Local) created, parseTimeErr := dateparse.ParseIn(fmt.Sprint(attrV), time.Local)
if nil == parseTimeErr { if nil == parseTimeErr {
docID := created.Format("20060102150405") + "-" + gulu.Rand.String(7) yfmRootID = created.Format("20060102150405") + "-" + gulu.Rand.String(7)
tree.Root.ID = docID tree.Root.ID = yfmRootID
tree.Root.SetIALAttr("id", docID) tree.Root.SetIALAttr("id", yfmRootID)
} }
continue continue
} }
if "lastmod" == attrK { if "lastmod" == attrK {
updated, parseTimeErr := dateparse.ParseIn(fmt.Sprint(attrV), time.Local) updated, parseTimeErr := dateparse.ParseIn(fmt.Sprint(attrV), time.Local)
if nil == parseTimeErr { if nil == parseTimeErr {
tree.Root.SetIALAttr("updated", updated.Format("20060102150405")) yfmUpdated = updated.Format("20060102150405")
tree.Root.SetIALAttr("updated", yfmUpdated)
} }
continue continue
} }

View file

@ -756,19 +756,19 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
return io.EOF return io.EOF
} }
tree = parseStdMd(data) tree, yfmRootID, yfmTitle, yfmUpdated := parseStdMd(data)
if nil == tree { if nil == tree {
logging.LogErrorf("parse tree [%s] failed", currentPath) logging.LogErrorf("parse tree [%s] failed", currentPath)
return nil return nil
} }
if "" != tree.Root.ID { if "" != yfmRootID {
id = tree.Root.ID id = yfmRootID
} }
if "" != tree.Root.IALAttr("title") { if "" != yfmTitle {
title = tree.Root.IALAttr("title") title = yfmTitle
} }
updated := tree.Root.IALAttr("updated") updated := yfmUpdated
fname := path.Base(targetPath) fname := path.Base(targetPath)
targetPath = strings.ReplaceAll(targetPath, fname, id+".sy") targetPath = strings.ReplaceAll(targetPath, fname, id+".sy")
targetPaths[curRelPath] = targetPath targetPaths[curRelPath] = targetPath
@ -872,20 +872,20 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
if err != nil { if err != nil {
return err return err
} }
tree := parseStdMd(data) tree, yfmRootID, yfmTitle, yfmUpdated := parseStdMd(data)
if nil == tree { if nil == tree {
msg := fmt.Sprintf("parse tree [%s] failed", localPath) msg := fmt.Sprintf("parse tree [%s] failed", localPath)
logging.LogErrorf(msg) logging.LogErrorf(msg)
return errors.New(msg) return errors.New(msg)
} }
if "" != tree.Root.ID { if "" != yfmRootID {
id = tree.Root.ID id = yfmRootID
} }
if "" != tree.Root.IALAttr("title") { if "" != yfmTitle {
title = tree.Root.IALAttr("title") title = yfmTitle
} }
updated := tree.Root.IALAttr("updated") updated := yfmUpdated
fname := path.Base(targetPath) fname := path.Base(targetPath)
targetPath = strings.ReplaceAll(targetPath, fname, id+".sy") targetPath = strings.ReplaceAll(targetPath, fname, id+".sy")
@ -992,14 +992,14 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
return return
} }
func parseStdMd(markdown []byte) (ret *parse.Tree) { func parseStdMd(markdown []byte) (ret *parse.Tree, yfmRootID, yfmTitle, yfmUpdated string) {
luteEngine := util.NewStdLute() luteEngine := util.NewStdLute()
luteEngine.SetYamlFrontMatter(true) // 解析 YAML Front Matter https://github.com/siyuan-note/siyuan/issues/10878 luteEngine.SetYamlFrontMatter(true) // 解析 YAML Front Matter https://github.com/siyuan-note/siyuan/issues/10878
ret = parse.Parse("", markdown, luteEngine.ParseOptions) ret = parse.Parse("", markdown, luteEngine.ParseOptions)
if nil == ret { if nil == ret {
return return
} }
normalizeTree(ret) yfmRootID, yfmTitle, yfmUpdated = normalizeTree(ret)
imgHtmlBlock2InlineImg(ret) imgHtmlBlock2InlineImg(ret)
parse.NestedInlines2FlattedSpansHybrid(ret, false) parse.NestedInlines2FlattedSpansHybrid(ret, false)
return return