diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index c965a05dc1d..3377a4a5be1 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -26,6 +26,7 @@ import ( "unicode/utf8" "github.com/88250/gulu" + "github.com/88250/lute/ast" "github.com/gin-gonic/gin" "github.com/siyuan-note/siyuan/kernel/filesys" "github.com/siyuan-note/siyuan/kernel/model" @@ -481,6 +482,12 @@ func createDocWithMd(c *gin.Context) { parentID = parentIDArg.(string) } + id := ast.NewNodeID() + idArg := arg["id"] + if nil != idArg { + id = idArg.(string) + } + hPath := arg["path"].(string) markdown := arg["markdown"].(string) @@ -496,7 +503,7 @@ func createDocWithMd(c *gin.Context) { hPath = "/" + hPath } - id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID) + err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id) if nil != err { ret.Code = -1 ret.Msg = err.Error() diff --git a/kernel/model/file.go b/kernel/model/file.go index cfd8f404c59..a73877716db 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -1028,7 +1028,7 @@ func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree return } -func CreateWithMarkdown(boxID, hPath, md, parentID string) (id string, err error) { +func CreateWithMarkdown(boxID, hPath, md, parentID, id string) (err error) { box := Conf.Box(boxID) if nil == box { err = errors.New(Conf.Language(0)) @@ -1038,7 +1038,7 @@ func CreateWithMarkdown(boxID, hPath, md, parentID string) (id string, err error WaitForWritingFiles() luteEngine := util.NewLute() dom := luteEngine.Md2BlockDOM(md, false) - id, _, err = createDocsByHPath(box.ID, hPath, dom, parentID) + _, _, err = createDocsByHPath(box.ID, hPath, dom, parentID, id) return } @@ -1442,7 +1442,7 @@ func CreateDailyNote(boxID string) (p string, existed bool, err error) { return } - id, existed, err := createDocsByHPath(box.ID, hPath, "", "") + id, existed, err := createDocsByHPath(box.ID, hPath, "", "", "") if nil != err { return } diff --git a/kernel/model/path.go b/kernel/model/path.go index ee9fe774bb6..da18ea33605 100644 --- a/kernel/model/path.go +++ b/kernel/model/path.go @@ -33,7 +33,7 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -func createDocsByHPath(boxID, hPath, content, parentID string) (id string, existed bool, err error) { +func createDocsByHPath(boxID, hPath, content, parentID, id string /* id 参数仅在 parentID 不为空的情况下使用 */) (retID string, existed bool, err error) { hPath = strings.TrimSuffix(hPath, ".sy") pathBuilder := bytes.Buffer{} pathBuilder.WriteString("/") @@ -41,20 +41,25 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist hPathBuilder.WriteString("/") if "" != parentID { - // The save path is incorrect when creating a sub-doc by ref in a doc with the same name https://github.com/siyuan-note/siyuan/issues/8138 + retID = id + // The save path is incorrect when creating a sub-doc by ref in a doc with the same name https://github.com/siyuan-note/siyuan/issues/8138 // 在指定父文档 ID 的情况下优先查找父文档 parentHPath, name := path.Split(hPath) parentHPath = strings.TrimSuffix(parentHPath, "/") preferredParent := treenode.GetBlockTreeRootByHPathPreferredParentID(boxID, parentHPath, parentID) if nil != preferredParent && preferredParent.ID == parentID { // 如果父文档存在且 ID 一致,则直接在父文档下创建 - id = ast.NewNodeID() p := strings.TrimSuffix(preferredParent.Path, ".sy") + "/" + id + ".sy" if _, err = createDoc(boxID, p, name, content); nil != err { return } } + } else { + if "" == id { + id = ast.NewNodeID() + retID = id + } } parts := strings.Split(hPath, "/")[1:] @@ -64,8 +69,8 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist root := treenode.GetBlockTreeRootByHPath(boxID, hp) isNotLast := i < len(parts)-1 if nil == root { - id = ast.NewNodeID() - pathBuilder.WriteString(id) + retID = ast.NewNodeID() + pathBuilder.WriteString(retID) docP := pathBuilder.String() + ".sy" if isNotLast { if _, err = createDoc(boxID, docP, part, ""); nil != err { @@ -85,7 +90,7 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist } } } else { - id = root.ID + retID = root.ID pathBuilder.WriteString(root.ID) if !isNotLast { pathBuilder.WriteString(".sy")