mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-18 15:40:12 +01:00
🎨 搜索结果按文档分组时按块在文档中的先后排序 Fix https://github.com/siyuan-note/siyuan/issues/6749
This commit is contained in:
parent
4f25021884
commit
3e2bddf5d8
2 changed files with 22 additions and 0 deletions
|
|
@ -55,6 +55,7 @@ type Block struct {
|
||||||
Children []*Block `json:"children"`
|
Children []*Block `json:"children"`
|
||||||
Depth int `json:"depth"`
|
Depth int `json:"depth"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
|
Sort int `json:"sort"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) IsContainerBlock() bool {
|
func (block *Block) IsContainerBlock() bool {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -344,20 +345,39 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b
|
||||||
case 1: // 按文档分组
|
case 1: // 按文档分组
|
||||||
rootMap := map[string]bool{}
|
rootMap := map[string]bool{}
|
||||||
var rootIDs []string
|
var rootIDs []string
|
||||||
|
sorts := map[string]int{}
|
||||||
for _, b := range blocks {
|
for _, b := range blocks {
|
||||||
if _, ok := rootMap[b.RootID]; !ok {
|
if _, ok := rootMap[b.RootID]; !ok {
|
||||||
rootMap[b.RootID] = true
|
rootMap[b.RootID] = true
|
||||||
rootIDs = append(rootIDs, b.RootID)
|
rootIDs = append(rootIDs, b.RootID)
|
||||||
|
tree, _ := loadTreeByBlockID(b.RootID)
|
||||||
|
if nil == tree {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
sort := 0
|
||||||
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
|
if !entering || !n.IsBlock() {
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
sorts[n.ID] = sort
|
||||||
|
sort++
|
||||||
|
return ast.WalkContinue
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlRoots := sql.GetBlocks(rootIDs)
|
sqlRoots := sql.GetBlocks(rootIDs)
|
||||||
roots := fromSQLBlocks(&sqlRoots, "", beforeLen)
|
roots := fromSQLBlocks(&sqlRoots, "", beforeLen)
|
||||||
for _, root := range roots {
|
for _, root := range roots {
|
||||||
for _, b := range blocks {
|
for _, b := range blocks {
|
||||||
|
b.Sort = sorts[b.ID]
|
||||||
if b.RootID == root.ID {
|
if b.RootID == root.ID {
|
||||||
root.Children = append(root.Children, b)
|
root.Children = append(root.Children, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sort.Slice(root.Children, func(i, j int) bool { return root.Children[i].Sort < root.Children[j].Sort })
|
||||||
}
|
}
|
||||||
ret = roots
|
ret = roots
|
||||||
default:
|
default:
|
||||||
|
|
@ -729,6 +749,7 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc
|
||||||
Markdown: markdown,
|
Markdown: markdown,
|
||||||
Type: treenode.FromAbbrType(sqlBlock.Type),
|
Type: treenode.FromAbbrType(sqlBlock.Type),
|
||||||
SubType: sqlBlock.SubType,
|
SubType: sqlBlock.SubType,
|
||||||
|
Sort: sqlBlock.Sort,
|
||||||
}
|
}
|
||||||
if "" != sqlBlock.IAL {
|
if "" != sqlBlock.IAL {
|
||||||
block.IAL = map[string]string{}
|
block.IAL = map[string]string{}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue