Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

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

Daniel56b766dd8879
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 4a012eba42..f937976340 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -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:正则表达式