From ac0bb12dc8fe8ae18d1ae56d30c561f3d76071ce Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 27 Dec 2024 10:53:37 +0800 Subject: [PATCH] :art: Display reference counts in the backlink panel https://github.com/siyuan-note/siyuan/issues/13618 --- kernel/model/backlink.go | 15 +++++++++------ kernel/model/block.go | 32 ++------------------------------ kernel/model/render.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index 334196ae9..9d116cb79 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 aec5668b9..30cbaeb8b 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 1a9cb6963..41168dfe0 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)