🎨 Block ref search and global search results display reference counts https://github.com/siyuan-note/siyuan/issues/15721

This commit is contained in:
Daniel 2025-08-30 21:05:27 +08:00
parent ab66716178
commit 74304e9cba
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 37 additions and 2 deletions

View file

@ -61,6 +61,7 @@ type Block struct {
Children []*Block `json:"children"`
Depth int `json:"depth"`
Count int `json:"count"`
RefCount int `json:"refCount"`
Sort int `json:"sort"`
Created string `json:"created"`
Updated string `json:"updated"`

View file

@ -314,6 +314,9 @@ func buildEmbedBlock(embedBlockID string, excludeIDs []string, headingMode int,
func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets, isDatabase bool) (ret []*Block, newDoc bool) {
cachedTrees := map[string]*parse.Tree{}
nodeTrees := map[string]*parse.Tree{}
var nodeIDs []string
var nodes []*ast.Node
onlyDoc := false
if isSquareBrackets {
@ -332,6 +335,7 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets,
}
btsID = gulu.Str.RemoveDuplicatedElem(btsID)
bts := treenode.GetBlockTrees(btsID)
for _, ref := range refs {
tree := cachedTrees[ref.DefBlockRootID]
if nil == tree {
@ -347,6 +351,15 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets,
continue
}
nodes = append(nodes, node)
nodeIDs = append(nodeIDs, node.ID)
nodeTrees[node.ID] = tree
}
refCount := sql.QueryRefCount(nodeIDs)
for _, node := range nodes {
tree := nodeTrees[node.ID]
sqlBlock := sql.BuildBlockFromNode(node, tree)
if nil == sqlBlock {
return
@ -355,8 +368,10 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets,
block := fromSQLBlock(sqlBlock, "", 0)
block.RefText = getNodeRefText(node)
block.RefText = maxContent(block.RefText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen)
block.RefCount = refCount[node.ID]
ret = append(ret, block)
}
if 1 > len(ret) {
ret = []*Block{}
}
@ -388,7 +403,7 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets,
hitFirstChildID := false
if b.IsContainerBlock() && "NodeDocument" != b.Type {
// `((` 引用候选中排除当前块的父块 https://github.com/siyuan-note/siyuan/issues/4538
tree := cachedTrees[b.RootID]
tree = cachedTrees[b.RootID]
if nil == tree {
tree, _ = loadTreeByBlockTree(bts[b.RootID])
cachedTrees[b.RootID] = tree
@ -404,16 +419,25 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets,
if "NodeAttributeView" == b.Type {
// 数据库块可以添加到自身数据库块中,当前文档也可以添加到自身数据库块中
tmp = append(tmp, b)
nodeIDs = append(nodeIDs, b.ID)
nodeTrees[b.ID] = tree
} else {
// 排除自身块、父块和根块
if b.ID != id && !hitFirstChildID && b.ID != rootID {
tmp = append(tmp, b)
nodeIDs = append(nodeIDs, b.ID)
nodeTrees[b.ID] = tree
}
}
}
ret = tmp
refCount := sql.QueryRefCount(nodeIDs)
for _, b := range ret {
b.RefCount = refCount[b.ID]
}
if !isDatabase {
// 如果非数据库中搜索块引,则不允许新建重名文档
if block := treenode.GetBlockTree(id); nil != block {
@ -1296,6 +1320,16 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b
if 0 == groupBy {
filterSelfHPath(ret)
}
var nodeIDs []string
for _, b := range ret {
nodeIDs = append(nodeIDs, b.ID)
}
refCount := sql.QueryRefCount(nodeIDs)
for _, b := range ret {
b.RefCount = refCount[b.ID]
}
return
}