From ab8b70d78d52b37db34193ce8dcad69d25596920 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 2 Oct 2022 11:39:25 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:sparkles:=20=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E5=BC=8F=E5=8F=8D=E9=93=BE=E9=9D=A2=E6=9D=BF=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/3565?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/backlink.go | 176 +++++++++++++++++++-------------------- 1 file changed, 88 insertions(+), 88 deletions(-) diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index cea5d30d5..27b8b4361 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -236,94 +236,6 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) { return } -func buildLinkRefs(defRootID string, refs []*sql.Ref) (ret []*Block, refsCount int, excludeBacklinkIDs *hashset.Set) { - // 为了减少查询,组装好 IDs 后一次查出 - defSQLBlockIDs, refSQLBlockIDs := map[string]bool{}, map[string]bool{} - var queryBlockIDs []string - for _, ref := range refs { - defSQLBlockIDs[ref.DefBlockID] = true - refSQLBlockIDs[ref.BlockID] = true - queryBlockIDs = append(queryBlockIDs, ref.DefBlockID) - queryBlockIDs = append(queryBlockIDs, ref.BlockID) - } - querySQLBlocks := sql.GetBlocks(queryBlockIDs) - defSQLBlocksCache := map[string]*sql.Block{} - for _, defSQLBlock := range querySQLBlocks { - if nil != defSQLBlock && defSQLBlockIDs[defSQLBlock.ID] { - defSQLBlocksCache[defSQLBlock.ID] = defSQLBlock - } - } - refSQLBlocksCache := map[string]*sql.Block{} - for _, refSQLBlock := range querySQLBlocks { - if nil != refSQLBlock && refSQLBlockIDs[refSQLBlock.ID] { - refSQLBlocksCache[refSQLBlock.ID] = refSQLBlock - } - } - - var links []*Block - excludeBacklinkIDs = hashset.New() - for _, ref := range refs { - defSQLBlock := defSQLBlocksCache[(ref.DefBlockID)] - if nil == defSQLBlock { - continue - } - - refSQLBlock := refSQLBlocksCache[ref.BlockID] - if nil == refSQLBlock { - continue - } - refBlock := fromSQLBlock(refSQLBlock, "", 12) - if defRootID == refBlock.RootID { // 排除当前文档内引用提及 - excludeBacklinkIDs.Add(refBlock.RootID, refBlock.ID) - } - defBlock := fromSQLBlock(defSQLBlock, "", 12) - if defBlock.RootID == defRootID { // 当前文档的定义块 - links = append(links, defBlock) - if ref.DefBlockID == defBlock.ID { - defBlock.Refs = append(defBlock.Refs, refBlock) - } - } - } - - for _, link := range links { - for _, ref := range link.Refs { - excludeBacklinkIDs.Add(ref.RootID, ref.ID) - } - refsCount += len(link.Refs) - } - - processedParagraphs := hashset.New() - var paragraphParentIDs []string - for _, link := range links { - for _, ref := range link.Refs { - if "NodeParagraph" == ref.Type { - paragraphParentIDs = append(paragraphParentIDs, ref.ParentID) - } - } - } - paragraphParents := sql.GetBlocks(paragraphParentIDs) - for _, p := range paragraphParents { - if "i" == p.Type || "h" == p.Type { - ret = append(ret, fromSQLBlock(p, "", 12)) - processedParagraphs.Add(p.ID) - } - } - for _, link := range links { - for _, ref := range link.Refs { - if "NodeParagraph" == ref.Type { - if processedParagraphs.Contains(ref.ParentID) { - continue - } - } - - ref.DefID = link.ID - ref.DefPath = link.Path - ret = append(ret, ref) - } - } - return -} - func buildBacklink(refID string, refTree *parse.Tree, luteEngine *lute.Lute) (ret *Backlink) { n := treenode.GetNodeInTree(refTree, refID) if nil == n { @@ -412,6 +324,94 @@ func BuildTreeBacklink(id, keyword, mentionKeyword string, beforeLen int) (boxID return } +func buildLinkRefs(defRootID string, refs []*sql.Ref) (ret []*Block, refsCount int, excludeBacklinkIDs *hashset.Set) { + // 为了减少查询,组装好 IDs 后一次查出 + defSQLBlockIDs, refSQLBlockIDs := map[string]bool{}, map[string]bool{} + var queryBlockIDs []string + for _, ref := range refs { + defSQLBlockIDs[ref.DefBlockID] = true + refSQLBlockIDs[ref.BlockID] = true + queryBlockIDs = append(queryBlockIDs, ref.DefBlockID) + queryBlockIDs = append(queryBlockIDs, ref.BlockID) + } + querySQLBlocks := sql.GetBlocks(queryBlockIDs) + defSQLBlocksCache := map[string]*sql.Block{} + for _, defSQLBlock := range querySQLBlocks { + if nil != defSQLBlock && defSQLBlockIDs[defSQLBlock.ID] { + defSQLBlocksCache[defSQLBlock.ID] = defSQLBlock + } + } + refSQLBlocksCache := map[string]*sql.Block{} + for _, refSQLBlock := range querySQLBlocks { + if nil != refSQLBlock && refSQLBlockIDs[refSQLBlock.ID] { + refSQLBlocksCache[refSQLBlock.ID] = refSQLBlock + } + } + + var links []*Block + excludeBacklinkIDs = hashset.New() + for _, ref := range refs { + defSQLBlock := defSQLBlocksCache[(ref.DefBlockID)] + if nil == defSQLBlock { + continue + } + + refSQLBlock := refSQLBlocksCache[ref.BlockID] + if nil == refSQLBlock { + continue + } + refBlock := fromSQLBlock(refSQLBlock, "", 12) + if defRootID == refBlock.RootID { // 排除当前文档内引用提及 + excludeBacklinkIDs.Add(refBlock.RootID, refBlock.ID) + } + defBlock := fromSQLBlock(defSQLBlock, "", 12) + if defBlock.RootID == defRootID { // 当前文档的定义块 + links = append(links, defBlock) + if ref.DefBlockID == defBlock.ID { + defBlock.Refs = append(defBlock.Refs, refBlock) + } + } + } + + for _, link := range links { + for _, ref := range link.Refs { + excludeBacklinkIDs.Add(ref.RootID, ref.ID) + } + refsCount += len(link.Refs) + } + + processedParagraphs := hashset.New() + var paragraphParentIDs []string + for _, link := range links { + for _, ref := range link.Refs { + if "NodeParagraph" == ref.Type { + paragraphParentIDs = append(paragraphParentIDs, ref.ParentID) + } + } + } + paragraphParents := sql.GetBlocks(paragraphParentIDs) + for _, p := range paragraphParents { + if "i" == p.Type || "h" == p.Type { + ret = append(ret, fromSQLBlock(p, "", 12)) + processedParagraphs.Add(p.ID) + } + } + for _, link := range links { + for _, ref := range link.Refs { + if "NodeParagraph" == ref.Type { + if processedParagraphs.Contains(ref.ParentID) { + continue + } + } + + ref.DefID = link.ID + ref.DefPath = link.Path + ret = append(ret, ref) + } + } + return +} + func removeDuplicatedRefs(refs []*sql.Ref) (ret []*sql.Ref) { for _, ref := range refs { contain := false From 17595a3076fdb879dc4b7c8efd373b31bb11217b Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 2 Oct 2022 12:00:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:sparkles:=20=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E5=BC=8F=E5=8F=8D=E9=93=BE=E9=9D=A2=E6=9D=BF=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/3565?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/backlink.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index 27b8b4361..8586aa349 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -298,6 +298,8 @@ func buildBacklink(refID string, refTree *parse.Tree, luteEngine *lute.Lute) (re } func BuildTreeBacklink(id, keyword, mentionKeyword string, beforeLen int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) { + keyword = strings.TrimSpace(keyword) + mentionKeyword = strings.TrimSpace(mentionKeyword) backlinks, backmentions = []*Path{}, []*Path{} sqlBlock := sql.GetBlock(id) @@ -310,15 +312,27 @@ func BuildTreeBacklink(id, keyword, mentionKeyword string, beforeLen int) (boxID refs = removeDuplicatedRefs(refs) // 同一个块中引用多个相同块时反链去重 https://github.com/siyuan-note/siyuan/issues/3317 linkRefs, linkRefsCount, excludeBacklinkIDs := buildLinkRefs(id, refs) - backlinks = toFlatTree(linkRefs, 0, "backlink") - for _, l := range backlinks { + tmpBacklinks := toFlatTree(linkRefs, 0, "backlink") + for _, l := range tmpBacklinks { l.Blocks = nil + if "" != keyword { + if !strings.Contains(l.Name, keyword) { + continue + } + } + backlinks = append(backlinks, l) } mentionRefs := buildTreeBackmention(sqlBlock, linkRefs, mentionKeyword, excludeBacklinkIDs, beforeLen) - backmentions = toFlatTree(mentionRefs, 0, "backlink") - for _, l := range backmentions { + tmpBackmentions := toFlatTree(mentionRefs, 0, "backlink") + for _, l := range tmpBackmentions { l.Blocks = nil + if "" != mentionKeyword { + if !strings.Contains(l.Name, mentionKeyword) { + continue + } + } + backmentions = append(backmentions, l) } mentionsCount = len(backmentions) return From efefa9baafb9008adb58508ed776f00be77cf8f8 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 2 Oct 2022 12:01:17 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:sparkles:=20=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E5=BC=8F=E5=8F=8D=E9=93=BE=E9=9D=A2=E6=9D=BF=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/3565?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/ref.go | 2 +- kernel/model/backlink.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/api/ref.go b/kernel/api/ref.go index 02b0ee37b..29ca48289 100644 --- a/kernel/api/ref.go +++ b/kernel/api/ref.go @@ -90,7 +90,7 @@ func getBacklink(c *gin.Context) { keyword := arg["k"].(string) mentionKeyword := arg["mk"].(string) beforeLen := arg["beforeLen"].(float64) - boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.BuildTreeBacklink(id, keyword, mentionKeyword, int(beforeLen)) + boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink(id, keyword, mentionKeyword, int(beforeLen)) ret.Data = map[string]interface{}{ "backlinks": backlinks, "linkRefsCount": linkRefsCount, diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index 8586aa349..65013e3d0 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -297,7 +297,7 @@ func buildBacklink(refID string, refTree *parse.Tree, luteEngine *lute.Lute) (re return } -func BuildTreeBacklink(id, keyword, mentionKeyword string, beforeLen int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) { +func GetBacklink(id, keyword, mentionKeyword string, beforeLen int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) { keyword = strings.TrimSpace(keyword) mentionKeyword = strings.TrimSpace(mentionKeyword) backlinks, backmentions = []*Path{}, []*Path{}