From 8a6733e5b5ae382da831f0bb5efe2e1ef969bff6 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 20 Nov 2024 15:57:04 +0800 Subject: [PATCH] :bug: Incorrect backlink keyword highlighting https://github.com/siyuan-note/siyuan/issues/13210 --- kernel/model/backlink.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index e5136a2b8..d88829780 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -63,7 +63,11 @@ type Backlink struct { } func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) { + var keywords []string keyword = strings.TrimSpace(keyword) + if "" != keyword { + keywords = strings.Split(keyword, " ") + } ret = []*Backlink{} beforeLen := 12 sqlBlock := sql.GetBlock(defID) @@ -75,7 +79,7 @@ func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) ( refs := sql.QueryRefsByDefID(defID, containChildren) refs = removeDuplicatedRefs(refs) - linkRefs, _, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keyword) + linkRefs, _, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keywords) tmpMentions, mentionKeywords := buildTreeBackmention(sqlBlock, linkRefs, keyword, excludeBacklinkIDs, beforeLen) luteEngine := util.NewLute() var mentions []*Block @@ -113,7 +117,12 @@ func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) ( } func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) { + var keywords []string keyword = strings.TrimSpace(keyword) + if "" != keyword { + keywords = strings.Split(keyword, " ") + } + ret = []*Backlink{} sqlBlock := sql.GetBlock(defID) if nil == sqlBlock { @@ -130,7 +139,7 @@ func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret } refs = removeDuplicatedRefs(refs) - linkRefs, _, _ := buildLinkRefs(rootID, refs, keyword) + linkRefs, _, _ := buildLinkRefs(rootID, refs, keywords) refTree, err := LoadTreeByBlockID(refTreeID) if err != nil { logging.LogWarnf("load ref tree [%s] failed: %s", refTreeID, err) @@ -139,7 +148,6 @@ func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret luteEngine := util.NewLute() for _, linkRef := range linkRefs { - keywords := strings.Split(keyword, " ") backlink := buildBacklink(linkRef.ID, refTree, keywords, luteEngine) if nil != backlink { ret = append(ret, backlink) @@ -276,6 +284,10 @@ func getBacklinkRenderNodes(n *ast.Node) (ret []*ast.Node, expand bool) { func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode int, containChildren bool) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) { keyword = strings.TrimSpace(keyword) + var keywords []string + if "" != keyword { + keywords = strings.Split(keyword, " ") + } mentionKeyword = strings.TrimSpace(mentionKeyword) backlinks, backmentions = []*Path{}, []*Path{} @@ -289,7 +301,7 @@ func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode refs := sql.QueryRefsByDefID(id, containChildren) refs = removeDuplicatedRefs(refs) - linkRefs, linkRefsCount, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keyword) + linkRefs, linkRefsCount, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keywords) tmpBacklinks := toFlatTree(linkRefs, 0, "backlink", nil) for _, l := range tmpBacklinks { l.Blocks = nil @@ -485,7 +497,7 @@ func GetBacklink(id, keyword, mentionKeyword string, beforeLen int, containChild return } -func buildLinkRefs(defRootID string, refs []*sql.Ref, keyword string) (ret []*Block, refsCount int, excludeBacklinkIDs *hashset.Set) { +func buildLinkRefs(defRootID string, refs []*sql.Ref, keywords []string) (ret []*Block, refsCount int, excludeBacklinkIDs *hashset.Set) { // 为了减少查询,组装好 IDs 后一次查出 defSQLBlockIDs, refSQLBlockIDs := map[string]bool{}, map[string]bool{} var queryBlockIDs []string @@ -566,7 +578,7 @@ func buildLinkRefs(defRootID string, refs []*sql.Ref, keyword string) (ret []*Bl if nil != refBlock && p.FContent == refBlock.Content { // 使用内容判断是否是列表项下第一个子块 // 如果是列表项下第一个子块,则后续会通过列表项传递或关联处理,所以这里就不处理这个段落了 processedParagraphs.Add(p.ID) - if !matchBacklinkKeyword(p, keyword) { + if !matchBacklinkKeyword(p, keywords) { refsCount-- continue } @@ -582,7 +594,7 @@ func buildLinkRefs(defRootID string, refs []*sql.Ref, keyword string) (ret []*Bl } } - if !matchBacklinkKeyword(ref, keyword) { + if !matchBacklinkKeyword(ref, keywords) { refsCount-- continue } @@ -595,8 +607,11 @@ func buildLinkRefs(defRootID string, refs []*sql.Ref, keyword string) (ret []*Bl return } -func matchBacklinkKeyword(block *Block, keyword string) bool { - keywords := strings.Split(keyword, " ") +func matchBacklinkKeyword(block *Block, keywords []string) bool { + if 1 > len(keywords) { + return true + } + for _, k := range keywords { k = strings.ToLower(k) if strings.Contains(strings.ToLower(block.FContent), k) ||