Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Add internal kernel API `/api/block/getTailChildBlocks` https://github.com/siyuan-note/siyuan/issues/9884

Daniela16fde8ae7b5
diff --git a/kernel/api/block.go b/kernel/api/block.go
index 4ef021bd17..e6d32b2a37 100644
--- a/kernel/api/block.go
+++ b/kernel/api/block.go
@@ -533,3 +533,29 @@ func getChildBlocks(c *gin.Context) {
ret.Data = model.GetChildBlocks(id)
}
+
+func getTailChildBlocks(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
+ }
+
+ var n int
+ nArg := arg["n"]
+ if nil != nArg {
+ n = int(nArg.(float64))
+ }
+ if 1 > n {
+ n = 7
+ }
+
+ ret.Data = model.GetTailChildBlocks(id, n)
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 4367e4f43b..571f78f01d 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -160,6 +160,7 @@ func ServeAPI(ginServer *gin.Engine) {
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/getTailChildBlocks", model.CheckAuth, getTailChildBlocks)
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)
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 3309d1e741..9d9a880637 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -546,6 +546,60 @@ func GetChildBlocks(id string) (ret []*ChildBlock) {
return
}
+func GetTailChildBlocks(id string, n int) (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 i := len(children) - 1; 0 <= i; i-- {
+ c := children[i]
+ ret = append(ret, &ChildBlock{
+ ID: c.ID,
+ Type: treenode.TypeAbbr(c.Type.String()),
+ SubType: treenode.SubTypeAbbr(c),
+ })
+ if n == len(ret) {
+ return
+ }
+ }
+ return
+ }
+
+ if !node.IsContainerBlock() {
+ return
+ }
+
+ for c := node.LastChild; nil != c; c = c.Previous {
+ if !c.IsBlock() {
+ continue
+ }
+
+ ret = append(ret, &ChildBlock{
+ ID: c.ID,
+ Type: treenode.TypeAbbr(c.Type.String()),
+ SubType: treenode.SubTypeAbbr(c),
+ })
+
+ if n == len(ret) {
+ return
+ }
+ }
+ return
+}
+
func GetBlock(id string, tree *parse.Tree) (ret *Block, err error) {
ret, err = getBlock(id, tree)
return