Skip to content

Commit

Permalink
🐛 Replace fails when search results contain mixed case in text elements
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Sep 19, 2024
1 parent 35f5e76 commit 6596d03
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions kernel/model/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,30 @@ func replaceNodeTextMarkTextContent(n *ast.Node, method int, keyword string, rep
// Supports replacing text elements with other elements https://github.com/siyuan-note/siyuan/issues/11058
func replaceTextNode(text *ast.Node, method int, keyword string, replacement string, r *regexp.Regexp, luteEngine *lute.Lute) bool {
if 0 == method {
if bytes.Contains(text.Tokens, []byte(keyword)) {
newContent := bytes.ReplaceAll(text.Tokens, []byte(keyword), []byte(replacement))
newContent := text.Tokens
if Conf.Search.CaseSensitive {
if bytes.Contains(text.Tokens, []byte(keyword)) {
newContent = bytes.ReplaceAll(text.Tokens, []byte(keyword), []byte(replacement))
}
} else {
// 当搜索结果中的文本元素包含大小写混合时替换失败
// Replace fails when search results contain mixed case in text elements https://github.com/siyuan-note/siyuan/issues/9171
keywords := strings.Split(keyword, " ")
// keyword 可能是 "foo Foo" 使用空格分隔的大小写命中情况,这里统一转换小写后去重
if 1 < len(keywords) {
var lowerKeywords []string
for _, k := range keywords {
lowerKeywords = append(lowerKeywords, strings.ToLower(k))
}
lowerKeywords = gulu.Str.RemoveDuplicatedElem(lowerKeywords)
keyword = strings.Join(lowerKeywords, " ")
}

if bytes.Contains(bytes.ToLower(text.Tokens), []byte(keyword)) {
newContent = replaceCaseInsensitive(text.Tokens, []byte(keyword), []byte(replacement))
}
}
if !bytes.Equal(newContent, text.Tokens) {
tree := parse.Inline("", newContent, luteEngine.ParseOptions)
if nil == tree.Root.FirstChild {
return false
Expand Down Expand Up @@ -1818,3 +1840,8 @@ func filterQueryInvisibleChars(query string) string {
query = strings.ReplaceAll(query, "_@full_width_space@_", " ")
return query
}

func replaceCaseInsensitive(input, old, new []byte) []byte {
re := regexp.MustCompile("(?i)" + regexp.QuoteMeta(string(old)))
return []byte(re.ReplaceAllString(string(input), string(new)))
}

0 comments on commit 6596d03

Please sign in to comment.