🎨 Supports replacing tags with other inline elements https://github.com/siyuan-note/siyuan/issues/11238

This commit is contained in:
Daniel 2025-04-13 18:20:38 +08:00
parent 6c60a3726f
commit 87709cd5a1
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -728,7 +728,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "em")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "em", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
@ -738,7 +738,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "strong")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "strong", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
@ -748,7 +748,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "kbd")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "kbd", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
}
@ -757,7 +757,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "mark")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "mark", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
@ -767,7 +767,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "s")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "s", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
@ -777,7 +777,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "sub")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "sub", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
}
@ -786,7 +786,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "sup")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "sup", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
}
@ -795,7 +795,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "tag")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "tag", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
}
@ -804,7 +804,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "u")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "u", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
@ -853,7 +853,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return ast.WalkContinue
}
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "text")
replaceNodeTextMarkTextContent(n, method, keyword, escapedKey, replacement, r, "text", luteEngine)
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
@ -935,7 +935,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
return
}
func replaceNodeTextMarkTextContent(n *ast.Node, method int, keyword, escapedKey string, replacement string, r *regexp.Regexp, typ string) {
func replaceNodeTextMarkTextContent(n *ast.Node, method int, keyword, escapedKey string, replacement string, r *regexp.Regexp, typ string, luteEngine *lute.Lute) {
if 0 == method {
if strings.Contains(typ, "tag") {
keyword = strings.TrimPrefix(keyword, "#")
@ -955,7 +955,21 @@ func replaceNodeTextMarkTextContent(n *ast.Node, method int, keyword, escapedKey
} else if strings.Contains(content, keyword) {
content = strings.ReplaceAll(content, keyword, replacement)
}
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(content)})
tree := parse.Inline("", []byte(content), luteEngine.ParseOptions)
if nil == tree.Root.FirstChild {
return
}
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 {
n.InsertBefore(rNode)
}
n.TextMarkTextContent = ""
return
}