Skip to content

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

:bug: Preserve WPS comments during paste https://github.com/siyuan-note/siyuan/issues/18748

Daniel4df531fbb5df
diff --git a/kernel/api/lute_wps_comment.go b/kernel/api/lute_wps_comment.go
index 03932a7499..77c072460d 100644
--- a/kernel/api/lute_wps_comment.go
+++ b/kernel/api/lute_wps_comment.go
@@ -107,8 +107,12 @@ func normalizeWPSComments(dom, text, encoded string) string {
if len(nodeRanges) == 0 {
return dom
}
+ var commentSpans []*nethtml.Node
for node, ranges := range nodeRanges {
- wrapWPSCommentRanges(node, ranges)
+ commentSpans = append(commentSpans, wrapWPSCommentRanges(node, ranges)...)
+ }
+ for _, span := range commentSpans {
+ liftWPSCommentSpan(span)
}
ret, err := doc.Find("body").Html()
@@ -275,7 +279,7 @@ func wpsHTMLTextPositions(root *nethtml.Node) (string, []wpsHTMLChar) {
return string(text), positions
}
-func wrapWPSCommentRanges(node *nethtml.Node, ranges []wpsNodeRange) {
+func wrapWPSCommentRanges(node *nethtml.Node, ranges []wpsNodeRange) (ret []*nethtml.Node) {
if node.Parent == nil {
return
}
@@ -299,6 +303,7 @@ func wrapWPSCommentRanges(node *nethtml.Node, ranges []wpsNodeRange) {
}
span.AppendChild(&nethtml.Node{Type: nethtml.TextNode, Data: string(text[commentRange.start:commentRange.end])})
node.Parent.InsertBefore(span, node)
+ ret = append(ret, span)
cursor = commentRange.end
}
if cursor == 0 {
@@ -308,4 +313,46 @@ func wrapWPSCommentRanges(node *nethtml.Node, ranges []wpsNodeRange) {
node.Parent.InsertBefore(&nethtml.Node{Type: nethtml.TextNode, Data: string(text[cursor:])}, node)
}
node.Parent.RemoveChild(node)
+ return
+}
+
+func liftWPSCommentSpan(span *nethtml.Node) {
+ for span.Parent != nil && (span.Parent.DataAtom == atom.Font || span.Parent.DataAtom == atom.Span) {
+ parent := span.Parent
+ grandparent := parent.Parent
+ if grandparent == nil {
+ return
+ }
+ before := cloneWPSInline(parent)
+ for parent.FirstChild != span {
+ child := parent.FirstChild
+ parent.RemoveChild(child)
+ before.AppendChild(child)
+ }
+ parent.RemoveChild(span)
+ after := cloneWPSInline(parent)
+ for parent.FirstChild != nil {
+ child := parent.FirstChild
+ parent.RemoveChild(child)
+ after.AppendChild(child)
+ }
+ if before.FirstChild != nil {
+ grandparent.InsertBefore(before, parent)
+ }
+ grandparent.InsertBefore(span, parent)
+ if after.FirstChild != nil {
+ grandparent.InsertBefore(after, parent)
+ }
+ grandparent.RemoveChild(parent)
+ }
+}
+
+func cloneWPSInline(node *nethtml.Node) *nethtml.Node {
+ return &nethtml.Node{
+ Type: node.Type,
+ DataAtom: node.DataAtom,
+ Data: node.Data,
+ Namespace: node.Namespace,
+ Attr: append([]nethtml.Attribute(nil), node.Attr...),
+ }
}
diff --git a/kernel/api/lute_wps_comment_test.go b/kernel/api/lute_wps_comment_test.go
index 51d64f4eb7..47580742b2 100644
--- a/kernel/api/lute_wps_comment_test.go
+++ b/kernel/api/lute_wps_comment_test.go
@@ -23,7 +23,9 @@ import (
"strings"
"testing"
+ "github.com/88250/lute/ast"
"github.com/PuerkitoBio/goquery"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
func TestNormalizeWPSComments(t *testing.T) {
@@ -47,6 +49,22 @@ func TestNormalizeWPSComments(t *testing.T) {
if doc.Find("body").Text() != "111111111111111111" {
t.Fatalf("unexpected normalized text: %s", doc.Find("body").Text())
}
+
+ luteEngine := util.NewLute()
+ luteEngine.SetHTMLTag2TextMark(true)
+ tree := luteEngine.HTML2Tree(normalized)
+ var inlineMemo *ast.Node
+ ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && ast.NodeTextMark == node.Type && node.IsTextMarkType("inline-memo") {
+ inlineMemo = node
+ return ast.WalkStop
+ }
+ return ast.WalkContinue
+ })
+ if inlineMemo == nil || inlineMemo.TextMarkTextContent != "111" ||
+ inlineMemo.TextMarkInlineMemoContent != "22222222222222" {
+ t.Fatal("WPS comment was not converted to an inline memo")
+ }
}
func TestNormalizeWPSCommentsRequiresMatchingText(t *testing.T) {