🐛 Incorrect backlink keyword highlighting https://github.com/siyuan-note/siyuan/issues/13210

This commit is contained in:
Daniel 2024-11-20 15:57:04 +08:00
parent c62015da38
commit 8a6733e5b5
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -63,7 +63,11 @@ type Backlink struct {
} }
func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) { func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) {
var keywords []string
keyword = strings.TrimSpace(keyword) keyword = strings.TrimSpace(keyword)
if "" != keyword {
keywords = strings.Split(keyword, " ")
}
ret = []*Backlink{} ret = []*Backlink{}
beforeLen := 12 beforeLen := 12
sqlBlock := sql.GetBlock(defID) sqlBlock := sql.GetBlock(defID)
@ -75,7 +79,7 @@ func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (
refs := sql.QueryRefsByDefID(defID, containChildren) refs := sql.QueryRefsByDefID(defID, containChildren)
refs = removeDuplicatedRefs(refs) refs = removeDuplicatedRefs(refs)
linkRefs, _, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keyword) linkRefs, _, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keywords)
tmpMentions, mentionKeywords := buildTreeBackmention(sqlBlock, linkRefs, keyword, excludeBacklinkIDs, beforeLen) tmpMentions, mentionKeywords := buildTreeBackmention(sqlBlock, linkRefs, keyword, excludeBacklinkIDs, beforeLen)
luteEngine := util.NewLute() luteEngine := util.NewLute()
var mentions []*Block 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) { func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) {
var keywords []string
keyword = strings.TrimSpace(keyword) keyword = strings.TrimSpace(keyword)
if "" != keyword {
keywords = strings.Split(keyword, " ")
}
ret = []*Backlink{} ret = []*Backlink{}
sqlBlock := sql.GetBlock(defID) sqlBlock := sql.GetBlock(defID)
if nil == sqlBlock { if nil == sqlBlock {
@ -130,7 +139,7 @@ func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret
} }
refs = removeDuplicatedRefs(refs) refs = removeDuplicatedRefs(refs)
linkRefs, _, _ := buildLinkRefs(rootID, refs, keyword) linkRefs, _, _ := buildLinkRefs(rootID, refs, keywords)
refTree, err := LoadTreeByBlockID(refTreeID) refTree, err := LoadTreeByBlockID(refTreeID)
if err != nil { if err != nil {
logging.LogWarnf("load ref tree [%s] failed: %s", refTreeID, err) 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() luteEngine := util.NewLute()
for _, linkRef := range linkRefs { for _, linkRef := range linkRefs {
keywords := strings.Split(keyword, " ")
backlink := buildBacklink(linkRef.ID, refTree, keywords, luteEngine) backlink := buildBacklink(linkRef.ID, refTree, keywords, luteEngine)
if nil != backlink { if nil != backlink {
ret = append(ret, 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) { func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode int, containChildren bool) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) {
keyword = strings.TrimSpace(keyword) keyword = strings.TrimSpace(keyword)
var keywords []string
if "" != keyword {
keywords = strings.Split(keyword, " ")
}
mentionKeyword = strings.TrimSpace(mentionKeyword) mentionKeyword = strings.TrimSpace(mentionKeyword)
backlinks, backmentions = []*Path{}, []*Path{} backlinks, backmentions = []*Path{}, []*Path{}
@ -289,7 +301,7 @@ func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode
refs := sql.QueryRefsByDefID(id, containChildren) refs := sql.QueryRefsByDefID(id, containChildren)
refs = removeDuplicatedRefs(refs) refs = removeDuplicatedRefs(refs)
linkRefs, linkRefsCount, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keyword) linkRefs, linkRefsCount, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keywords)
tmpBacklinks := toFlatTree(linkRefs, 0, "backlink", nil) tmpBacklinks := toFlatTree(linkRefs, 0, "backlink", nil)
for _, l := range tmpBacklinks { for _, l := range tmpBacklinks {
l.Blocks = nil l.Blocks = nil
@ -485,7 +497,7 @@ func GetBacklink(id, keyword, mentionKeyword string, beforeLen int, containChild
return 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 后一次查出 // 为了减少查询,组装好 IDs 后一次查出
defSQLBlockIDs, refSQLBlockIDs := map[string]bool{}, map[string]bool{} defSQLBlockIDs, refSQLBlockIDs := map[string]bool{}, map[string]bool{}
var queryBlockIDs []string 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 { // 使用内容判断是否是列表项下第一个子块 if nil != refBlock && p.FContent == refBlock.Content { // 使用内容判断是否是列表项下第一个子块
// 如果是列表项下第一个子块,则后续会通过列表项传递或关联处理,所以这里就不处理这个段落了 // 如果是列表项下第一个子块,则后续会通过列表项传递或关联处理,所以这里就不处理这个段落了
processedParagraphs.Add(p.ID) processedParagraphs.Add(p.ID)
if !matchBacklinkKeyword(p, keyword) { if !matchBacklinkKeyword(p, keywords) {
refsCount-- refsCount--
continue 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-- refsCount--
continue continue
} }
@ -595,8 +607,11 @@ func buildLinkRefs(defRootID string, refs []*sql.Ref, keyword string) (ret []*Bl
return return
} }
func matchBacklinkKeyword(block *Block, keyword string) bool { func matchBacklinkKeyword(block *Block, keywords []string) bool {
keywords := strings.Split(keyword, " ") if 1 > len(keywords) {
return true
}
for _, k := range keywords { for _, k := range keywords {
k = strings.ToLower(k) k = strings.ToLower(k)
if strings.Contains(strings.ToLower(block.FContent), k) || if strings.Contains(strings.ToLower(block.FContent), k) ||