This commit is contained in:
Liang Ding 2023-03-01 09:17:05 +08:00
parent 5c21de0a0c
commit b93af1965e
No known key found for this signature in database
GPG key ID: 136F30F901A2231D

View file

@ -209,6 +209,9 @@ func FindReplace(keyword, replacement string, ids []string, method int) (err err
return return
} }
r, _ := regexp.Compile(keyword)
escapedKey := util.EscapeHTML(keyword)
escapedR, _ := regexp.Compile(escapedKey)
ids = gulu.Str.RemoveDuplicatedElem(ids) ids = gulu.Str.RemoveDuplicatedElem(ids)
var renameRoots []*ast.Node var renameRoots []*ast.Node
renameRootTitles := map[string]string{} renameRootTitles := map[string]string{}
@ -238,7 +241,6 @@ func FindReplace(keyword, replacement string, ids []string, method int) (err err
renameRoots = append(renameRoots, n) renameRoots = append(renameRoots, n)
} }
} else if 3 == method { } else if 3 == method {
r, _ := regexp.Compile(keyword)
if nil != r && r.MatchString(title) { if nil != r && r.MatchString(title) {
renameRootTitles[n.ID] = r.ReplaceAllString(title, replacement) renameRootTitles[n.ID] = r.ReplaceAllString(title, replacement)
renameRoots = append(renameRoots, n) renameRoots = append(renameRoots, n)
@ -250,22 +252,19 @@ func FindReplace(keyword, replacement string, ids []string, method int) (err err
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte(keyword), []byte(replacement)) n.Tokens = bytes.ReplaceAll(n.Tokens, []byte(keyword), []byte(replacement))
} }
} else if 3 == method { } else if 3 == method {
r, _ := regexp.Compile(keyword)
if nil != r && r.MatchString(string(n.Tokens)) { if nil != r && r.MatchString(string(n.Tokens)) {
n.Tokens = []byte(r.ReplaceAllString(string(n.Tokens), replacement)) n.Tokens = []byte(r.ReplaceAllString(string(n.Tokens), replacement))
} }
} }
case ast.NodeTextMark: case ast.NodeTextMark:
if n.IsTextMarkType("code") { if n.IsTextMarkType("code") {
escapedKey := util.EscapeHTML(keyword)
if 0 == method { if 0 == method {
if strings.Contains(n.TextMarkTextContent, escapedKey) { if strings.Contains(n.TextMarkTextContent, escapedKey) {
n.TextMarkTextContent = strings.ReplaceAll(n.TextMarkTextContent, escapedKey, replacement) n.TextMarkTextContent = strings.ReplaceAll(n.TextMarkTextContent, escapedKey, replacement)
} }
} else if 3 == method { } else if 3 == method {
r, _ := regexp.Compile(escapedKey) if nil != escapedR && escapedR.MatchString(n.TextMarkTextContent) {
if nil != r && r.MatchString(n.TextMarkTextContent) { n.TextMarkTextContent = escapedR.ReplaceAllString(n.TextMarkTextContent, replacement)
n.TextMarkTextContent = r.ReplaceAllString(n.TextMarkTextContent, replacement)
} }
} }
} else { } else {
@ -274,7 +273,6 @@ func FindReplace(keyword, replacement string, ids []string, method int) (err err
n.TextMarkTextContent = strings.ReplaceAll(n.TextMarkTextContent, keyword, replacement) n.TextMarkTextContent = strings.ReplaceAll(n.TextMarkTextContent, keyword, replacement)
} }
} else if 3 == method { } else if 3 == method {
r, _ := regexp.Compile(keyword)
if nil != r && r.MatchString(n.TextMarkTextContent) { if nil != r && r.MatchString(n.TextMarkTextContent) {
n.TextMarkTextContent = r.ReplaceAllString(n.TextMarkTextContent, replacement) n.TextMarkTextContent = r.ReplaceAllString(n.TextMarkTextContent, replacement)
} }
@ -298,7 +296,6 @@ func FindReplace(keyword, replacement string, ids []string, method int) (err err
n.TextMarkAHref = strings.ReplaceAll(n.TextMarkAHref, keyword, replacement) n.TextMarkAHref = strings.ReplaceAll(n.TextMarkAHref, keyword, replacement)
} }
} else if 3 == method { } else if 3 == method {
r, _ := regexp.Compile(keyword)
if nil != r { if nil != r {
if r.MatchString(n.TextMarkTextContent) { if r.MatchString(n.TextMarkTextContent) {
n.TextMarkTextContent = r.ReplaceAllString(n.TextMarkTextContent, replacement) n.TextMarkTextContent = r.ReplaceAllString(n.TextMarkTextContent, replacement)
@ -637,7 +634,6 @@ func fullTextSearchByKeyword(query, boxFilter, pathFilter, typeFilter string, or
func fullTextSearchByRegexp(exp, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen int) (ret []*Block, matchedBlockCount, matchedRootCount int) { func fullTextSearchByRegexp(exp, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen int) (ret []*Block, matchedBlockCount, matchedRootCount int) {
exp = gulu.Str.RemoveInvisible(exp) exp = gulu.Str.RemoveInvisible(exp)
exp = regexp.QuoteMeta(exp)
fieldFilter := fieldRegexp(exp) fieldFilter := fieldRegexp(exp)
stmt := "SELECT * FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter stmt := "SELECT * FROM `blocks` WHERE " + fieldFilter + " AND type IN " + typeFilter