From 87709cd5a192f43f33e519b854cd81c9c19fe905 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 13 Apr 2025 18:20:38 +0800 Subject: [PATCH] :art: Supports replacing tags with other inline elements https://github.com/siyuan-note/siyuan/issues/11238 --- kernel/model/search.go | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/kernel/model/search.go b/kernel/model/search.go index 6da54f800..13492699e 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -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 }