瀏覽代碼

Merge remote-tracking branch 'origin/dev' into dev

Vanessa 2 年之前
父節點
當前提交
c738230bd9

+ 2 - 4
app/appearance/langs/en_US.json

@@ -265,10 +265,6 @@
   "vLayout": "Vertical layout",
   "hLayout": "Horizontal layout",
   "merge": "Merge",
-  "docWordCount": "Document words",
-  "blockWordCount": "Block words",
-  "docRuneCount": "Document characters",
-  "blockRuneCount": "Block characters",
   "wordCount": "Words",
   "runeCount": "Characters",
   "kbd": "Keyboard",
@@ -733,6 +729,8 @@
   "italic": "Italic",
   "line": "Divider",
   "link": "Link",
+  "image": "Image",
+  "ref": "Ref",
   "list": "List",
   "more": "More",
   "nameEmpty": "Name is empty",

+ 2 - 4
app/appearance/langs/es_ES.json

@@ -265,10 +265,6 @@
   "vLayout": "Diseño vertical",
   "hLayout": "Diseño horizontal",
   "merge": "Fusionar",
-  "docWordCount": "Palabras del documento",
-  "blockWordCount": "Palabras del bloque",
-  "docRuneCount": "Caracteres del documento",
-  "blockRuneCount": "Caracteres del bloque",
   "wordCount": "Palabras",
   "runeCount": "Caracteres",
   "kbd": "Teclado",
@@ -733,6 +729,8 @@
   "italic": "Cursiva",
   "line": "Divisor",
   "link": "Enlace",
+  "imagen": "Imagen",
+  "ref": "Ref",
   "list": "Lista",
   "more": "Más",
   "nameEmpty": "El nombre está vacío",

+ 2 - 4
app/appearance/langs/fr_FR.json

@@ -265,10 +265,6 @@
   "vLayout": "Disposition verticale",
   "hLayout": "Horizontal horizontale",
   "merge": "Merge",
-  "docWordCount": "Document words",
-  "blockWordCount": "Mots de bloc",
-  "docRuneCount": "Caractères des documents",
-  "blockRuneCount": "Caractères de bloc",
   "wordCount": "Mots",
   "runeCount": "Caractères",
   "kbd": "Clavier",
@@ -733,6 +729,8 @@
   "italic": "Italique",
   "line": "Diviseur",
   "link": "Lien",
+  "image": "Image",
+  "ref": "Réf",
   "list": "Liste",
   "more": "Plus",
   "nameEmpty": "Nom est vide",

+ 2 - 4
app/appearance/langs/zh_CHT.json

@@ -265,10 +265,6 @@
   "vLayout": "垂直佈局",
   "hLayout": "水平佈局",
   "merge": "合併",
-  "docWordCount": "文檔詞數",
-  "blockWordCount": " 塊詞數",
-  "docRuneCount": "文檔字數",
-  "blockRuneCount": " 塊字數",
   "wordCount": "詞數",
   "runeCount": "字數",
   "kbd": "鍵盤",
@@ -733,6 +729,8 @@
   "italic": "斜體",
   "line": "分隔線",
   "link": "連結",
+  "image": "圖片",
+  "ref": "引用",
   "list": "無序列表",
   "more": "更多",
   "nameEmpty": "檔案名不能為空",

+ 2 - 4
app/appearance/langs/zh_CN.json

@@ -265,10 +265,6 @@
   "vLayout": "垂直布局",
   "hLayout": "水平布局",
   "merge": "合并",
-  "docWordCount": "文档词数",
-  "blockWordCount": " 块词数",
-  "docRuneCount": "文档字数",
-  "blockRuneCount": " 块字数",
   "wordCount": "词数",
   "runeCount": "字数",
   "kbd": "键盘",
@@ -733,6 +729,8 @@
   "italic": "斜体",
   "line": "分隔线",
   "link": "链接",
+  "image": "图片",
+  "ref": "引用",
   "list": "无序列表",
   "more": "更多",
   "nameEmpty": "文件名不能为空",

+ 5 - 0
app/src/dialog/processSystem.ts

@@ -9,6 +9,7 @@ import {hideMessage, showMessage} from "./message";
 import {Dialog} from "./index";
 import {isMobile} from "../util/functions";
 import {confirmDialog} from "./confirmDialog";
+import {renderStatusbarCounter} from "../layout/status";
 
 export const lockFile = (id: string) => {
     const html = `<div class="b3-dialog__scrim"></div>
@@ -187,6 +188,10 @@ export const progressStatus = (data: IWebSocketData) => {
     document.querySelector("#status .status__msg").innerHTML = data.msg;
 };
 
+export const handleStatusbarCounter = (data: IWebSocketData) => {
+    renderStatusbarCounter(data.data);
+};
+
 export const progressLoading = (data: IWebSocketData) => {
     let progressElement = document.getElementById("progress");
     if (!progressElement) {

+ 3 - 1
app/src/index.ts

@@ -12,7 +12,7 @@ import {addBaseURL, setNoteBook} from "./util/pathName";
 import {openFileById} from "./editor/util";
 import {
     bootSync,
-    downloadProgress,
+    downloadProgress, handleStatusbarCounter,
     progressLoading,
     progressStatus,
     setTitle,
@@ -48,6 +48,8 @@ class App {
                             case"statusbar":
                                 progressStatus(data);
                                 break;
+                            case"statusbarCounter":
+                                handleStatusbarCounter(data)
                             case"downloadProgress":
                                 downloadProgress(data.data);
                                 break;

+ 16 - 10
app/src/layout/status.ts

@@ -137,11 +137,7 @@ export const countSelectWord = (range: Range) => {
     const selectText = range.toString();
     if (selectText) {
         fetchPost("/api/block/getContentWordCount", {"content": range.toString()}, (response) => {
-            document.querySelector("#status .status__counter").innerHTML = `<span class="ft__on-surface">${window.siyuan.languages.runeCount}</span>
-&nbsp;${response.data.runeCount}
-<span class="fn__space"></span>
-<span class="ft__on-surface">${window.siyuan.languages.wordCount}</span>
-&nbsp;${response.data.wordCount}<span class="fn__space"></span>`;
+            renderStatusbarCounter(response.data);
         });
     } else {
         document.querySelector("#status .status__counter").innerHTML = "";
@@ -156,11 +152,7 @@ export const countBlockWord = (ids: string[]) => {
     }
     if (ids.length > 0) {
         fetchPost("/api/block/getBlocksWordCount", {ids}, (response) => {
-            document.querySelector("#status .status__counter").innerHTML = `<span class="ft__on-surface">${window.siyuan.languages.runeCount}</span>
-&nbsp;${response.data.runeCount}
-<span class="fn__space"></span>
-<span class="ft__on-surface">${window.siyuan.languages.wordCount}</span>
-&nbsp;${response.data.wordCount}<span class="fn__space"></span>`;
+            renderStatusbarCounter(response.data);
         });
     } else {
         document.querySelector("#status .status__counter").innerHTML = "";
@@ -168,3 +160,17 @@ export const countBlockWord = (ids: string[]) => {
     /// #endif
 };
 
+export const renderStatusbarCounter = (stat: { runeCount: number, wordCount: number, linkCount: number, imageCount: number, refCount: number }) => {
+    let html = `<span class="ft__on-surface">${window.siyuan.languages.runeCount}</span>&nbsp;${stat.runeCount}<span class="fn__space"></span>
+<span class="ft__on-surface">${window.siyuan.languages.wordCount}</span>&nbsp;${stat.wordCount}<span class="fn__space"></span>`
+    if (0 < stat.linkCount) {
+        html += `<span class="ft__on-surface">${window.siyuan.languages.link}</span>&nbsp;${stat.linkCount}<span class="fn__space"></span>`
+    }
+    if (0 < stat.imageCount) {
+        html += `<span class="ft__on-surface">${window.siyuan.languages.image}</span>&nbsp;${stat.imageCount}<span class="fn__space"></span>`
+    }
+    if (0 < stat.refCount) {
+        html += `<span class="ft__on-surface">${window.siyuan.languages.ref}</span>&nbsp;${stat.refCount}<span class="fn__space"></span>`
+    }
+    document.querySelector("#status .status__counter").innerHTML = html;
+}

+ 6 - 5
app/src/protyle/breadcrumb/index.ts

@@ -135,7 +135,7 @@ export class Breadcrumb {
         if (cursorNodeElement) {
             id = cursorNodeElement.getAttribute("data-node-id");
         }
-        fetchPost("/api/block/getBlockWordCount", {id: id || protyle.block.id}, (response) => {
+        fetchPost("/api/block/getTreeStat", {id: id || protyle.block.id}, (response) => {
             window.siyuan.menus.menu.remove();
 
             if (!protyle.contentElement.classList.contains("fn__none")) {
@@ -327,10 +327,11 @@ export class Breadcrumb {
             window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
             window.siyuan.menus.menu.append(new MenuItem({
                 type: "readonly",
-                label: `<div class="fn__flex">${window.siyuan.languages.docRuneCount}<span class="fn__space fn__flex-1"></span>${response.data.rootBlockRuneCount}</div>
-<div class="fn__flex">${window.siyuan.languages.docWordCount}<span class="fn__space fn__flex-1"></span>${response.data.rootBlockWordCount}</div>
-<div class="fn__flex">${window.siyuan.languages.blockRuneCount}<span class="fn__space fn__flex-1"></span>${response.data.blockRuneCount}</div>
-<div class="fn__flex">${window.siyuan.languages.blockWordCount}<span class="fn__space fn__flex-1"></span>${response.data.blockWordCount}</div>`,
+                label: `<div class="fn__flex">${window.siyuan.languages.runeCount}<span class="fn__space fn__flex-1"></span>${response.data.runeCount}</div>
+<div class="fn__flex">${window.siyuan.languages.wordCount}<span class="fn__space fn__flex-1"></span>${response.data.wordCount}</div>
+<div class="fn__flex">${window.siyuan.languages.link}<span class="fn__space fn__flex-1"></span>${response.data.linkCount}</div>
+<div class="fn__flex">${window.siyuan.languages.image}<span class="fn__space fn__flex-1"></span>${response.data.imageCount}</div>
+<div class="fn__flex">${window.siyuan.languages.ref}<span class="fn__space fn__flex-1"></span>${response.data.refCount}</div>`,
             }).element);
             window.siyuan.menus.menu.popup(position);
         });

文件差異過大導致無法顯示
+ 0 - 0
app/stage/protyle/js/lute/lute.min.js


+ 4 - 18
kernel/api/block.go

@@ -157,11 +157,7 @@ func getContentWordCount(c *gin.Context) {
 	}
 
 	content := arg["content"].(string)
-	runeCount, wordCount := model.ContentWordCount(content)
-	ret.Data = map[string]interface{}{
-		"runeCount": runeCount,
-		"wordCount": wordCount,
-	}
+	ret.Data = model.ContentStat(content)
 }
 
 func getBlocksWordCount(c *gin.Context) {
@@ -178,14 +174,10 @@ func getBlocksWordCount(c *gin.Context) {
 	for _, id := range idsArg {
 		ids = append(ids, id.(string))
 	}
-	runeCount, wordCount := model.BlocksWordCount(ids)
-	ret.Data = map[string]interface{}{
-		"runeCount": runeCount,
-		"wordCount": wordCount,
-	}
+	ret.Data = model.BlocksWordCount(ids)
 }
 
-func getBlockWordCount(c *gin.Context) {
+func getTreeStat(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)
 
@@ -195,13 +187,7 @@ func getBlockWordCount(c *gin.Context) {
 	}
 
 	id := arg["id"].(string)
-	blockRuneCount, blockWordCount, rootBlockRuneCount, rootBlockWordCount := model.BlockWordCount(id)
-	ret.Data = map[string]interface{}{
-		"blockRuneCount":     blockRuneCount,
-		"blockWordCount":     blockWordCount,
-		"rootBlockRuneCount": rootBlockRuneCount,
-		"rootBlockWordCount": rootBlockWordCount,
-	}
+	ret.Data = model.StatTree(id)
 }
 
 func getRefText(c *gin.Context) {

+ 17 - 0
kernel/api/ref.go

@@ -38,6 +38,23 @@ func refreshBacklink(c *gin.Context) {
 	model.RefreshBacklink(id)
 }
 
+func getBackmentionDoc(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		return
+	}
+
+	defID := arg["defID"].(string)
+	keyword := ""
+	backlinks := model.GetBackmentionDoc(defID, keyword)
+	ret.Data = map[string]interface{}{
+		"backmentions": backlinks,
+	}
+}
+
 func getBacklinkDoc(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)

+ 2 - 1
kernel/api/router.go

@@ -134,7 +134,7 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/block/getRefIDsByFileAnnotationID", model.CheckAuth, getRefIDsByFileAnnotationID)
 	ginServer.Handle("POST", "/api/block/getBlockDefIDsByRefText", model.CheckAuth, getBlockDefIDsByRefText)
 	ginServer.Handle("POST", "/api/block/getRefText", model.CheckAuth, getRefText)
-	ginServer.Handle("POST", "/api/block/getBlockWordCount", model.CheckAuth, getBlockWordCount)
+	ginServer.Handle("POST", "/api/block/getTreeStat", model.CheckAuth, getTreeStat)
 	ginServer.Handle("POST", "/api/block/getBlocksWordCount", model.CheckAuth, getBlocksWordCount)
 	ginServer.Handle("POST", "/api/block/getContentWordCount", model.CheckAuth, getContentWordCount)
 	ginServer.Handle("POST", "/api/block/getRecentUpdatedBlocks", model.CheckAuth, getRecentUpdatedBlocks)
@@ -157,6 +157,7 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/ref/refreshBacklink", model.CheckAuth, refreshBacklink)
 	ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink)
 	ginServer.Handle("POST", "/api/ref/getBacklinkDoc", model.CheckAuth, getBacklinkDoc)
+	ginServer.Handle("POST", "/api/ref/getBackmentionDoc", model.CheckAuth, getBackmentionDoc)
 	ginServer.Handle("POST", "/api/ref/createBacklink", model.CheckAuth, model.CheckReadonly, createBacklink)
 
 	ginServer.Handle("POST", "/api/attr/getBookmarkLabels", model.CheckAuth, getBookmarkLabels)

+ 1 - 1
kernel/go.mod

@@ -6,7 +6,7 @@ require (
 	github.com/88250/clipboard v0.1.5
 	github.com/88250/css v0.1.2
 	github.com/88250/gulu v1.2.3-0.20220929123404-da1dc91c9343
-	github.com/88250/lute v1.7.5-0.20220928025238-bda91cbd4072
+	github.com/88250/lute v1.7.5-0.20221001045738-06a8c2407d65
 	github.com/88250/pdfcpu v0.3.13
 	github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1
 	github.com/ConradIrwin/font v0.0.0-20210318200717-ce8d41cc0732

+ 2 - 0
kernel/go.sum

@@ -19,6 +19,8 @@ github.com/88250/gulu v1.2.3-0.20220929123404-da1dc91c9343 h1:GJxJRZmA8GkAiU3Gsw
 github.com/88250/gulu v1.2.3-0.20220929123404-da1dc91c9343/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
 github.com/88250/lute v1.7.5-0.20220928025238-bda91cbd4072 h1:0d7YXGtw2ybeGs6oClIFiKvTqySfJCu5SUdJJWil6MA=
 github.com/88250/lute v1.7.5-0.20220928025238-bda91cbd4072/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
+github.com/88250/lute v1.7.5-0.20221001045738-06a8c2407d65 h1:EyxFJkB2DXZrAzSaMPZhSs72NzEdsn4YNh/zmqat8IY=
+github.com/88250/lute v1.7.5-0.20221001045738-06a8c2407d65/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
 github.com/88250/pdfcpu v0.3.13 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=
 github.com/88250/pdfcpu v0.3.13/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4=
 github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY=

+ 102 - 67
kernel/model/backlink.go

@@ -26,6 +26,7 @@ import (
 	"strings"
 
 	"github.com/88250/gulu"
+	"github.com/88250/lute"
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/parse"
 	"github.com/emirpasic/gods/sets/hashset"
@@ -163,9 +164,8 @@ type Backlink struct {
 	Expand     bool         `json:"expand"`
 }
 
-func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
+func GetBackmentionDoc(defID, keyword string) (ret []*Backlink) {
 	ret = []*Backlink{}
-	keyword := ""
 	beforeLen := 12
 	sqlBlock := sql.GetBlock(defID)
 	if nil == sqlBlock {
@@ -173,7 +173,39 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 	}
 	rootID := sqlBlock.RootID
 
-	var links []*Block
+	refs := sql.QueryRefsByDefID(defID, true)
+	refs = removeDuplicatedRefs(refs) // 同一个块中引用多个相同块时反链去重 https://github.com/siyuan-note/siyuan/issues/3317
+
+	linkRefs, excludeBacklinkIDs := buildLinkRefs(rootID, refs)
+	mentions := buildTreeBackmention(sqlBlock, linkRefs, keyword, excludeBacklinkIDs, beforeLen)
+	luteEngine := NewLute()
+	treeCache := map[string]*parse.Tree{}
+	for _, mention := range mentions {
+		refTree := treeCache[mention.RootID]
+		if nil == refTree {
+			var loadErr error
+			refTree, loadErr = loadTreeByBlockID(mention.ID)
+			if nil != loadErr {
+				logging.LogWarnf("load ref tree [%s] failed: %s", mention.ID, loadErr)
+				continue
+			}
+			treeCache[mention.RootID] = refTree
+		}
+
+		backlink := buildBacklink(mention.ID, refTree, luteEngine)
+		ret = append(ret, backlink)
+	}
+	return
+}
+
+func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
+	ret = []*Backlink{}
+	sqlBlock := sql.GetBlock(defID)
+	if nil == sqlBlock {
+		return
+	}
+	rootID := sqlBlock.RootID
+
 	tmpRefs := sql.QueryRefsByDefID(defID, true)
 	var refs []*sql.Ref
 	for _, ref := range tmpRefs {
@@ -183,6 +215,22 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 	}
 	refs = removeDuplicatedRefs(refs) // 同一个块中引用多个相同块时反链去重 https://github.com/siyuan-note/siyuan/issues/3317
 
+	linkRefs, _ := buildLinkRefs(rootID, refs)
+	refTree, err := loadTreeByBlockID(refTreeID)
+	if nil != err {
+		logging.LogWarnf("load ref tree [%s] failed: %s", refTreeID, err)
+		return
+	}
+
+	luteEngine := NewLute()
+	for _, linkRef := range linkRefs {
+		backlink := buildBacklink(linkRef.ID, refTree, luteEngine)
+		ret = append(ret, backlink)
+	}
+	return
+}
+
+func buildLinkRefs(defRootID string, refs []*sql.Ref) (ret []*Block, excludeBacklinkIDs *hashset.Set) {
 	// 为了减少查询,组装好 IDs 后一次查出
 	defSQLBlockIDs, refSQLBlockIDs := map[string]bool{}, map[string]bool{}
 	var queryBlockIDs []string
@@ -206,7 +254,8 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 		}
 	}
 
-	excludeBacklinkIDs := hashset.New()
+	var links []*Block
+	excludeBacklinkIDs = hashset.New()
 	for _, ref := range refs {
 		defSQLBlock := defSQLBlocksCache[(ref.DefBlockID)]
 		if nil == defSQLBlock {
@@ -217,12 +266,12 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 		if nil == refSQLBlock {
 			continue
 		}
-		refBlock := fromSQLBlock(refSQLBlock, "", beforeLen)
-		if rootID == refBlock.RootID { // 排除当前文档内引用提及
+		refBlock := fromSQLBlock(refSQLBlock, "", 12)
+		if defRootID == refBlock.RootID { // 排除当前文档内引用提及
 			excludeBacklinkIDs.Add(refBlock.RootID, refBlock.ID)
 		}
-		defBlock := fromSQLBlock(defSQLBlock, "", beforeLen)
-		if defBlock.RootID == rootID { // 当前文档的定义块
+		defBlock := fromSQLBlock(defSQLBlock, "", 12)
+		if defBlock.RootID == defRootID { // 当前文档的定义块
 			links = append(links, defBlock)
 			if ref.DefBlockID == defBlock.ID {
 				defBlock.Refs = append(defBlock.Refs, refBlock)
@@ -236,7 +285,6 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 		}
 	}
 
-	var linkRefs []*Block
 	processedParagraphs := hashset.New()
 	var paragraphParentIDs []string
 	for _, link := range links {
@@ -249,7 +297,7 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 	paragraphParents := sql.GetBlocks(paragraphParentIDs)
 	for _, p := range paragraphParents {
 		if "i" == p.Type || "h" == p.Type {
-			linkRefs = append(linkRefs, fromSQLBlock(p, keyword, beforeLen))
+			ret = append(ret, fromSQLBlock(p, "", 12))
 			processedParagraphs.Add(p.ID)
 		}
 	}
@@ -263,83 +311,70 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
 
 			ref.DefID = link.ID
 			ref.DefPath = link.Path
-
-			content := ref.Content
-			if "" != keyword {
-				_, content = search.MarkText(content, keyword, beforeLen, Conf.Search.CaseSensitive)
-				ref.Content = content
-			}
-			linkRefs = append(linkRefs, ref)
+			ret = append(ret, ref)
 		}
 	}
+	return
+}
 
-	luteEngine := NewLute()
-	refTree, err := loadTreeByBlockID(refTreeID)
-	if nil != err {
-		logging.LogErrorf("load ref tree [%s] failed: %s", refTreeID, err)
+func buildBacklink(refID string, refTree *parse.Tree, luteEngine *lute.Lute) (ret *Backlink) {
+	n := treenode.GetNodeInTree(refTree, refID)
+	if nil == n {
 		return
 	}
 
-	for _, linkRef := range linkRefs {
-		n := treenode.GetNodeInTree(refTree, linkRef.ID)
-		if nil == n {
-			continue
+	var renderNodes []*ast.Node
+	expand := true
+	if ast.NodeListItem == n.Type {
+		if nil == n.FirstChild {
+			return
+		}
+
+		c := n.FirstChild
+		if 3 == n.ListData.Typ {
+			c = n.FirstChild.Next
 		}
 
-		var renderNodes []*ast.Node
-		expand := true
-		if ast.NodeListItem == n.Type {
-			if nil == n.FirstChild {
+		for liFirstBlockSpan := c.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next {
+			if treenode.IsBlockRef(liFirstBlockSpan) {
 				continue
 			}
-
-			c := n.FirstChild
-			if 3 == n.ListData.Typ {
-				c = n.FirstChild.Next
+			if "" != strings.TrimSpace(liFirstBlockSpan.Text()) {
+				expand = false
+				break
 			}
+		}
 
-			for liFirstBlockSpan := c.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next {
-				if treenode.IsBlockRef(liFirstBlockSpan) {
-					continue
-				}
-				if "" != strings.TrimSpace(liFirstBlockSpan.Text()) {
-					expand = false
-					break
-				}
-			}
+		renderNodes = append(renderNodes, n)
+	} else if ast.NodeHeading == n.Type {
+		c := n.FirstChild
+		if nil == c {
+			return
+		}
 
-			renderNodes = append(renderNodes, n)
-		} else if ast.NodeHeading == n.Type {
-			c := n.FirstChild
-			if nil == c {
+		for headingFirstSpan := c; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
+			if treenode.IsBlockRef(headingFirstSpan) {
 				continue
 			}
-
-			for headingFirstSpan := c; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
-				if treenode.IsBlockRef(headingFirstSpan) {
-					continue
-				}
-				if "" != strings.TrimSpace(headingFirstSpan.Text()) {
-					expand = false
-					break
-				}
+			if "" != strings.TrimSpace(headingFirstSpan.Text()) {
+				expand = false
+				break
 			}
-
-			renderNodes = append(renderNodes, n)
-			cc := treenode.HeadingChildren(n)
-			renderNodes = append(renderNodes, cc...)
-		} else {
-			renderNodes = append(renderNodes, n)
 		}
 
-		dom := renderBlockDOMByNodes(renderNodes, luteEngine)
-		ret = append(ret, &Backlink{
-			DOM:        dom,
-			BlockPaths: buildBlockBreadcrumb(n),
-			Expand:     expand,
-		})
+		renderNodes = append(renderNodes, n)
+		cc := treenode.HeadingChildren(n)
+		renderNodes = append(renderNodes, cc...)
+	} else {
+		renderNodes = append(renderNodes, n)
 	}
 
+	dom := renderBlockDOMByNodes(renderNodes, luteEngine)
+	ret = &Backlink{
+		DOM:        dom,
+		BlockPaths: buildBlockBreadcrumb(n),
+		Expand:     expand,
+	}
 	return
 }
 

+ 25 - 22
kernel/model/file.go

@@ -387,14 +387,21 @@ func ListDocTree(boxID, path string, sortMode int) (ret []*File, totals int, err
 	return
 }
 
-func ContentWordCount(content string) (runeCount, wordCount int) {
+func ContentStat(content string) (ret *util.BlockStatResult) {
 	luteEngine := NewLute()
 	tree := luteEngine.BlockDOM2Tree(content)
-	runeCount, wordCount = tree.Root.ContentLen()
-	return
+	runeCnt, wordCnt, linkCnt, imgCnt, refCnt := tree.Root.Stat()
+	return &util.BlockStatResult{
+		RuneCount:  runeCnt,
+		WordCount:  wordCnt,
+		LinkCount:  linkCnt,
+		ImageCount: imgCnt,
+		RefCount:   refCnt,
+	}
 }
 
-func BlocksWordCount(ids []string) (runeCount, wordCount int) {
+func BlocksWordCount(ids []string) (ret *util.BlockStatResult) {
+	ret = &util.BlockStatResult{}
 	trees := map[string]*parse.Tree{} // 缓存
 	for _, id := range ids {
 		bt := treenode.GetBlockTree(id)
@@ -413,34 +420,30 @@ func BlocksWordCount(ids []string) (runeCount, wordCount int) {
 		}
 
 		node := treenode.GetNodeInTree(tree, id)
-		blockRuneCount, blockWordCount := node.ContentLen()
-		runeCount += blockRuneCount
-		wordCount += blockWordCount
+		runeCnt, wordCnt, linkCnt, imgCnt, refCnt := node.Stat()
+		ret.RuneCount += runeCnt
+		ret.WordCount += wordCnt
+		ret.LinkCount += linkCnt
+		ret.ImageCount += imgCnt
+		ret.RefCount += refCnt
 	}
 	return
 }
 
-func BlockWordCount(id string) (blockRuneCount, blockWordCount, rootBlockRuneCount, rootBlockWordCount int) {
+func StatTree(id string) (ret *util.BlockStatResult) {
 	tree, _ := loadTreeByBlockID(id)
 	if nil == tree {
 		return
 	}
 
-	node := treenode.GetNodeInTree(tree, id)
-	blockRuneCount, blockWordCount = node.ContentLen()
-	if ast.NodeHeading == node.Type {
-		level := node.HeadingLevel
-		for n := node.Next; nil != n; n = n.Next {
-			if ast.NodeHeading == n.Type && n.HeadingLevel <= level {
-				break
-			}
-			rc, wc := n.ContentLen()
-			blockRuneCount += rc
-			blockWordCount += wc
-		}
+	runeCnt, wordCnt, linkCnt, imgCnt, refCnt := tree.Root.Stat()
+	return &util.BlockStatResult{
+		RuneCount:  runeCnt,
+		WordCount:  wordCnt,
+		LinkCount:  linkCnt,
+		ImageCount: imgCnt,
+		RefCount:   refCnt,
 	}
-	rootBlockRuneCount, rootBlockWordCount = tree.Root.ContentLen()
-	return
 }
 
 func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) {

+ 7 - 0
kernel/model/transaction.go

@@ -997,6 +997,8 @@ func (tx *Transaction) begin() (err error) {
 
 func (tx *Transaction) commit() (err error) {
 	for _, tree := range tx.trees {
+		go pushTreeStat(tree)
+
 		if err = writeJSONQueue(tree); nil != err {
 			return
 		}
@@ -1007,6 +1009,11 @@ func (tx *Transaction) commit() (err error) {
 	return
 }
 
+func pushTreeStat(tree *parse.Tree) {
+	stat := treenode.StatTree(tree)
+	util.PushStatusBarCounter(stat)
+}
+
 func (tx *Transaction) rollback() {
 	tx.trees, tx.nodes = nil, nil
 	return

+ 11 - 0
kernel/treenode/tree.go

@@ -31,6 +31,17 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 
+func StatTree(tree *parse.Tree) (ret *util.BlockStatResult) {
+	runeCnt, wordCnt, linkCnt, imgCnt, refCnt := tree.Root.Stat()
+	return &util.BlockStatResult{
+		RuneCount:  runeCnt,
+		WordCount:  wordCnt,
+		LinkCount:  linkCnt,
+		ImageCount: imgCnt,
+		RefCount:   refCnt,
+	}
+}
+
 func NodeHash(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute) string {
 	ialArray := node.KramdownIAL
 	sort.Slice(ialArray, func(i, j int) bool {

+ 12 - 0
kernel/util/websocket.go

@@ -151,6 +151,18 @@ func PushStatusBar(msg string) {
 	BroadcastByType("main", "statusbar", 0, msg, nil)
 }
 
+type BlockStatResult struct {
+	RuneCount  int `json:"runeCount"`
+	WordCount  int `json:"wordCount"`
+	LinkCount  int `json:"linkCount"`
+	ImageCount int `json:"imageCount"`
+	RefCount   int `json:"refCount"`
+}
+
+func PushStatusBarCounter(stat *BlockStatResult) {
+	BroadcastByType("main", "statusbarCounter", 0, "", stat)
+}
+
 func ContextPushMsg(context map[string]interface{}, msg string) {
 	switch context[eventbus.CtxPushMsg].(int) {
 	case eventbus.CtxPushMsgToProgress:

部分文件因文件數量過多而無法顯示