An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Keep case-insensitive replacement text literal https://github.com/siyuan-note/siyuan/issues/20242
Danieldee81f8a259e
diff --git a/kernel/model/search.go b/kernel/model/search.goindex 69139722de..e67ae21332 100644--- a/kernel/model/search.go+++ b/kernel/model/search.go@@ -3446,5 +3446,7 @@ func filterQueryInvisibleChars(query string) string {func replaceCaseInsensitive(input, old, new []byte) []byte {re := regexp.MustCompile("(?i)" + regexp.QuoteMeta(string(old)))- return []byte(re.ReplaceAllString(string(input), string(new)))+ return re.ReplaceAllFunc(input, func([]byte) []byte {+ return new+ })}diff --git a/kernel/model/search_literal_replace_test.go b/kernel/model/search_literal_replace_test.gonew file mode 100644index 0000000000..4627a14bf1--- /dev/null+++ b/kernel/model/search_literal_replace_test.go@@ -0,0 +1,56 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+// SPDX-License-Identifier: AGPL-3.0-or-later++package model++import (+ "regexp"+ "testing"++ "github.com/88250/lute/ast"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestReplaceCaseInsensitiveLiteralReplacement(t *testing.T) {+ for _, replacement := range []string{"bar$1baz", "a$bb", "x${y}z", "100$", "费用$100", `path\file$1`, "$$", "plain", ""} {+ got := string(replaceCaseInsensitive([]byte("foo and FOO"), []byte("foo"), []byte(replacement)))+ want := replacement + " and " + replacement+ if got != want {+ t.Errorf("replacement %q: got %q, want %q", replacement, got, want)+ }+ }+ if got := string(replaceCaseInsensitive([]byte("a.b and A.B and axb"), []byte("a.b"), []byte("$1"))); got != "$1 and $1 and axb" {+ t.Fatalf("literal keyword or replacement mismatch: %q", got)+ }+}++func TestReplaceTextNodeLiteralAndRegexReplacement(t *testing.T) {+ engine := util.NewLute()+ for _, tc := range []struct {+ name string+ caseSensitive bool+ method int+ keyword string+ replacement string+ want string+ }{+ {"insensitive-literal", false, 0, "foo", "费用$100", "费用$100"},+ {"sensitive-literal", true, 0, "FOO", "费用$100", "费用$100"},+ {"regex-capture", false, 3, "(?i)(foo)", "${1}bar", "FOObar"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ setSearchCaseSensitive(t, tc.caseSensitive)+ root := &ast.Node{Type: ast.NodeParagraph, ID: ast.NewNodeID()}+ text := replaceTextTestText("FOO")+ root.AppendChild(text)+ if !replaceTextNode(text, tc.method, tc.keyword, tc.replacement, regexp.MustCompile(tc.keyword), engine) {+ t.Fatal("replacement was not applied")+ }+ text.Unlink()+ if got := root.Content(); got != tc.want {+ t.Fatalf("got %q, want %q", got, tc.want)+ }+ })+ }+}