Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): paste copied code blocks as plain text (#104154)

posthog[bot]84eccdb8b0f4
diff --git a/products/desktop/packages/ui/src/features/message-editor/tiptap/useTiptapEditor.ts b/products/desktop/packages/ui/src/features/message-editor/tiptap/useTiptapEditor.ts
index 1e45cb4cf041..5b818f3befc4 100644
--- a/products/desktop/packages/ui/src/features/message-editor/tiptap/useTiptapEditor.ts
+++ b/products/desktop/packages/ui/src/features/message-editor/tiptap/useTiptapEditor.ts
@@ -45,7 +45,7 @@ import { editorContentToTiptapJson } from "../tiptap/markdownDoc";
import { insertPastedMarkdown } from "../tiptap/markdownPaste";
import { getPromptEditorAttributes } from "../tiptap/promptEditorAttributes";
import { type DraftContext, useDraftSync } from "../tiptap/useDraftSync";
-import { htmlToMarkdown } from "../utils/htmlToMarkdown";
+import { convertClipboardHtml } from "../utils/htmlToMarkdown";
import {
persistImageFile,
persistTextContent,
@@ -629,10 +629,13 @@ export function useTiptapEditor(options: UseTiptapEditorOptions) {
return true;
}
- // Editor is plain-text, so preserve pasted formatting as Markdown.
+ // Preserve rich formatting as Markdown. A standalone code block uses
+ // plain text so selection-copy matches the code block's copy button.
const html = event.clipboardData?.getData("text/html");
- const markdown = html ? htmlToMarkdown(html, clipboardText) : null;
- const effectiveText = markdown ?? clipboardText;
+ const convertedHtml = html
+ ? convertClipboardHtml(html, clipboardText)
+ : null;
+ const effectiveText = convertedHtml?.text ?? clipboardText;
// Auto-convert long pasted text into a file attachment
const autoConvertThreshold =
@@ -679,15 +682,21 @@ export function useTiptapEditor(options: UseTiptapEditorOptions) {
// Lists, fences and inline code paste as nodes; input rules only fire
// while typing, so without this they would stay literal text.
- if (effectiveText && insertPastedMarkdown(view, effectiveText)) {
+ if (
+ effectiveText &&
+ convertedHtml?.kind !== "plain" &&
+ insertPastedMarkdown(view, effectiveText)
+ ) {
event.preventDefault();
return true;
}
// Insert inline; ProseMirror would otherwise drop the HTML formatting.
- if (markdown) {
+ if (convertedHtml) {
event.preventDefault();
- view.dispatch(view.state.tr.insertText(markdown, from, to));
+ view.dispatch(
+ view.state.tr.insertText(convertedHtml.text, from, to),
+ );
return true;
}
diff --git a/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.test.ts b/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.test.ts
index c845b20b8da4..ed29f5bed4bf 100644
--- a/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.test.ts
+++ b/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { htmlToMarkdown } from "./htmlToMarkdown";
+import { convertClipboardHtml, htmlToMarkdown } from "./htmlToMarkdown";
describe("htmlToMarkdown", () => {
it.each([
@@ -74,4 +74,39 @@ describe("htmlToMarkdown", () => {
"See **item_1.** in arr[0]",
);
});
+
+ it.each([
+ [
+ "a standalone code block as plain text",
+ '<div class="selection"><pre><code><span>const x = 1;</span></code></pre></div>',
+ "const x = 1;",
+ { text: "const x = 1;", kind: "plain" },
+ ],
+ [
+ "a code block with surrounding prose as markdown",
+ "<p>Example:</p><pre><code>const x = 1;</code></pre>",
+ "Example:\nconst x = 1;",
+ { text: "Example:\n\n```\nconst x = 1;\n```", kind: "markdown" },
+ ],
+ [
+ "a code block with preceding prose in the same wrapper as markdown",
+ "<div>Example:<pre><code>const x = 1;</code></pre></div>",
+ "Example:\nconst x = 1;",
+ { text: "Example:\n\n```\nconst x = 1;\n```", kind: "markdown" },
+ ],
+ [
+ "a code block with following prose in the same wrapper as markdown",
+ "<div><pre><code>const x = 1;</code></pre>Done.</div>",
+ "const x = 1;\nDone.",
+ { text: "```\nconst x = 1;\n```\n\nDone.", kind: "markdown" },
+ ],
+ [
+ "a code block without a plain-text payload as markdown",
+ "<pre><code>const x = 1;</code></pre>",
+ "",
+ { text: "```\nconst x = 1;\n```", kind: "markdown" },
+ ],
+ ])("converts %s", (_name, html, plainText, expected) => {
+ expect(convertClipboardHtml(html, plainText)).toEqual(expected);
+ });
});
diff --git a/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.ts b/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.ts
index 49ab15002d70..e33ef3e9328b 100644
--- a/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.ts
+++ b/products/desktop/packages/ui/src/features/message-editor/utils/htmlToMarkdown.ts
@@ -4,6 +4,20 @@ import TurndownService from "turndown";
let turndown: TurndownService | null = null;
+export interface ClipboardHtmlConversion {
+ text: string;
+ kind: "markdown" | "plain";
+}
+
+const NON_CONTENT_TAGS = new Set([
+ "HEAD",
+ "LINK",
+ "META",
+ "SCRIPT",
+ "STYLE",
+ "TITLE",
+]);
+
function getTurndown(): TurndownService {
if (turndown) return turndown;
turndown = new TurndownService({
@@ -46,3 +60,51 @@ export function htmlToMarkdown(
return markdown;
}
+
+function containsOnlyOneCodeBlock(html: string): boolean {
+ const document = new DOMParser().parseFromString(html, "text/html");
+ const codeBlocks = document.body.querySelectorAll("pre");
+ if (codeBlocks.length !== 1) return false;
+
+ const codeBlock = codeBlocks[0];
+ const containsOnlyCodeBlockElements = Array.from(
+ document.body.querySelectorAll("*"),
+ ).every(
+ (element) =>
+ NON_CONTENT_TAGS.has(element.tagName) ||
+ element === codeBlock ||
+ element.contains(codeBlock) ||
+ codeBlock.contains(element),
+ );
+ if (!containsOnlyCodeBlockElements) return false;
+
+ return !containsTextOutsideCodeBlock(document.body, codeBlock);
+}
+
+function containsTextOutsideCodeBlock(
+ node: Node,
+ codeBlock: HTMLPreElement,
+): boolean {
+ if (node === codeBlock) return false;
+ if (node.nodeType === Node.TEXT_NODE)
+ return Boolean(node.textContent?.trim());
+ if (node.nodeType !== Node.ELEMENT_NODE) return false;
+
+ const element = node as Element;
+ if (NON_CONTENT_TAGS.has(element.tagName)) return false;
+ return Array.from(element.childNodes).some((child) =>
+ containsTextOutsideCodeBlock(child, codeBlock),
+ );
+}
+
+export function convertClipboardHtml(
+ html: string,
+ plainTextFallback?: string,
+): ClipboardHtmlConversion | null {
+ if (plainTextFallback && containsOnlyOneCodeBlock(html)) {
+ return { text: plainTextFallback, kind: "plain" };
+ }
+
+ const markdown = htmlToMarkdown(html, plainTextFallback);
+ return markdown ? { text: markdown, kind: "markdown" } : null;
+}