Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Sep 27, 2023
2 parents 725bc6c + ddffc27 commit bef5a71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
9 changes: 8 additions & 1 deletion kernel/api/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions kernel/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 11 additions & 6 deletions kernel/model/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,33 @@ 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("/")
hPathBuilder := bytes.Buffer{}
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:]
Expand All @@ -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 {
Expand All @@ -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")
Expand Down

0 comments on commit bef5a71

Please sign in to comment.