Skip to content

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

:art: Display reference counts in the backlink panel https://github.com/siyuan-note/siyuan/issues/13618

Danielac0bb12dc8fe
diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go
index 334196ae90..9d116cb797 100644
--- a/kernel/model/backlink.go
+++ b/kernel/model/backlink.go
@@ -199,12 +199,12 @@ func sortBacklinks(backlinks []*Backlink, tree *parse.Tree) {
}
func buildBacklink(refID string, refTree *parse.Tree, keywords []string, highlight bool, luteEngine *lute.Lute) (ret *Backlink) {
- n := treenode.GetNodeInTree(refTree, refID)
- if nil == n {
+ node := treenode.GetNodeInTree(refTree, refID)
+ if nil == node {
return
}
- renderNodes, expand := getBacklinkRenderNodes(n)
+ renderNodes, expand := getBacklinkRenderNodes(node)
if highlight && 0 < len(keywords) {
for _, renderNode := range renderNodes {
@@ -229,15 +229,18 @@ func buildBacklink(refID string, refTree *parse.Tree, keywords []string, highlig
}
}
+ // 反链面板中显示块引用计数 Display reference counts in the backlink panel https://github.com/siyuan-note/siyuan/issues/13618
+ fillBlockRefCount(renderNodes)
+
dom := renderBlockDOMByNodes(renderNodes, luteEngine)
var blockPaths []*BlockPath
- if (nil != n.Parent && ast.NodeDocument != n.Parent.Type) || (ast.NodeHeading != n.Type && 0 < treenode.HeadingLevel(n)) {
- blockPaths = buildBlockBreadcrumb(n, nil, false)
+ if (nil != node.Parent && ast.NodeDocument != node.Parent.Type) || (ast.NodeHeading != node.Type && 0 < treenode.HeadingLevel(node)) {
+ blockPaths = buildBlockBreadcrumb(node, nil, false)
}
if 1 > len(blockPaths) {
blockPaths = []*BlockPath{}
}
- ret = &Backlink{DOM: dom, BlockPaths: blockPaths, Expand: expand, node: n}
+ ret = &Backlink{DOM: dom, BlockPaths: blockPaths, Expand: expand, node: node}
return
}
diff --git a/kernel/model/block.go b/kernel/model/block.go
index aec5668b9e..30cbaeb8b4 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -20,14 +20,12 @@ import (
"bytes"
"errors"
"fmt"
- "github.com/88250/lute/render"
- "strconv"
"strings"
"time"
- "github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
+ "github.com/88250/lute/render"
"github.com/open-spaced-repetition/go-fsrs/v3"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
@@ -902,33 +900,7 @@ func getEmbeddedBlock(trees map[string]*parse.Tree, sqlBlock *sql.Block, heading
}
// 嵌入块查询结果中显示块引用计数 https://github.com/siyuan-note/siyuan/issues/7191
- var defIDs []string
- for _, n := range nodes {
- ast.Walk(n, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering {
- return ast.WalkContinue
- }
-
- if n.IsBlock() {
- defIDs = append(defIDs, n.ID)
- }
- return ast.WalkContinue
- })
- }
- defIDs = gulu.Str.RemoveDuplicatedElem(defIDs)
- refCount := sql.QueryRefCount(defIDs)
- for _, n := range nodes {
- ast.Walk(n, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering || !n.IsBlock() {
- return ast.WalkContinue
- }
-
- if cnt := refCount[n.ID]; 0 < cnt {
- n.SetIALAttr("refcount", strconv.Itoa(cnt))
- }
- return ast.WalkContinue
- })
- }
+ fillBlockRefCount(nodes)
luteEngine := NewLute()
luteEngine.RenderOptions.ProtyleContenteditable = false // 不可编辑
diff --git a/kernel/model/render.go b/kernel/model/render.go
index 1a9cb69637..41168dfe06 100644
--- a/kernel/model/render.go
+++ b/kernel/model/render.go
@@ -19,6 +19,7 @@ package model
import (
"bytes"
"regexp"
+ "strconv"
"strings"
"github.com/88250/gulu"
@@ -141,6 +142,36 @@ func renderBlockText(node *ast.Node, excludeTypes []string) (ret string) {
return
}
+func fillBlockRefCount(nodes []*ast.Node) {
+ var defIDs []string
+ for _, n := range nodes {
+ ast.Walk(n, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering {
+ return ast.WalkContinue
+ }
+
+ if n.IsBlock() {
+ defIDs = append(defIDs, n.ID)
+ }
+ return ast.WalkContinue
+ })
+ }
+ defIDs = gulu.Str.RemoveDuplicatedElem(defIDs)
+ refCount := sql.QueryRefCount(defIDs)
+ for _, n := range nodes {
+ ast.Walk(n, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering || !n.IsBlock() {
+ return ast.WalkContinue
+ }
+
+ if cnt := refCount[n.ID]; 0 < cnt {
+ n.SetIALAttr("refcount", strconv.Itoa(cnt))
+ }
+ return ast.WalkContinue
+ })
+ }
+}
+
func renderBlockDOMByNodes(nodes []*ast.Node, luteEngine *lute.Lute) string {
tree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument}, Context: &parse.Context{ParseOption: luteEngine.ParseOptions}}
blockRenderer := render.NewProtyleRenderer(tree, luteEngine.RenderOptions)