fix: Copying out of code blocks and inline code marks should not include markdown formatting

This commit is contained in:
Tom Moor committed 2025-02-10 21:28:36 -05:00
1 parent 926a4e2224
commit f53c2828ef
1 file changed
+24 -5
@@ -1,5 +1,6 @@
import { Plugin, PluginKey } from "prosemirror-state";
import Extension from "@shared/editor/lib/Extension";
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
/**
* A plugin that allows overriding the default behavior of the editor to allow
@@ -11,16 +12,34 @@ export default class ClipboardTextSerializer extends Extension {
}
get plugins() {
const serializer = this.editor.extensions.serializer();
const mdSerializer = this.editor.extensions.serializer();
return [
new Plugin({
key: new PluginKey("clipboardTextSerializer"),
props: {
clipboardTextSerializer: (slice) =>
serializer.serialize(slice.content, {
softBreak: true,
}),
clipboardTextSerializer: (slice) => {
const isMultiline = slice.content.childCount > 1;
// This is a cheap way to determine if the content is "complex",
// aka it has multiple marks or formatting. In which case we'll use
// markdown formatting
const copyAsMarkdown =
isMultiline ||
slice.content.content.some(
(node) => node.content.content.length > 1
);
return copyAsMarkdown
? mdSerializer.serialize(slice.content, {
softBreak: true,
})
: slice.content.content
.map((node) =>
ProsemirrorHelper.toPlainText(node, this.editor.schema)
)
.join("");
},
},
}),
];