mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-02 05:31:47 +01:00
🎨 Add kernel API /api/block/getChildBlocks Fix https://github.com/siyuan-note/siyuan/issues/8249
This commit is contained in:
parent
897b1d5ccc
commit
b3b891338b
5 changed files with 149 additions and 1 deletions
|
|
@ -481,3 +481,20 @@ func getBlockKramdown(c *gin.Context) {
|
|||
"kramdown": kramdown,
|
||||
}
|
||||
}
|
||||
|
||||
func getChildBlocks(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
||||
arg, ok := util.JsonArg(c, ret)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
id := arg["id"].(string)
|
||||
if util.InvalidIDPattern(id, ret) {
|
||||
return
|
||||
}
|
||||
|
||||
ret.Data = model.GetChildBlocks(id)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -147,6 +147,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||
ginServer.Handle("POST", "/api/block/getBlockInfo", model.CheckAuth, getBlockInfo)
|
||||
ginServer.Handle("POST", "/api/block/getBlockDOM", model.CheckAuth, getBlockDOM)
|
||||
ginServer.Handle("POST", "/api/block/getBlockKramdown", model.CheckAuth, getBlockKramdown)
|
||||
ginServer.Handle("POST", "/api/block/getChildBlocks", model.CheckAuth, getChildBlocks)
|
||||
ginServer.Handle("POST", "/api/block/getBlockBreadcrumb", model.CheckAuth, getBlockBreadcrumb)
|
||||
ginServer.Handle("POST", "/api/block/getBlockIndex", model.CheckAuth, getBlockIndex)
|
||||
ginServer.Handle("POST", "/api/block/getRefIDs", model.CheckAuth, getRefIDs)
|
||||
|
|
|
|||
|
|
@ -445,6 +445,58 @@ func GetBlockKramdown(id string) (ret string) {
|
|||
return
|
||||
}
|
||||
|
||||
type ChildBlock struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
SubType string `json:"subType,omitempty"`
|
||||
}
|
||||
|
||||
func GetChildBlocks(id string) (ret []*ChildBlock) {
|
||||
ret = []*ChildBlock{}
|
||||
if "" == id {
|
||||
return
|
||||
}
|
||||
|
||||
tree, err := loadTreeByBlockID(id)
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
node := treenode.GetNodeInTree(tree, id)
|
||||
if nil == node {
|
||||
return
|
||||
}
|
||||
|
||||
if ast.NodeHeading == node.Type {
|
||||
children := treenode.HeadingChildren(node)
|
||||
for _, c := range children {
|
||||
ret = append(ret, &ChildBlock{
|
||||
ID: c.ID,
|
||||
Type: treenode.TypeAbbr(c.Type.String()),
|
||||
SubType: treenode.SubTypeAbbr(c),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !node.IsContainerBlock() {
|
||||
return
|
||||
}
|
||||
|
||||
for c := node.FirstChild; nil != c; c = c.Next {
|
||||
if !c.IsBlock() {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(ret, &ChildBlock{
|
||||
ID: c.ID,
|
||||
Type: treenode.TypeAbbr(c.Type.String()),
|
||||
SubType: treenode.SubTypeAbbr(c),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetBlock(id string, tree *parse.Tree) (ret *Block, err error) {
|
||||
ret, err = getBlock(id, tree)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue