From e74733b4e1af49977fd77b3df6e37277089ec2de Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 30 Sep 2023 17:57:23 +0800 Subject: [PATCH 1/3] :recycle: Refactor create doc by hpath --- kernel/model/file.go | 4 ++-- kernel/model/path.go | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/kernel/model/file.go b/kernel/model/file.go index 9a1651825..1be997f73 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -1038,7 +1038,7 @@ func CreateWithMarkdown(boxID, hPath, md, parentID, id string) (retID string, er WaitForWritingFiles() luteEngine := util.NewLute() dom := luteEngine.Md2BlockDOM(md, false) - retID, _, err = createDocsByHPath(box.ID, hPath, dom, parentID, id) + retID, 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, err := createDocsByHPath(box.ID, hPath, "", "", "") if nil != err { return } diff --git a/kernel/model/path.go b/kernel/model/path.go index da18ea336..48edfc9a6 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, id string /* id 参数仅在 parentID 不为空的情况下使用 */) (retID string, existed bool, err error) { +func createDocsByHPath(boxID, hPath, content, parentID, id string /* id 参数仅在 parentID 不为空的情况下使用 */) (retID string, err error) { hPath = strings.TrimSuffix(hPath, ".sy") pathBuilder := bytes.Buffer{} pathBuilder.WriteString("/") @@ -62,6 +62,12 @@ func createDocsByHPath(boxID, hPath, content, parentID, id string /* id 参数 } } + root := treenode.GetBlockTreeRootByPath(boxID, hPath) + if nil != root { + retID = root.ID + return + } + parts := strings.Split(hPath, "/")[1:] for i, part := range parts { hPathBuilder.WriteString(part) From 17dd264479cd0aa40320cb69592af848a751378a Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 30 Sep 2023 19:34:28 +0800 Subject: [PATCH 2/3] :art: Improve handling of copy block ref when including images https://github.com/siyuan-note/siyuan/issues/9317 --- kernel/model/blockinfo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go index e2aa800b0..45d27f8a4 100644 --- a/kernel/model/blockinfo.go +++ b/kernel/model/blockinfo.go @@ -120,7 +120,7 @@ func GetBlockRefText(id string) string { func GetDOMText(dom string) (ret string) { luteEngine := NewLute() tree := luteEngine.BlockDOM2Tree(dom) - ret = renderBlockText(tree.Root, nil) + ret = renderBlockText(tree.Root.FirstChild, nil) return } From 49d92538dfa76a70afa5f057274bb2afb99384d3 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 30 Sep 2023 19:52:26 +0800 Subject: [PATCH 3/3] :bug: The subdoc creation path is unstable when a parent doc with the same name exists Fix https://github.com/siyuan-note/siyuan/issues/9322 --- kernel/model/path.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/kernel/model/path.go b/kernel/model/path.go index 48edfc9a6..2f2950740 100644 --- a/kernel/model/path.go +++ b/kernel/model/path.go @@ -35,11 +35,6 @@ import ( func createDocsByHPath(boxID, hPath, content, parentID, id string /* id 参数仅在 parentID 不为空的情况下使用 */) (retID string, err error) { hPath = strings.TrimSuffix(hPath, ".sy") - pathBuilder := bytes.Buffer{} - pathBuilder.WriteString("/") - hPathBuilder := bytes.Buffer{} - hPathBuilder.WriteString("/") - if "" != parentID { retID = id @@ -68,11 +63,35 @@ func createDocsByHPath(boxID, hPath, content, parentID, id string /* id 参数 return } + hPathBuilder := bytes.Buffer{} + hpathBtMap := map[string]*treenode.BlockTree{} parts := strings.Split(hPath, "/")[1:] + // The subdoc creation path is unstable when a parent doc with the same name exists https://github.com/siyuan-note/siyuan/issues/9322 + // 存在同名父文档时子文档创建路径不稳定,这里需要按照完整的 hpath 映射,不能在下面的循环中边构建 hpath 边构建 path,否则虽然 hpath 相同,但是会导致 path 组装错位 + for i, part := range parts { + if i == len(parts)-1 { + break + } + + hPathBuilder.WriteString("/") + hPathBuilder.WriteString(part) + hp := hPathBuilder.String() + root = treenode.GetBlockTreeRootByHPath(boxID, hp) + if nil == root { + break + } + + hpathBtMap[hp] = root + } + + pathBuilder := bytes.Buffer{} + pathBuilder.WriteString("/") + hPathBuilder = bytes.Buffer{} + hPathBuilder.WriteString("/") for i, part := range parts { hPathBuilder.WriteString(part) hp := hPathBuilder.String() - root := treenode.GetBlockTreeRootByHPath(boxID, hp) + root = hpathBtMap[hp] isNotLast := i < len(parts)-1 if nil == root { retID = ast.NewNodeID()