Browse Source

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

Daniel 6 months ago
parent
commit
ac0bb12dc8
3 changed files with 42 additions and 36 deletions
  1. 9 6
      kernel/model/backlink.go
  2. 2 30
      kernel/model/block.go
  3. 31 0
      kernel/model/render.go

+ 9 - 6
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
 }
 

+ 2 - 30
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 // 不可编辑

+ 31 - 0
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)