From 2eb3e8ffc79bdc6e9af6f2fc0e27a2061532150a Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 24 Dec 2022 12:05:55 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E5=9D=97=E5=BC=95?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E6=B5=AE=E7=AA=97=E6=98=BE=E7=A4=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20https://github.com/siyuan-note/siyuan/issues/6853?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/filetree.go | 34 ++++++++------ kernel/model/backlink.go | 98 +++++++++++++++++++++------------------- kernel/model/file.go | 29 ++++++++---- 3 files changed, 91 insertions(+), 70 deletions(-) diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index 4c6dba7e5..70de4e69e 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -705,8 +705,13 @@ func getDoc(c *gin.Context) { endID = endIDArg.(string) size = 36 } + isBacklinkArg := arg["isBacklink"] + isBacklink := false + if nil != isBacklinkArg { + isBacklink = isBacklinkArg.(bool) + } - blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size) + blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, keyword, mode, size, isBacklink) if errors.Is(err, filelock.ErrUnableAccessFile) { ret.Code = 2 ret.Data = id @@ -727,19 +732,20 @@ func getDoc(c *gin.Context) { isSyncing := model.IsSyncingFile(rootID) ret.Data = map[string]interface{}{ - "id": id, - "mode": mode, - "parentID": parentID, - "parent2ID": parent2ID, - "rootID": rootID, - "type": typ, - "content": content, - "blockCount": blockCount, - "childBlockCount": childBlockCount, - "eof": eof, - "box": boxID, - "path": docPath, - "isSyncing": isSyncing, + "id": id, + "mode": mode, + "parentID": parentID, + "parent2ID": parent2ID, + "rootID": rootID, + "type": typ, + "content": content, + "blockCount": blockCount, + "childBlockCount": childBlockCount, + "eof": eof, + "box": boxID, + "path": docPath, + "isSyncing": isSyncing, + "isBacklinkExpand": isBacklinkExpand, } } diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index 9cf0a2e95..c0138fd3a 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -147,53 +147,7 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string, return } - 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 - } - - if c != n.LastChild { // 存在子列表 - 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 - } - - 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) - } + renderNodes, expand := getBacklinkRenderNodes(n) if 0 < len(mentionKeywords) { for _, renderNode := range renderNodes { @@ -241,6 +195,56 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string, return } +func getBacklinkRenderNodes(n *ast.Node) (ret []*ast.Node, expand bool) { + expand = true + if ast.NodeListItem == n.Type { + if nil == n.FirstChild { + return + } + + c := n.FirstChild + if 3 == n.ListData.Typ { + c = n.FirstChild.Next + } + + if c != n.LastChild { // 存在子列表 + for liFirstBlockSpan := c.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next { + if treenode.IsBlockRef(liFirstBlockSpan) { + continue + } + if "" != strings.TrimSpace(liFirstBlockSpan.Text()) { + expand = false + break + } + } + } + + ret = append(ret, 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 + } + } + + ret = append(ret, n) + cc := treenode.HeadingChildren(n) + ret = append(ret, cc...) + } else { + ret = append(ret, n) + } + return +} + func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) { keyword = strings.TrimSpace(keyword) mentionKeyword = strings.TrimSpace(mentionKeyword) diff --git a/kernel/model/file.go b/kernel/model/file.go index 718589593..19d693d12 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -452,7 +452,7 @@ func getMarkSpanEnd() string { 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, isBacklink bool) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, isBacklinkExpand bool, err error) { WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致 inputIndex := index @@ -477,9 +477,16 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size return } + if isBacklink { // 引用计数浮窗请求,需要按照反链逻辑组装 https://github.com/siyuan-note/siyuan/issues/6853 + if ast.NodeParagraph == node.Type { + node = node.Parent + } + } + located := false isDoc := ast.NodeDocument == node.Type isHeading := ast.NodeHeading == node.Type + boxID = node.Box docPath = node.Path if isDoc { @@ -582,16 +589,20 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size } var nodes []*ast.Node - - // 如果同时存在 startID 和 endID,则只加载 startID 和 endID 之间的块 [startID, endID] - if "" != startID && "" != endID { - nodes, eof = loadNodesByStartEnd(tree, startID, endID) - if 1 > len(nodes) { - // 按 mode 加载兜底 + if isBacklink { + // 引用计数浮窗请求,需要按照反链逻辑组装 https://github.com/siyuan-note/siyuan/issues/6853 + nodes, isBacklinkExpand = getBacklinkRenderNodes(node) + } else { + // 如果同时存在 startID 和 endID,则只加载 startID 和 endID 之间的块 [startID, endID] + if "" != startID && "" != endID { + nodes, eof = loadNodesByStartEnd(tree, startID, endID) + if 1 > len(nodes) { + // 按 mode 加载兜底 + nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading) + } + } else { nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading) } - } else { - nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading) } refCount := sql.QueryRootChildrenRefCount(rootID)