Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(editor): preserve Markdown source on copy/cut (#1858)

Naiyuan Qing9467a8c61675
diff --git a/packages/views/editor/extensions/index.ts b/packages/views/editor/extensions/index.ts
index d6ca55da45..5a91a9dec9 100644
--- a/packages/views/editor/extensions/index.ts
+++ b/packages/views/editor/extensions/index.ts
@@ -39,6 +39,7 @@ import { BaseMentionExtension } from "./mention-extension";
import { createMentionSuggestion } from "./mention-suggestion";
import { CodeBlockView } from "./code-block-view";
import { createMarkdownPasteExtension } from "./markdown-paste";
+import { createMarkdownCopyExtension } from "./markdown-copy";
import { createSubmitExtension } from "./submit-shortcut";
import { createBlurShortcutExtension } from "./blur-shortcut";
import { createFileUploadExtension } from "./file-upload";
@@ -129,6 +130,10 @@ export function createEditorExtensions(
InlineMathExtension,
// 3-space indent so nested ordered lists survive CommonMark in ReadonlyContent.
Markdown.configure({ indentation: { style: "space", size: 3 } }),
+ // Make Cmd+C / Cmd+X / drag write Markdown source to clipboard text/plain.
+ // Registered for both editable and readonly so users can copy from rendered
+ // comments and paste the original Markdown elsewhere.
+ createMarkdownCopyExtension(),
FileCardExtension,
...(options.disableMentions
? []
diff --git a/packages/views/editor/extensions/markdown-copy.ts b/packages/views/editor/extensions/markdown-copy.ts
new file mode 100644
index 0000000000..f71f8642e1
--- /dev/null
+++ b/packages/views/editor/extensions/markdown-copy.ts
@@ -0,0 +1,62 @@
+/**
+ * Markdown copy extension — make the clipboard's text/plain channel carry
+ * Markdown source instead of plain textContent.
+ *
+ * Symmetric to markdown-paste.ts:
+ * paste: text/plain → editor.markdown.parse → doc
+ * copy: slice → editor.markdown.serialize → text/plain
+ *
+ * Why: ProseMirror's default clipboardTextSerializer calls Slice.textBetween,
+ * which flattens every node to its inner text. Headings, lists, code blocks,
+ * mentions, file cards — all lose their Markdown markers. Pasting into VS
+ * Code, terminals, or messaging apps then sees only naked text.
+ *
+ * The text/html channel is left at ProseMirror's default so pasting back
+ * into another ProseMirror editor still preserves exact node structure via
+ * data-pm-slice.
+ */
+import { Extension } from "@tiptap/core";
+import { Plugin, PluginKey } from "@tiptap/pm/state";
+import type { Slice } from "@tiptap/pm/model";
+
+// Blob URLs (blob:http://…) are process-local; never let them leave the page.
+const BLOB_IMAGE_RE = /!\[[^\]]*\]\(blob:[^)]*\)\n?/g;
+
+export function createMarkdownCopyExtension() {
+ return Extension.create({
+ name: "markdownCopy",
+ addProseMirrorPlugins() {
+ const { editor } = this;
+
+ const fallback = (slice: Slice) =>
+ slice.content.textBetween(0, slice.content.size, "\n\n");
+
+ return [
+ new Plugin({
+ key: new PluginKey("markdownCopy"),
+ props: {
+ clipboardTextSerializer(slice: Slice) {
+ if (!editor.markdown) return fallback(slice);
+ try {
+ // Wrap slice content in a temp doc so the serializer walks
+ // it like a real document. Inline-only slices auto-wrap
+ // into doc → paragraph; block slices pass through.
+ const doc = editor.schema.topNodeType.create(
+ null,
+ slice.content,
+ );
+ const md = editor.markdown.serialize(doc.toJSON());
+ return md.replace(BLOB_IMAGE_RE, "").replace(/\n+$/, "");
+ } catch {
+ // Special selections (e.g. table cellSelection) may fail
+ // schema validation when wrapped in a doc node. Fall back
+ // so copy never breaks.
+ return fallback(slice);
+ }
+ },
+ },
+ }),
+ ];
+ },
+ });
+}