Skip to content

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

:bug: Improve internal kernel API `/api/block/getBlockKramdowns` (#17106)

Jeffrey Chence39730a1054
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 5570454512..5871eb7447 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -1015,16 +1015,37 @@ func GetBlockKramdown(id, mode string) (ret string) {
return
}
+ luteEngine := NewLute()
+ return getBlockKramdown0(tree, id, mode, luteEngine)
+}
+
+func GetBlockKramdowns(ids []string, mode string) (ret map[string]string) {
+ ret = make(map[string]string, len(ids))
+ if 0 == len(ids) {
+ return
+ }
+
+ luteEngine := NewLute()
+ for _, id := range ids {
+ // 节点会被移走,tree 不能共享,需重新加载
+ tree, err := LoadTreeByBlockID(id)
+ if err != nil {
+ continue
+ }
+ ret[id] = getBlockKramdown0(tree, id, mode, luteEngine)
+ }
+ return
+}
+
+func getBlockKramdown0(tree *parse.Tree, id, mode string, luteEngine *lute.Lute) (ret string) {
addBlockIALNodes(tree, false)
node := treenode.GetNodeInTree(tree, id)
root := &ast.Node{Type: ast.NodeDocument}
root.AppendChild(node.Next) // IAL
root.PrependChild(node)
- luteEngine := NewLute()
if "md" == mode {
// `/api/block/getBlockKramdown` link/image URLs are no longer encoded with spaces https://github.com/siyuan-note/siyuan/issues/15611
luteEngine.SetPreventEncodeLinkSpace(true)
-
ret = treenode.ExportNodeStdMd(root, luteEngine)
} else {
tree.Root = root
@@ -1034,43 +1055,6 @@ func GetBlockKramdown(id, mode string) (ret string) {
return
}
-func GetBlockKramdowns(ids []string, mode string) (ret map[string]string) {
- ret = map[string]string{}
- if 0 == len(ids) {
- return
- }
-
- luteEngine := NewLute()
- if "md" == mode {
- // `/api/block/getBlockKramdown` link/image URLs are no longer encoded with spaces https://github.com/siyuan-note/siyuan/issues/15611
- luteEngine.SetPreventEncodeLinkSpace(true)
- }
-
- trees := filesys.LoadTrees(ids)
- for id, tree := range trees {
- node := treenode.GetNodeInTree(tree, id)
- if nil == node {
- continue
- }
-
- addBlockIALNodes(tree, false)
- root := &ast.Node{Type: ast.NodeDocument}
- root.AppendChild(node.Next) // IAL
- root.PrependChild(node)
-
- var kramdown string
- if "md" == mode {
- kramdown = treenode.ExportNodeStdMd(root, luteEngine)
- } else {
- tree.Root = root
- formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)
- kramdown = string(formatRenderer.Render())
- }
- ret[id] = kramdown
- }
- return
-}
-
type ChildBlock struct {
ID string `json:"id"`
Type string `json:"type"`