Files
paseo/packages/app/src/utils/rich-clipboard.ts
T
e99eb7de3d fix(app): keep line breaks and indentation in copied code (#2935)
* fix(app): keep line breaks and indentation in copied code

#2930 strips the pre/code tags from a partly selected code block, which
is right for the Markdown syntax but leaves the code itself going
through Turndown as prose. Turndown collapses whitespace, so two
selected lines arrive as one, the indentation is gone, and
Markdown-significant characters come back escaped.

Divert a partial code selection before that: copy the selected text
verbatim. Fully selected regions are untouched and still produce a
fence, which is what hasSelectedAllContents was already deciding.

The html half needs pre for anything multi-line -- HTML collapses
newlines too, so a rich paste target would lose the lines the same way.
A single line stays undecorated, matching the rule that a partial
selection does not carry its container's formatting.

Two trailing-newline fixes ride along, since both make a paste run code
nobody asked to run. Newlines between rendered lines are their own text
nodes, so a selection that overshoots the end of a line carries one;
strip it. The block's Copy button was handed the raw fence body, which
ends in a newline; hand it the rendered text instead.

* fix(app): strip every trailing blank line from copied code

The Copy button stripped one trailing newline, so a fence whose author
left a blank line before the closing delimiter still put an executable
newline on the clipboard. Pasting that into a terminal runs the last
line, which is the thing the strip existed to prevent.

Both copy paths now share one regex in markup.ts and remove every
trailing break, plus any indentation that followed the last one.

Reported by Greptile on #2935.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(app): infer code copy format from selection boundary

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
2026-08-06 11:27:38 +00:00

88 lines
2.8 KiB
TypeScript

import { createAssistantMarkdownParser } from "./assistant-markdown-parser";
const markdownRenderer = createAssistantMarkdownParser();
type ClipboardMimeType = "text/plain" | "text/html";
export interface MarkdownClipboardContent {
plainText: string;
html: string;
}
export interface RichClipboardWriter {
supportsHtml: () => boolean;
write: (data: Record<ClipboardMimeType, Blob>) => Promise<void>;
}
export interface MarkdownClipboardEnvironment {
richWriter?: RichClipboardWriter | null;
writePlainText: (text: string) => Promise<unknown>;
}
export function createMarkdownClipboardContent(markdown: string): MarkdownClipboardContent {
return {
plainText: markdown,
html: `<meta charset="utf-8">${markdownRenderer.render(markdown)}`,
};
}
export interface CodeClipboardOptions {
language?: string | null;
/** Wrap in `pre`/`code` so the line structure survives. */
block: boolean;
}
/**
* Clipboard payload for a selection that lies inside rendered code.
*
* `plainText` is the code exactly as it was selected, so it pastes straight into a
* shell or an editor.
*
* The html half stays undecorated for a single line, matching the rule that a partial
* selection does not carry its container's formatting. Multi-line code is the
* exception and needs `pre`: HTML collapses newlines, so anything else loses the line
* structure in a rich target exactly the way the plain half would.
*
* The fence info string is agent-authored, so the language is escaped as an attribute
* value rather than interpolated raw.
*/
export function createCodeClipboardContent(
code: string,
options: CodeClipboardOptions,
): MarkdownClipboardContent {
const escapedCode = markdownRenderer.utils.escapeHtml(code);
if (!options.block) {
return { plainText: code, html: `<meta charset="utf-8">${escapedCode}` };
}
const language = options.language?.trim();
const className = language
? ` class="language-${markdownRenderer.utils.escapeHtml(language)}"`
: "";
return {
plainText: code,
html: `<meta charset="utf-8"><pre><code${className}>${escapedCode}</code></pre>`,
};
}
export async function writeMarkdownToRichClipboard(
markdown: string,
environment: MarkdownClipboardEnvironment,
): Promise<void> {
if (environment.richWriter?.supportsHtml()) {
const content = createMarkdownClipboardContent(markdown);
try {
await environment.richWriter.write({
"text/plain": new Blob([content.plainText], { type: "text/plain" }),
"text/html": new Blob([content.html], { type: "text/html" }),
});
return;
} catch {
// Fall through to the plain-text path. Some webviews expose rich clipboard
// APIs but deny writes depending on focus, permissions, or browser policy.
}
}
await environment.writePlainText(markdown);
}