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

This commit is contained in:
Vanessa 2022-10-01 17:17:59 +08:00
commit c738230bd9
20 changed files with 232 additions and 154 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -135,7 +135,7 @@ export class Breadcrumb {
if (cursorNodeElement) { if (cursorNodeElement) {
id = cursorNodeElement.getAttribute("data-node-id"); 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(); window.siyuan.menus.menu.remove();
if (!protyle.contentElement.classList.contains("fn__none")) { 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: "separator"}).element);
window.siyuan.menus.menu.append(new MenuItem({ window.siyuan.menus.menu.append(new MenuItem({
type: "readonly", type: "readonly",
label: `<div class="fn__flex">${window.siyuan.languages.docRuneCount}<span class="fn__space fn__flex-1"></span>${response.data.rootBlockRuneCount}</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.docWordCount}<span class="fn__space fn__flex-1"></span>${response.data.rootBlockWordCount}</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.blockRuneCount}<span class="fn__space fn__flex-1"></span>${response.data.blockRuneCount}</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.blockWordCount}<span class="fn__space fn__flex-1"></span>${response.data.blockWordCount}</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); }).element);
window.siyuan.menus.menu.popup(position); window.siyuan.menus.menu.popup(position);
}); });

File diff suppressed because one or more lines are too long

View File

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

View File

@ -38,6 +38,23 @@ func refreshBacklink(c *gin.Context) {
model.RefreshBacklink(id) 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) { func getBacklinkDoc(c *gin.Context) {
ret := gulu.Ret.NewResult() ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret) defer c.JSON(http.StatusOK, ret)

View File

@ -134,7 +134,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/block/getRefIDsByFileAnnotationID", model.CheckAuth, getRefIDsByFileAnnotationID) ginServer.Handle("POST", "/api/block/getRefIDsByFileAnnotationID", model.CheckAuth, getRefIDsByFileAnnotationID)
ginServer.Handle("POST", "/api/block/getBlockDefIDsByRefText", model.CheckAuth, getBlockDefIDsByRefText) ginServer.Handle("POST", "/api/block/getBlockDefIDsByRefText", model.CheckAuth, getBlockDefIDsByRefText)
ginServer.Handle("POST", "/api/block/getRefText", model.CheckAuth, getRefText) 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/getBlocksWordCount", model.CheckAuth, getBlocksWordCount)
ginServer.Handle("POST", "/api/block/getContentWordCount", model.CheckAuth, getContentWordCount) ginServer.Handle("POST", "/api/block/getContentWordCount", model.CheckAuth, getContentWordCount)
ginServer.Handle("POST", "/api/block/getRecentUpdatedBlocks", model.CheckAuth, getRecentUpdatedBlocks) 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/refreshBacklink", model.CheckAuth, refreshBacklink)
ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink) ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink)
ginServer.Handle("POST", "/api/ref/getBacklinkDoc", model.CheckAuth, getBacklinkDoc) 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/ref/createBacklink", model.CheckAuth, model.CheckReadonly, createBacklink)
ginServer.Handle("POST", "/api/attr/getBookmarkLabels", model.CheckAuth, getBookmarkLabels) ginServer.Handle("POST", "/api/attr/getBookmarkLabels", model.CheckAuth, getBookmarkLabels)

View File

@ -6,7 +6,7 @@ require (
github.com/88250/clipboard v0.1.5 github.com/88250/clipboard v0.1.5
github.com/88250/css v0.1.2 github.com/88250/css v0.1.2
github.com/88250/gulu v1.2.3-0.20220929123404-da1dc91c9343 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/pdfcpu v0.3.13
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1
github.com/ConradIrwin/font v0.0.0-20210318200717-ce8d41cc0732 github.com/ConradIrwin/font v0.0.0-20210318200717-ce8d41cc0732

View File

@ -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/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 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.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 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=
github.com/88250/pdfcpu v0.3.13/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4= github.com/88250/pdfcpu v0.3.13/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4=
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY= github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY=

View File

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

View File

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

View File

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

View File

@ -31,6 +31,17 @@ import (
"github.com/siyuan-note/siyuan/kernel/util" "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 { func NodeHash(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute) string {
ialArray := node.KramdownIAL ialArray := node.KramdownIAL
sort.Slice(ialArray, func(i, j int) bool { sort.Slice(ialArray, func(i, j int) bool {

View File

@ -151,6 +151,18 @@ func PushStatusBar(msg string) {
BroadcastByType("main", "statusbar", 0, msg, nil) 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) { func ContextPushMsg(context map[string]interface{}, msg string) {
switch context[eventbus.CtxPushMsg].(int) { switch context[eventbus.CtxPushMsg].(int) {
case eventbus.CtxPushMsgToProgress: case eventbus.CtxPushMsgToProgress: