mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 简化搜索命中结果高亮实现
This commit is contained in:
parent
556fb1f2d4
commit
f6afd1c142
1 changed files with 5 additions and 21 deletions
|
|
@ -78,7 +78,7 @@ func SplitKeyword(keyword string) (keywords []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func EncloseHighlighting(text string, keywords []string, openMark, closeMark string, caseSensitive bool) string {
|
func EncloseHighlighting(text string, keywords []string, openMark, closeMark string, caseSensitive bool) (ret string) {
|
||||||
ic := "(?i)"
|
ic := "(?i)"
|
||||||
if caseSensitive {
|
if caseSensitive {
|
||||||
ic = "(?)"
|
ic = "(?)"
|
||||||
|
|
@ -92,26 +92,10 @@ func EncloseHighlighting(text string, keywords []string, openMark, closeMark str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
re += ")"
|
re += ")"
|
||||||
|
ret = text
|
||||||
|
|
||||||
if reg, err := regexp.Compile(re); nil == err {
|
if reg, err := regexp.Compile(re); nil == err {
|
||||||
text = reg.ReplaceAllStringFunc(text, func(s string) string {
|
ret = reg.ReplaceAllStringFunc(text, func(s string) string { return openMark + s + closeMark })
|
||||||
return openMark + s + closeMark
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
for _, k := range keywords {
|
|
||||||
k = regexp.QuoteMeta(k)
|
|
||||||
var repls, words []string
|
|
||||||
if re, err := regexp.Compile(ic + k); nil == err {
|
|
||||||
words = re.FindAllString(text, -1)
|
|
||||||
} else {
|
|
||||||
re, _ := regexp.Compile(ic + regexp.QuoteMeta(k))
|
|
||||||
words = re.FindAllString(text, -1)
|
|
||||||
}
|
|
||||||
for _, word := range words {
|
|
||||||
repls = append(repls, word, openMark+word+closeMark)
|
|
||||||
}
|
|
||||||
replacer := strings.NewReplacer(repls...)
|
|
||||||
text = replacer.Replace(text)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return text
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue