mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-04 01:59:05 +08:00
♻️ 搜索命中高亮
This commit is contained in:
parent
c56dcead19
commit
7d05f4e8c7
@ -915,9 +915,9 @@ func stringQuery(query string) string {
|
|||||||
func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markSpanDataType string, luteEngine *lute.Lute) bool {
|
func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markSpanDataType string, luteEngine *lute.Lute) bool {
|
||||||
text := n.Content()
|
text := n.Content()
|
||||||
if ast.NodeText == n.Type {
|
if ast.NodeText == n.Type {
|
||||||
text = search.EncloseHighlighting(text, keywords, getMarkSpanStart(markSpanDataType), getMarkSpanEnd(), Conf.Search.CaseSensitive)
|
text = search.EncloseHighlighting(text, keywords, search.GetMarkSpanStart(markSpanDataType), search.GetMarkSpanEnd(), Conf.Search.CaseSensitive)
|
||||||
n.Tokens = gulu.Str.ToBytes(text)
|
n.Tokens = gulu.Str.ToBytes(text)
|
||||||
if bytes.Contains(n.Tokens, []byte(searchMarkDataType)) {
|
if bytes.Contains(n.Tokens, []byte(search.MarkDataType)) {
|
||||||
linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
|
linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
|
||||||
var children []*ast.Node
|
var children []*ast.Node
|
||||||
for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
|
for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
|
||||||
@ -935,10 +935,10 @@ func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markS
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
startTag := getMarkSpanStart(markSpanDataType)
|
startTag := search.GetMarkSpanStart(markSpanDataType)
|
||||||
text = search.EncloseHighlighting(text, keywords, startTag, getMarkSpanEnd(), Conf.Search.CaseSensitive)
|
text = search.EncloseHighlighting(text, keywords, startTag, search.GetMarkSpanEnd(), Conf.Search.CaseSensitive)
|
||||||
if strings.Contains(text, searchMarkDataType) {
|
if strings.Contains(text, search.MarkDataType) {
|
||||||
dataType := getMarkSpanStart(n.TextMarkType + " " + searchMarkDataType)
|
dataType := search.GetMarkSpanStart(n.TextMarkType + " " + search.MarkDataType)
|
||||||
text = strings.ReplaceAll(text, startTag, dataType)
|
text = strings.ReplaceAll(text, startTag, dataType)
|
||||||
tokens := gulu.Str.ToBytes(text)
|
tokens := gulu.Str.ToBytes(text)
|
||||||
linkTree := parse.Inline("", tokens, luteEngine.ParseOptions)
|
linkTree := parse.Inline("", tokens, luteEngine.ParseOptions)
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package search
|
package search
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@ -99,3 +100,16 @@ func EncloseHighlighting(text string, keywords []string, openMark, closeMark str
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MarkDataType = "search-mark"
|
||||||
|
VirtualBlockRefDataType = "virtual-block-ref"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetMarkSpanStart(dataType string) string {
|
||||||
|
return fmt.Sprintf("<span data-type=\"%s\">", dataType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMarkSpanEnd() string {
|
||||||
|
return "</span>"
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user