🎨 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"` Children []*Block `json:"children"`
Depth int `json:"depth"` Depth int `json:"depth"`
Count int `json:"count"` Count int `json:"count"`
RefCount int `json:"refCount"`
Sort int `json:"sort"` Sort int `json:"sort"`
Created string `json:"created"` Created string `json:"created"`
Updated string `json:"updated"` Updated string `json:"updated"`
@ -765,7 +766,7 @@ func GetBlockKramdown(id, mode string) (ret string) {
if "md" == mode { if "md" == mode {
// `/api/block/getBlockKramdown` link/image URLs are no longer encoded with spaces https://github.com/siyuan-note/siyuan/issues/15611 // `/api/block/getBlockKramdown` link/image URLs are no longer encoded with spaces https://github.com/siyuan-note/siyuan/issues/15611
luteEngine.SetPreventEncodeLinkSpace(true) luteEngine.SetPreventEncodeLinkSpace(true)
ret = treenode.ExportNodeStdMd(root, luteEngine) ret = treenode.ExportNodeStdMd(root, luteEngine)
} else { } else {
tree.Root = root tree.Root = root

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