🎨 Merge the same elements after find-replace https://github.com/siyuan-note/siyuan/issues/14236

This commit is contained in:
Daniel 2025-03-06 11:05:14 +08:00
parent cf66a2a3c5
commit 56b766dd88
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -608,6 +608,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTokens(n, method, keyword, strings.TrimSpace(replacement), r)
if 1 > len(n.Tokens) {
unlinks = append(unlinks, n.Parent)
mergeSamePreNext(n)
}
case ast.NodeLinkText:
if !replaceTypes["imgText"] {
@ -657,6 +658,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("a") {
if replaceTypes["aText"] {
@ -671,6 +673,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
}
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
}
@ -700,6 +703,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
if "" == n.TextMarkAHref {
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
} else {
n.Type = ast.NodeText
n.Tokens = []byte(n.TextMarkTextContent)
@ -714,6 +718,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTextMarkTextContent(n, method, keyword, replacement, r, "em")
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("strong") {
if !replaceTypes["strong"] {
@ -723,6 +728,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTextMarkTextContent(n, method, keyword, replacement, r, "strong")
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("kbd") {
if !replaceTypes["kbd"] {
@ -741,6 +747,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTextMarkTextContent(n, method, keyword, replacement, r, "mark")
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("s") {
if !replaceTypes["s"] {
@ -750,6 +757,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTextMarkTextContent(n, method, keyword, replacement, r, "s")
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("sub") {
if !replaceTypes["sub"] {
@ -786,6 +794,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTextMarkTextContent(n, method, keyword, replacement, r, "u")
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("inline-math") {
if !replaceTypes["inlineMath"] {
@ -834,6 +843,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
replaceNodeTextMarkTextContent(n, method, keyword, replacement, r, "text")
if "" == n.TextMarkTextContent {
unlinks = append(unlinks, n)
mergeSamePreNext(n)
}
} else if n.IsTextMarkType("block-ref") {
if !replaceTypes["blockRef"] {
@ -1009,6 +1019,34 @@ func replaceNodeTokens(n *ast.Node, method int, keyword string, replacement stri
}
}
func mergeSamePreNext(n *ast.Node) {
prev, next := n.Previous, n.Next
if nil != n.Parent && ast.NodeImage == n.Parent.Type {
prev = n.Parent.Previous
next = n.Parent.Next
}
if nil == prev || nil == next || prev.Type != next.Type || ast.NodeKramdownSpanIAL == prev.Type {
return
}
switch prev.Type {
case ast.NodeText:
prev.Tokens = append(prev.Tokens, next.Tokens...)
next.Unlink()
case ast.NodeTextMark:
if prev.TextMarkType != next.TextMarkType {
break
}
switch prev.TextMarkType {
case "em", "strong", "mark", "s", "u", "text":
prev.TextMarkTextContent += next.TextMarkTextContent
next.Unlink()
}
}
}
// FullTextSearchBlock 搜索内容块。
//
// method0关键字1查询语法2SQL3正则表达式