Skip to content

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

:art: 状态栏显示选中内容的字数 https://github.com/siyuan-note/siyuan/issues/4932

Liang Ding46c4d842f928
diff --git a/kernel/api/block.go b/kernel/api/block.go
index 032ed30c77..9a0967f137 100644
--- a/kernel/api/block.go
+++ b/kernel/api/block.go
@@ -104,6 +104,44 @@ func getRecentUpdatedBlocks(c *gin.Context) {
ret.Data = blocks
}
+func getContentWordCount(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ content := arg["content"].(string)
+ runeCount, wordCount := model.ContentWordCount(content)
+ ret.Data = map[string]interface{}{
+ "runeCount": runeCount,
+ "wordCount": wordCount,
+ }
+}
+
+func getBlocksWordCount(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ idsArg := arg["ids"].([]interface{})
+ var ids []string
+ for _, id := range idsArg {
+ ids = append(ids, id.(string))
+ }
+ runeCount, wordCount := model.BlocksWordCount(ids)
+ ret.Data = map[string]interface{}{
+ "runeCount": runeCount,
+ "wordCount": wordCount,
+ }
+}
+
func getBlockWordCount(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 43d925001b..b7c967c032 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -131,6 +131,8 @@ func ServeAPI(ginServer *gin.Engine) {
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/getBlocksWordCount", model.CheckAuth, getBlocksWordCount)
+ ginServer.Handle("POST", "/api/block/getContentWordCount", model.CheckAuth, getContentWordCount)
ginServer.Handle("POST", "/api/block/getRecentUpdatedBlocks", model.CheckAuth, getRecentUpdatedBlocks)
ginServer.Handle("POST", "/api/block/getDocInfo", model.CheckAuth, getDocInfo)
ginServer.Handle("POST", "/api/block/checkBlockExist", model.CheckAuth, checkBlockExist)
diff --git a/kernel/model/file.go b/kernel/model/file.go
index c9fe820ab3..3c84324571 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -364,6 +364,28 @@ func ListDocTree(boxID, path string, sortMode int) (ret []*File, totals int, err
return
}
+func ContentWordCount(content string) (runeCount, wordCount int) {
+ luteEngine := NewLute()
+ tree := luteEngine.BlockDOM2Tree(content)
+ runeCount, wordCount = tree.Root.ContentLen()
+ return
+}
+
+func BlocksWordCount(ids []string) (runeCount, wordCount int) {
+ for _, id := range ids {
+ tree, _ := loadTreeByBlockID(id)
+ if nil == tree {
+ return
+ }
+
+ node := treenode.GetNodeInTree(tree, id)
+ blockRuneCount, blockWordCount := node.ContentLen()
+ runeCount += blockRuneCount
+ wordCount += blockWordCount
+ }
+ return
+}
+
func BlockWordCount(id string) (blockRuneCount, blockWordCount, rootBlockRuneCount, rootBlockWordCount int) {
tree, _ := loadTreeByBlockID(id)
if nil == tree {