mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🐛 The save path is incorrect when creating a sub-doc by ref in a doc with the same name Fix https://github.com/siyuan-note/siyuan/issues/8138
This commit is contained in:
parent
c7875757c0
commit
f3dd8e5315
4 changed files with 60 additions and 5 deletions
|
|
@ -475,6 +475,12 @@ func createDocWithMd(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var parentID string
|
||||||
|
parentIDArg := arg["parentID"]
|
||||||
|
if nil != parentIDArg {
|
||||||
|
parentID = parentIDArg.(string)
|
||||||
|
}
|
||||||
|
|
||||||
hPath := arg["path"].(string)
|
hPath := arg["path"].(string)
|
||||||
markdown := arg["markdown"].(string)
|
markdown := arg["markdown"].(string)
|
||||||
|
|
||||||
|
|
@ -490,7 +496,7 @@ func createDocWithMd(c *gin.Context) {
|
||||||
hPath = "/" + hPath
|
hPath = "/" + hPath
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := model.CreateWithMarkdown(notebook, hPath, markdown)
|
id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
|
|
|
||||||
|
|
@ -967,7 +967,7 @@ func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateWithMarkdown(boxID, hPath, md string) (id string, err error) {
|
func CreateWithMarkdown(boxID, hPath, md, parentID string) (id string, err error) {
|
||||||
box := Conf.Box(boxID)
|
box := Conf.Box(boxID)
|
||||||
if nil == box {
|
if nil == box {
|
||||||
err = errors.New(Conf.Language(0))
|
err = errors.New(Conf.Language(0))
|
||||||
|
|
@ -977,7 +977,7 @@ func CreateWithMarkdown(boxID, hPath, md string) (id string, err error) {
|
||||||
WaitForWritingFiles()
|
WaitForWritingFiles()
|
||||||
luteEngine := util.NewLute()
|
luteEngine := util.NewLute()
|
||||||
dom := luteEngine.Md2BlockDOM(md, false)
|
dom := luteEngine.Md2BlockDOM(md, false)
|
||||||
id, _, err = createDocsByHPath(box.ID, hPath, dom)
|
id, _, err = createDocsByHPath(box.ID, hPath, dom, parentID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1380,7 +1380,7 @@ func CreateDailyNote(boxID string) (p string, existed bool, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id, existed, err := createDocsByHPath(box.ID, hPath, "")
|
id, existed, err := createDocsByHPath(box.ID, hPath, "", "")
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,13 +32,30 @@ import (
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createDocsByHPath(boxID, hPath, content string) (id string, existed bool, err error) {
|
func createDocsByHPath(boxID, hPath, content, parentID string) (id string, existed bool, err error) {
|
||||||
hPath = strings.TrimSuffix(hPath, ".sy")
|
hPath = strings.TrimSuffix(hPath, ".sy")
|
||||||
pathBuilder := bytes.Buffer{}
|
pathBuilder := bytes.Buffer{}
|
||||||
pathBuilder.WriteString("/")
|
pathBuilder.WriteString("/")
|
||||||
hPathBuilder := bytes.Buffer{}
|
hPathBuilder := bytes.Buffer{}
|
||||||
hPathBuilder.WriteString("/")
|
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
|
||||||
|
|
||||||
|
// 在指定父文档 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parts := strings.Split(hPath, "/")[1:]
|
parts := strings.Split(hPath, "/")[1:]
|
||||||
for i, part := range parts {
|
for i, part := range parts {
|
||||||
hPathBuilder.WriteString(part)
|
hPathBuilder.WriteString(part)
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,38 @@ func GetBlockTreeRootByHPath(boxID, hPath string) (ret *BlockTree) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBlockTreeRootByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) {
|
||||||
|
var roots []*BlockTree
|
||||||
|
blockTrees.Range(func(key, value interface{}) bool {
|
||||||
|
slice := value.(*btSlice)
|
||||||
|
slice.m.Lock()
|
||||||
|
for _, b := range slice.data {
|
||||||
|
if b.BoxID == boxID && b.HPath == hPath && b.RootID == b.ID {
|
||||||
|
if "" == preferredParentID {
|
||||||
|
ret = b
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
roots = append(roots, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slice.m.Unlock()
|
||||||
|
return nil == ret
|
||||||
|
})
|
||||||
|
if 1 > len(roots) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, root := range roots {
|
||||||
|
if root.ID == preferredParentID {
|
||||||
|
ret = root
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret = roots[0]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func GetBlockTree(id string) (ret *BlockTree) {
|
func GetBlockTree(id string) (ret *BlockTree) {
|
||||||
if "" == id {
|
if "" == id {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue