diff --git a/kernel/model/box.go b/kernel/model/box.go index ba51b81dc..066b4a4de 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -494,7 +494,7 @@ func parseKTree(kramdown []byte) (ret *parse.Tree) { return } -func normalizeTree(tree *parse.Tree) { +func normalizeTree(tree *parse.Tree) (yfmRootID, yfmTitle, yfmUpdated string) { if nil == tree.Root.FirstChild { tree.Root.AppendChild(treenode.NewParagraph()) } @@ -573,22 +573,24 @@ func normalizeTree(tree *parse.Tree) { for attrK, attrV := range attrs { // Improve parsing of YAML Front Matter when importing Markdown https://github.com/siyuan-note/siyuan/issues/12962 if "title" == attrK { - tree.Root.SetIALAttr("title", fmt.Sprint(attrV)) + yfmTitle = fmt.Sprint(attrV) + tree.Root.SetIALAttr("title", yfmTitle) continue } if "date" == attrK { created, parseTimeErr := dateparse.ParseIn(fmt.Sprint(attrV), time.Local) if nil == parseTimeErr { - docID := created.Format("20060102150405") + "-" + gulu.Rand.String(7) - tree.Root.ID = docID - tree.Root.SetIALAttr("id", docID) + yfmRootID = created.Format("20060102150405") + "-" + gulu.Rand.String(7) + tree.Root.ID = yfmRootID + tree.Root.SetIALAttr("id", yfmRootID) } continue } if "lastmod" == attrK { updated, parseTimeErr := dateparse.ParseIn(fmt.Sprint(attrV), time.Local) if nil == parseTimeErr { - tree.Root.SetIALAttr("updated", updated.Format("20060102150405")) + yfmUpdated = updated.Format("20060102150405") + tree.Root.SetIALAttr("updated", yfmUpdated) } continue } diff --git a/kernel/model/import.go b/kernel/model/import.go index 16d39c2ad..63e8575a2 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -756,19 +756,19 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) { return io.EOF } - tree = parseStdMd(data) + tree, yfmRootID, yfmTitle, yfmUpdated := parseStdMd(data) if nil == tree { logging.LogErrorf("parse tree [%s] failed", currentPath) return nil } - if "" != tree.Root.ID { - id = tree.Root.ID + if "" != yfmRootID { + id = yfmRootID } - if "" != tree.Root.IALAttr("title") { - title = tree.Root.IALAttr("title") + if "" != yfmTitle { + title = yfmTitle } - updated := tree.Root.IALAttr("updated") + updated := yfmUpdated fname := path.Base(targetPath) targetPath = strings.ReplaceAll(targetPath, fname, id+".sy") targetPaths[curRelPath] = targetPath @@ -872,20 +872,20 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) { if err != nil { return err } - tree := parseStdMd(data) + tree, yfmRootID, yfmTitle, yfmUpdated := parseStdMd(data) if nil == tree { msg := fmt.Sprintf("parse tree [%s] failed", localPath) logging.LogErrorf(msg) return errors.New(msg) } - if "" != tree.Root.ID { - id = tree.Root.ID + if "" != yfmRootID { + id = yfmRootID } - if "" != tree.Root.IALAttr("title") { - title = tree.Root.IALAttr("title") + if "" != yfmTitle { + title = yfmTitle } - updated := tree.Root.IALAttr("updated") + updated := yfmUpdated fname := path.Base(targetPath) targetPath = strings.ReplaceAll(targetPath, fname, id+".sy") @@ -992,14 +992,14 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) { return } -func parseStdMd(markdown []byte) (ret *parse.Tree) { +func parseStdMd(markdown []byte) (ret *parse.Tree, yfmRootID, yfmTitle, yfmUpdated string) { 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 } - normalizeTree(ret) + yfmRootID, yfmTitle, yfmUpdated = normalizeTree(ret) imgHtmlBlock2InlineImg(ret) parse.NestedInlines2FlattedSpansHybrid(ret, false) return