🎨 Improve the backlink panel breadcrumb and block sorting https://github.com/siyuan-note/siyuan/issues/13008

This commit is contained in:
Daniel 2024-11-03 11:04:23 +08:00
parent 46fd653144
commit 3dbffe00c9
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 39 additions and 2 deletions

View file

@ -58,6 +58,8 @@ type Backlink struct {
DOM string `json:"dom"`
BlockPaths []*BlockPath `json:"blockPaths"`
Expand bool `json:"expand"`
node *ast.Node // 仅用于按文档内容顺序排序
}
func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) {
@ -93,12 +95,20 @@ func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (
}
mentionKeywords = gulu.Str.RemoveDuplicatedElem(mentionKeywords)
var refTree *parse.Tree
trees := filesys.LoadTrees(mentionBlockIDs)
for id, tree := range trees {
backlink := buildBacklink(id, tree, mentionKeywords, luteEngine)
if nil != backlink {
ret = append(ret, backlink)
}
if nil != tree && nil == refTree {
refTree = tree
}
}
if 0 < len(trees) {
sortBacklinks(ret, refTree)
}
return
}
@ -139,9 +149,31 @@ func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret
ret = append(ret, backlink)
}
}
sortBacklinks(ret, refTree)
return
}
func sortBacklinks(backlinks []*Backlink, tree *parse.Tree) {
contentSorts := map[string]int{}
sortVal := 0
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || !n.IsBlock() {
return ast.WalkContinue
}
contentSorts[n.ID] = sortVal
sortVal++
return ast.WalkContinue
})
sort.Slice(backlinks, func(i, j int) bool {
s1 := contentSorts[backlinks[i].node.ID]
s2 := contentSorts[backlinks[j].node.ID]
return s1 < s2
})
}
func buildBacklink(refID string, refTree *parse.Tree, keywords []string, luteEngine *lute.Lute) (ret *Backlink) {
n := treenode.GetNodeInTree(refTree, refID)
if nil == n {
@ -175,12 +207,12 @@ func buildBacklink(refID string, refTree *parse.Tree, keywords []string, luteEng
dom := renderBlockDOMByNodes(renderNodes, luteEngine)
blockPaths := []*BlockPath{}
if nil != n.Parent && ast.NodeDocument != n.Parent.Type && nil != n.Parent.Parent && ast.NodeDocument != n.Parent.Parent.Type {
if (nil != n.Parent && ast.NodeDocument != n.Parent.Type && nil != n.Parent.Parent && ast.NodeDocument != n.Parent.Parent.Type) || nil != treenode.HeadingParent(n) {
// 仅在多于一层时才显示面包屑,这样界面展示更加简洁
// The backlink panel no longer displays breadcrumbs of the first-level blocks https://github.com/siyuan-note/siyuan/issues/12862
blockPaths = buildBlockBreadcrumb(n, nil, false)
}
ret = &Backlink{DOM: dom, BlockPaths: blockPaths, Expand: expand}
ret = &Backlink{DOM: dom, BlockPaths: blockPaths, Expand: expand, node: n}
return
}

View file

@ -507,6 +507,11 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, displayCurrentN
}
if ast.NodeHeading == b.Type && headingLevel > b.HeadingLevel {
if b.ParentIs(ast.NodeListItem) {
// 标题在列表下时不显示 https://github.com/siyuan-note/siyuan/issues/13008
continue
}
name = gulu.Str.SubStr(renderBlockText(b, excludeTypes), maxNameLen)
name = util.EscapeHTML(name)
ret = append([]*BlockPath{{