🎨 New a row in the database no longer require to create a relevant doc https://github.com/siyuan-note/siyuan/issues/9294

This commit is contained in:
Daniel 2023-09-28 00:15:46 +08:00
parent 26db301a12
commit ddffc27745
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 22 additions and 10 deletions

View file

@ -26,6 +26,7 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/filesys" "github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/model" "github.com/siyuan-note/siyuan/kernel/model"
@ -481,6 +482,12 @@ func createDocWithMd(c *gin.Context) {
parentID = parentIDArg.(string) parentID = parentIDArg.(string)
} }
id := ast.NewNodeID()
idArg := arg["id"]
if nil != idArg {
id = idArg.(string)
}
hPath := arg["path"].(string) hPath := arg["path"].(string)
markdown := arg["markdown"].(string) markdown := arg["markdown"].(string)
@ -496,7 +503,7 @@ func createDocWithMd(c *gin.Context) {
hPath = "/" + hPath hPath = "/" + hPath
} }
id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID) err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id)
if nil != err { if nil != err {
ret.Code = -1 ret.Code = -1
ret.Msg = err.Error() ret.Msg = err.Error()

View file

@ -1028,7 +1028,7 @@ func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree
return 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) box := Conf.Box(boxID)
if nil == box { if nil == box {
err = errors.New(Conf.Language(0)) err = errors.New(Conf.Language(0))
@ -1038,7 +1038,7 @@ func CreateWithMarkdown(boxID, hPath, md, parentID 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, parentID) _, _, err = createDocsByHPath(box.ID, hPath, dom, parentID, id)
return return
} }
@ -1442,7 +1442,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
} }

View file

@ -33,7 +33,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/util" "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") hPath = strings.TrimSuffix(hPath, ".sy")
pathBuilder := bytes.Buffer{} pathBuilder := bytes.Buffer{}
pathBuilder.WriteString("/") pathBuilder.WriteString("/")
@ -41,20 +41,25 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist
hPathBuilder.WriteString("/") hPathBuilder.WriteString("/")
if "" != parentID { 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 的情况下优先查找父文档 // 在指定父文档 ID 的情况下优先查找父文档
parentHPath, name := path.Split(hPath) parentHPath, name := path.Split(hPath)
parentHPath = strings.TrimSuffix(parentHPath, "/") parentHPath = strings.TrimSuffix(parentHPath, "/")
preferredParent := treenode.GetBlockTreeRootByHPathPreferredParentID(boxID, parentHPath, parentID) preferredParent := treenode.GetBlockTreeRootByHPathPreferredParentID(boxID, parentHPath, parentID)
if nil != preferredParent && preferredParent.ID == parentID { if nil != preferredParent && preferredParent.ID == parentID {
// 如果父文档存在且 ID 一致,则直接在父文档下创建 // 如果父文档存在且 ID 一致,则直接在父文档下创建
id = ast.NewNodeID()
p := strings.TrimSuffix(preferredParent.Path, ".sy") + "/" + id + ".sy" p := strings.TrimSuffix(preferredParent.Path, ".sy") + "/" + id + ".sy"
if _, err = createDoc(boxID, p, name, content); nil != err { if _, err = createDoc(boxID, p, name, content); nil != err {
return return
} }
} }
} else {
if "" == id {
id = ast.NewNodeID()
retID = id
}
} }
parts := strings.Split(hPath, "/")[1:] parts := strings.Split(hPath, "/")[1:]
@ -64,8 +69,8 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist
root := treenode.GetBlockTreeRootByHPath(boxID, hp) root := treenode.GetBlockTreeRootByHPath(boxID, hp)
isNotLast := i < len(parts)-1 isNotLast := i < len(parts)-1
if nil == root { if nil == root {
id = ast.NewNodeID() retID = ast.NewNodeID()
pathBuilder.WriteString(id) pathBuilder.WriteString(retID)
docP := pathBuilder.String() + ".sy" docP := pathBuilder.String() + ".sy"
if isNotLast { if isNotLast {
if _, err = createDoc(boxID, docP, part, ""); nil != err { if _, err = createDoc(boxID, docP, part, ""); nil != err {
@ -85,7 +90,7 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist
} }
} }
} else { } else {
id = root.ID retID = root.ID
pathBuilder.WriteString(root.ID) pathBuilder.WriteString(root.ID)
if !isNotLast { if !isNotLast {
pathBuilder.WriteString(".sy") pathBuilder.WriteString(".sy")