🎨 Highlight regular expression search results https://github.com/siyuan-note/siyuan/issues/11112

This commit is contained in:
Daniel 2024-09-21 17:29:01 +08:00
parent 783c8a092b
commit eb84c1f90f
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 77 additions and 5 deletions

View file

@ -20,6 +20,7 @@ import (
"bytes"
"database/sql"
"math"
"regexp"
"sort"
"strconv"
"strings"
@ -619,6 +620,57 @@ func SelectBlocksRawStmt(stmt string, page, limit int) (ret []*Block) {
return
}
func SelectBlocksRegex(stmt string, exp *regexp.Regexp, name, alias, memo, ial bool, page, pageSize int) (ret []*Block) {
rows, err := query(stmt)
if err != nil {
logging.LogErrorf("sql query [%s] failed: %s", stmt, err)
return
}
defer rows.Close()
count := 0
for rows.Next() {
count++
if count <= (page-1)*pageSize {
continue
}
var block Block
if err := rows.Scan(&block.ID, &block.ParentID, &block.RootID, &block.Hash, &block.Box, &block.Path, &block.HPath, &block.Name, &block.Alias, &block.Memo, &block.Tag, &block.Content, &block.FContent, &block.Markdown, &block.Length, &block.Type, &block.SubType, &block.IAL, &block.Sort, &block.Created, &block.Updated); err != nil {
logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
return
}
hitContent := exp.MatchString(block.Content)
hitName := name && exp.MatchString(block.Name)
hitAlias := alias && exp.MatchString(block.Alias)
hitMemo := memo && exp.MatchString(block.Memo)
hitIAL := ial && exp.MatchString(block.IAL)
if hitContent || hitName || hitAlias || hitMemo || hitIAL {
if hitContent {
block.Content = exp.ReplaceAllString(block.Content, "__@mark__${0}__mark@__")
}
if hitName {
block.Name = exp.ReplaceAllString(block.Name, "__@mark__${0}__mark@__")
}
if hitAlias {
block.Alias = exp.ReplaceAllString(block.Alias, "__@mark__${0}__mark@__")
}
if hitMemo {
block.Memo = exp.ReplaceAllString(block.Memo, "__@mark__${0}__mark@__")
}
if hitIAL {
block.IAL = exp.ReplaceAllString(block.IAL, "__@mark__${0}__mark@__")
}
ret = append(ret, &block)
if len(ret) >= pageSize {
break
}
}
}
return
}
func selectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
rows, err := query(stmt)
if err != nil {