This commit is contained in:
Liang Ding 2022-10-01 15:59:57 +08:00
parent e250a4b53c
commit 66f27afcc2
No known key found for this signature in database
GPG key ID: 136F30F901A2231D

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.LogErrorf("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.LogErrorf("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,34 +311,23 @@ 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)
} }
} }
luteEngine := NewLute()
refTree, err := loadTreeByBlockID(refTreeID)
if nil != err {
logging.LogErrorf("load ref tree [%s] failed: %s", refTreeID, err)
return return
} }
for _, linkRef := range linkRefs { func buildBacklink(refID string, refTree *parse.Tree, luteEngine *lute.Lute) (ret *Backlink) {
n := treenode.GetNodeInTree(refTree, linkRef.ID) n := treenode.GetNodeInTree(refTree, refID)
if nil == n { if nil == n {
continue return
} }
var renderNodes []*ast.Node var renderNodes []*ast.Node
expand := true expand := true
if ast.NodeListItem == n.Type { if ast.NodeListItem == n.Type {
if nil == n.FirstChild { if nil == n.FirstChild {
continue return
} }
c := n.FirstChild c := n.FirstChild
@ -312,7 +349,7 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
} else if ast.NodeHeading == n.Type { } else if ast.NodeHeading == n.Type {
c := n.FirstChild c := n.FirstChild
if nil == c { if nil == c {
continue return
} }
for headingFirstSpan := c; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next { for headingFirstSpan := c; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
@ -333,13 +370,11 @@ func GetBacklinkDoc(defID, refTreeID string) (ret []*Backlink) {
} }
dom := renderBlockDOMByNodes(renderNodes, luteEngine) dom := renderBlockDOMByNodes(renderNodes, luteEngine)
ret = append(ret, &Backlink{ ret = &Backlink{
DOM: dom, DOM: dom,
BlockPaths: buildBlockBreadcrumb(n), BlockPaths: buildBlockBreadcrumb(n),
Expand: expand, Expand: expand,
})
} }
return return
} }