mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 Highlight regular expression search results https://github.com/siyuan-note/siyuan/issues/11112
This commit is contained in:
parent
783c8a092b
commit
eb84c1f90f
3 changed files with 77 additions and 5 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue