Keep block code plain when copying from rendered markdown#4468
Keep block code plain when copying from rendered markdown#4468yashranaway wants to merge 1 commit into
Conversation
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
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
ApprovabilityVerdict: 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. |
What Changed
The markdown clipboard serializer now recognizes block code that arrives without its
prewrapper (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
codeelement but not itspreparent, 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:Copies that legitimately span prose plus a whole code block still include the
preelement 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.tspasses (3/3): inline code still wrapped, highlighted block selection stays plain, multi-line selection stays plainpnpm typecheckin apps/web cleanvp linton changed files cleanChecklist
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.serializeRenderedMarkdownFragmentnow treats a lonecodenode as block when the text has newlines or Shiki line spans (isBlockCodeElement); true inlinecodestill gets backticks. Selections that still includeprecontinue to use fenced blocks via the existing path.Adds unit tests in
markdown-clipboard.test.tsfor 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,
CODEelements that represent block code were incorrectly wrapped in backticks. The fix introducesisBlockCodeElementin markdown-clipboard.ts, which checks for newlines in content or child elements with classline(used by syntax-highlighted blocks). Block code is now returned as plain text; only inline code gets backtick wrapping.Macroscope summarized 4847b45.