mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-28 03:06:10 +01:00
🎨 Merge the same elements after find-replace https://github.com/siyuan-note/siyuan/issues/14236
This commit is contained in:
parent
cf66a2a3c5
commit
56b766dd88
1 changed files with 38 additions and 0 deletions
|
|
@ -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 搜索内容块。
|
||||
//
|
||||
// method:0:关键字,1:查询语法,2:SQL,3:正则表达式
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue