From d0d5193358a996353938d24116d4b58045a80426 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 12 Jan 2025 00:08:56 +0800 Subject: [PATCH] :art: Improve list item, super block and blockquote backlink propagation https://github.com/siyuan-note/siyuan/issues/13776 --- kernel/model/backlink.go | 49 +++++++++++++----------- kernel/model/blockinfo.go | 80 +++++++++++++++++++++++++++++++-------- 2 files changed, 91 insertions(+), 38 deletions(-) diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index c69945c6f..534c2de82 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -593,36 +593,39 @@ func buildLinkRefs(defRootID string, refs []*sql.Ref, keywords []string) (ret [] processedParagraphs := hashset.New() for _, parent := range paragraphParents { if "NodeListItem" == parent.Type || "NodeBlockquote" == parent.Type || "NodeSuperBlock" == parent.Type { - paragraphUseParentLi := true - if refBlock := parentRefParagraphs[parent.ID]; nil != refBlock { - if "NodeListItem" == parent.Type && parent.FContent != refBlock.Content { - if inlineTree := parse.Inline("", []byte(refBlock.Markdown), luteEngine.ParseOptions); nil != inlineTree { - for c := inlineTree.Root.FirstChild.FirstChild; c != nil; c = c.Next { - if treenode.IsBlockRef(c) { - continue - } + refBlock := parentRefParagraphs[parent.ID] + if nil == refBlock { + continue + } - if "" != strings.TrimSpace(c.Text()) { - paragraphUseParentLi = false - break - } + paragraphUseParentLi := true + if "NodeListItem" == parent.Type && parent.FContent != refBlock.Content { + if inlineTree := parse.Inline("", []byte(refBlock.Markdown), luteEngine.ParseOptions); nil != inlineTree { + for c := inlineTree.Root.FirstChild.FirstChild; c != nil; c = c.Next { + if treenode.IsBlockRef(c) { + continue + } + + if "" != strings.TrimSpace(c.Text()) { + paragraphUseParentLi = false + break } } } + } - if paragraphUseParentLi { - processedParagraphs.Add(parent.ID) - } + if paragraphUseParentLi { + processedParagraphs.Add(parent.ID) + } - originalRefBlockIDs[parent.ID] = refBlock.ID - if !matchBacklinkKeyword(parent, keywords) { - refsCount-- - continue - } + originalRefBlockIDs[parent.ID] = refBlock.ID + if !matchBacklinkKeyword(parent, keywords) { + refsCount-- + continue + } - if paragraphUseParentLi { - ret = append(ret, parent) - } + if paragraphUseParentLi { + ret = append(ret, parent) } } } diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go index ab092852a..222488117 100644 --- a/kernel/model/blockinfo.go +++ b/kernel/model/blockinfo.go @@ -17,6 +17,7 @@ package model import ( + "github.com/emirpasic/gods/sets/hashset" "os" "path/filepath" "sort" @@ -90,8 +91,8 @@ func GetDocInfo(blockID string) (ret *BlockInfo) { } } - ret.RefIDs, _ = sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) - buildBacklinkListItemRefs(&ret.RefIDs, &map[string]string{}) + refIDs, _ := sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) + ret.RefIDs, _ = buildBacklinkListItemRefs(refIDs) ret.RefCount = len(ret.RefIDs) // 填充块引计数 // 填充属性视图角标 Display the database title on the block superscript https://github.com/siyuan-note/siyuan/issues/10545 @@ -339,7 +340,7 @@ func GetBlockRefs(defID string, isBacklink bool) (refIDs, refTexts, defIDs []str } if isBacklink { - buildBacklinkListItemRefs(&refIDs, &originalRefIDs) + refIDs, originalRefIDs = buildBacklinkListItemRefs(refIDs) } return } @@ -558,19 +559,68 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, isEmbedBlock bo return } -func buildBacklinkListItemRefs(refIDs *[]string, originalRefIDs *map[string]string) { - refBts := treenode.GetBlockTrees(*refIDs) - for i, refID := range *refIDs { - bt := refBts[refID] - if nil == bt || "p" != bt.Type { - continue - } +func buildBacklinkListItemRefs(refIDs []string) (retRefIDs []string, originalRefBlockIDs map[string]string) { + retRefIDs = []string{} + originalRefBlockIDs = map[string]string{} - if parent := treenode.GetBlockTree(bt.ParentID); nil != parent && - ("i" == parent.Type || "b" == parent.Type || "s" == parent.Type) { - // 引用计数浮窗请求,需要按照反链逻辑组装 https://github.com/siyuan-note/siyuan/issues/6853 - (*refIDs)[i] = parent.ID - (*originalRefIDs)[parent.ID] = refID + sqlRefBlocks := sql.GetBlocks(refIDs) + refBlocks := fromSQLBlocks(&sqlRefBlocks, "", 12) + + parentRefParagraphs := map[string]*Block{} + for _, ref := range refBlocks { + if nil != ref && "NodeParagraph" == ref.Type { + parentRefParagraphs[ref.ParentID] = ref } } + + var paragraphParentIDs []string + for parentID := range parentRefParagraphs { + paragraphParentIDs = append(paragraphParentIDs, parentID) + } + sqlParagraphParents := sql.GetBlocks(paragraphParentIDs) + paragraphParents := fromSQLBlocks(&sqlParagraphParents, "", 12) + + luteEngine := util.NewLute() + processedParagraphs := hashset.New() + for _, parent := range paragraphParents { + if "NodeListItem" == parent.Type || "NodeBlockquote" == parent.Type || "NodeSuperBlock" == parent.Type { + refBlock := parentRefParagraphs[parent.ID] + if nil == refBlock { + continue + } + + paragraphUseParentLi := true + if "NodeListItem" == parent.Type && parent.FContent != refBlock.Content { + if inlineTree := parse.Inline("", []byte(refBlock.Markdown), luteEngine.ParseOptions); nil != inlineTree { + for c := inlineTree.Root.FirstChild.FirstChild; c != nil; c = c.Next { + if treenode.IsBlockRef(c) { + continue + } + + if "" != strings.TrimSpace(c.Text()) { + paragraphUseParentLi = false + break + } + } + } + } + + if paragraphUseParentLi { + retRefIDs = append(retRefIDs, parent.ID) + processedParagraphs.Add(parent.ID) + } else { + retRefIDs = append(retRefIDs, refBlock.ID) + } + + originalRefBlockIDs[parent.ID] = refBlock.ID + } + } + + for _, ref := range refBlocks { + if !processedParagraphs.Contains(ref.ParentID) { + retRefIDs = append(retRefIDs, ref.ID) + } + } + retRefIDs = gulu.Str.RemoveDuplicatedElem(retRefIDs) + return }