From 3e2bddf5d8bb7b81c0e7027413beb130a5dc8d45 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 30 Nov 2022 12:11:49 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E6=8C=89=E6=96=87=E6=A1=A3=E5=88=86=E7=BB=84=E6=97=B6=E6=8C=89?= =?UTF-8?q?=E5=9D=97=E5=9C=A8=E6=96=87=E6=A1=A3=E4=B8=AD=E7=9A=84=E5=85=88?= =?UTF-8?q?=E5=90=8E=E6=8E=92=E5=BA=8F=20Fix=20https://github.com/siyuan-n?= =?UTF-8?q?ote/siyuan/issues/6749?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/block.go | 1 + kernel/model/search.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/kernel/model/block.go b/kernel/model/block.go index 0d805e647..b8b8b2124 100644 --- a/kernel/model/block.go +++ b/kernel/model/block.go @@ -55,6 +55,7 @@ type Block struct { Children []*Block `json:"children"` Depth int `json:"depth"` Count int `json:"count"` + Sort int `json:"sort"` } func (block *Block) IsContainerBlock() bool { diff --git a/kernel/model/search.go b/kernel/model/search.go index 33a3ee1b6..f60132beb 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -22,6 +22,7 @@ import ( "fmt" "path" "regexp" + "sort" "strconv" "strings" "time" @@ -344,20 +345,39 @@ func FullTextSearchBlock(query string, boxes, paths []string, types map[string]b case 1: // 按文档分组 rootMap := map[string]bool{} var rootIDs []string + sorts := map[string]int{} for _, b := range blocks { if _, ok := rootMap[b.RootID]; !ok { rootMap[b.RootID] = true 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) roots := fromSQLBlocks(&sqlRoots, "", beforeLen) for _, root := range roots { for _, b := range blocks { + b.Sort = sorts[b.ID] if b.RootID == root.ID { 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 default: @@ -729,6 +749,7 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc Markdown: markdown, Type: treenode.FromAbbrType(sqlBlock.Type), SubType: sqlBlock.SubType, + Sort: sqlBlock.Sort, } if "" != sqlBlock.IAL { block.IAL = map[string]string{}