Skip to content

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

:art: 记录文档浏览位置 https://github.com/siyuan-note/siyuan/issues/4042

Liang Dinge40bc18d86e3
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go
index 0b3f8f3baa..cdc2bfc77c 100644
--- a/kernel/api/filetree.go
+++ b/kernel/api/filetree.go
@@ -598,8 +598,16 @@ func getDoc(c *gin.Context) {
if nil != s {
size = int(s.(float64))
}
+ startID := ""
+ endID := ""
+ startIDArg := arg["startID"]
+ endIDArg := arg["endID"]
+ if nil != startIDArg && nil != endIDArg {
+ startID = startIDArg.(string)
+ endID = endIDArg.(string)
+ }
- blockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(id, index, keyword, mode, size)
+ blockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size)
if errors.Is(err, filelock.ErrUnableLockFile) {
ret.Code = 2
ret.Data = id
diff --git a/kernel/model/file.go b/kernel/model/file.go
index ab0d97594c..3aaf427f7a 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -422,7 +422,7 @@ func BlockWordCount(id string) (blockRuneCount, blockWordCount, rootBlockRuneCou
return
}
-func GetDoc(id string, index int, keyword string, mode int, size int) (blockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) {
+func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int) (blockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) {
WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致
inputIndex := index
@@ -550,7 +550,18 @@ func GetDoc(id string, index int, keyword string, mode int, size int) (blockCoun
typ = node.Type.String()
}
- nodes, eof := loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
+ var nodes []*ast.Node
+
+ // 如果同时存在 startID 和 endID,则只加载 startID 和 endID 之间的块
+ if "" != startID && "" != endID {
+ nodes, eof = loadNodesByStartEnd(tree, startID, endID)
+ if 1 > len(nodes) {
+ // 按 mode 加载兜底
+ nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
+ }
+ } else {
+ nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
+ }
refCount := sql.QueryRootChildrenRefCount(rootID)
var virtualBlockRefKeywords []string
@@ -698,6 +709,28 @@ func GetDoc(id string, index int, keyword string, mode int, size int) (blockCoun
return
}
+func loadNodesByStartEnd(tree *parse.Tree, startID, endID string) (nodes []*ast.Node, eof bool) {
+ node := treenode.GetNodeInTree(tree, startID)
+ if nil == node {
+ return
+ }
+ nodes = append(nodes, node)
+ for n := node.Next; nil != n; n = n.Next {
+ if n.ID == endID {
+ next := n.Next
+ if nil == next {
+ eof = true
+ } else {
+ eof = util2.IsDocIAL(n.Tokens) || util2.IsDocIAL(next.Tokens)
+ }
+ break
+ }
+
+ nodes = append(nodes, n)
+ }
+ return
+}
+
func loadNodesByMode(node *ast.Node, inputIndex, mode, size int, isDoc, isHeading bool) (nodes []*ast.Node, eof bool) {
if 2 == mode /* 向下 */ {
next := node.Next