Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2024-04-20 10:29:06 +08:00
commit 59aa3fc383

View file

@ -522,16 +522,20 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
title := node.IALAttr("title")
if 0 == method {
if strings.Contains(title, keyword) {
renameRootTitles[node.ID] = strings.ReplaceAll(title, keyword, replacement)
docTitleReplacement := strings.ReplaceAll(replacement, "/", "")
renameRootTitles[node.ID] = strings.ReplaceAll(title, keyword, docTitleReplacement)
renameRoots = append(renameRoots, node)
}
} else if 3 == method {
if nil != r && r.MatchString(title) {
renameRootTitles[node.ID] = r.ReplaceAllString(title, replacement)
docTitleReplacement := strings.ReplaceAll(replacement, "/", "")
renameRootTitles[node.ID] = r.ReplaceAllString(title, docTitleReplacement)
renameRoots = append(renameRoots, node)
}
}
} else {
luteEngine := util.NewLute()
var unlinks []*ast.Node
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
@ -543,7 +547,9 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTokens(n, method, keyword, replacement, r)
if replaceTextNode(n, method, keyword, replacement, r, luteEngine) {
unlinks = append(unlinks, n)
}
case ast.NodeLinkDest:
if !replaceTypes["imgSrc"] {
return ast.WalkContinue
@ -726,6 +732,10 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
})
for _, unlink := range unlinks {
unlink.Unlink()
}
if err = writeTreeUpsertQueue(tree); nil != err {
return
}
@ -763,6 +773,50 @@ func replaceNodeTextMarkTextContent(n *ast.Node, method int, keyword string, rep
}
}
// replaceTextNode 替换文本节点为其他节点。
// 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))
tree := parse.Inline("", newContent, luteEngine.ParseOptions)
if nil == tree.Root.FirstChild {
return false
}
parse.NestedInlines2FlattedSpans(tree, false)
var replaceNodes []*ast.Node
for rNode := tree.Root.FirstChild.FirstChild; nil != rNode; rNode = rNode.Next {
replaceNodes = append(replaceNodes, rNode)
}
for _, rNode := range replaceNodes {
text.InsertBefore(rNode)
}
return true
}
} else if 3 == method {
if nil != r && r.MatchString(string(text.Tokens)) {
newContent := bytes.ReplaceAll(text.Tokens, []byte(keyword), []byte(replacement))
tree := parse.Inline("", newContent, luteEngine.ParseOptions)
if nil == tree.Root.FirstChild {
return false
}
var replaceNodes []*ast.Node
for rNode := tree.Root.FirstChild; nil != rNode; rNode = rNode.Next {
replaceNodes = append(replaceNodes, rNode)
}
for _, rNode := range replaceNodes {
text.InsertBefore(rNode)
}
return true
}
}
return false
}
func replaceNodeTokens(n *ast.Node, method int, keyword string, replacement string, r *regexp.Regexp) {
if 0 == method {
if bytes.Contains(n.Tokens, []byte(keyword)) {