Skip to content

Keep block code plain when copying from rendered markdown#4468

Open
yashranaway wants to merge 1 commit into
pingdotgg:mainfrom
yashranaway:copy-plain-code-selection
Open

Keep block code plain when copying from rendered markdown#4468
yashranaway wants to merge 1 commit into
pingdotgg:mainfrom
yashranaway:copy-plain-code-selection

Conversation

@yashranaway

@yashranaway yashranaway commented Jul 24, 2026

Copy link
Copy Markdown

What Changed

The markdown clipboard serializer now recognizes block code that arrives without its pre wrapper (highlighter line spans or embedded newlines) and copies it verbatim instead of wrapping it like inline code. Real inline code keeps its backticks.

Why

Selecting the contents of a rendered code block can produce a copy fragment holding the code element but not its pre parent, most visibly on Firefox. The serializer fell into the inline-code branch and wrapped the copied text in single backticks. Pasting that into a shell runs the command inside command substitution:

λ `git show-ref --verify refs/remotes/origin/opt/deploy/dev`
zsh: 97649d91...: command not found

Copies that legitimately span prose plus a whole code block still include the pre element and keep their fenced form, so the markdown-preserving copy feature is unchanged.

Fixes #4368

Testing

  • vp test run apps/web/src/markdown-clipboard.test.ts passes (3/3): inline code still wrapped, highlighted block selection stays plain, multi-line selection stays plain
  • pnpm typecheck in apps/web clean
  • vp lint on changed files clean

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Note

Low Risk
Small change to clipboard markdown serialization in apps/web with targeted tests; no auth, data, or API surface.

Overview
Fixes copy-from-chat markdown so block code copied without its surrounding pre (common on Firefox) is serialized as plain text instead of single-backtick inline code, which could turn shell commands into dangerous command substitution on paste.

serializeRenderedMarkdownFragment now treats a lone code node as block when the text has newlines or Shiki line spans (isBlockCodeElement); true inline code still gets backticks. Selections that still include pre continue to use fenced blocks via the existing path.

Adds unit tests in markdown-clipboard.test.ts for inline wrapping, highlighted block-without-pre, and multi-line code.

Reviewed by Cursor Bugbot for commit 4847b45. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Keep block code plain when copying from rendered markdown

When copying from rendered markdown, CODE elements that represent block code were incorrectly wrapped in backticks. The fix introduces isBlockCodeElement in markdown-clipboard.ts, which checks for newlines in content or child elements with class line (used by syntax-highlighted blocks). Block code is now returned as plain text; only inline code gets backtick wrapping.

Macroscope summarized 4847b45.

Selecting the contents of a code block could produce a fragment holding
the code element without its pre wrapper, most visibly on Firefox. The
markdown clipboard serializer then treated it as inline code and wrapped
the copied shell command in backticks, which executes command
substitution if pasted into a shell unedited.

Recognize block code by its highlighter line spans or embedded newlines
and copy it verbatim. Real inline code keeps its backticks.

Fixes pingdotgg#4368
@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 33ff0814-adc0-45c3-b5bd-6a224ff7a3ba

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Jul 24, 2026
@macroscopeapp

macroscopeapp Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved

Small bug fix that prevents block code from being incorrectly wrapped in backticks when copying from rendered markdown. The change is self-contained with comprehensive unit tests covering the new behavior.

You can customize Macroscope's approvability policy. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S 10-29 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: copying surrounds text in backticks

1 participant