Skip to content

feat: add copy button to code blocks in markdown preview - #323609

Merged
Henning Dieterichs (hediet) merged 1 commit into
microsoft:mainfrom
rushil-b-patel:main-copy-code-block-markdown-preview
Jul 28, 2026
Merged

feat: add copy button to code blocks in markdown preview#323609
Henning Dieterichs (hediet) merged 1 commit into
microsoft:mainfrom
rushil-b-patel:main-copy-code-block-markdown-preview

Conversation

@rushil-b-patel

@rushil-b-patel Rushil Patel (rusp) (rushil-b-patel) commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Adds a GitHub-style copy button that appears on hover in the top-right corner of code blocks in the markdown preview. Clicking copies the code block content to the clipboard and shows a checkmark confirmation.

fixes: #322269

Copilot AI review requested due to automatic review settings June 29, 2026 20:23
@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a GitHub-style “copy code” affordance to the Markdown preview webview by injecting an icon-only button into rendered code blocks and styling it to appear on hover/focus, with a temporary “copied” checkmark state after activation.

Changes:

  • Inject a copy button into each pre > code block on initial load and on preview content updates.
  • Implement inline SVG icons (copy + check) and clipboard write behavior on click.
  • Add CSS for positioning and hover/focus styling of the copy button within code blocks.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
extensions/markdown-language-features/preview-src/index.ts Adds DOM injection and clipboard handling for code-block copy buttons in the preview webview.
extensions/markdown-language-features/media/markdown.css Adds styling for the code-block copy button and updates <pre> positioning to support it.

Comment thread extensions/markdown-language-features/preview-src/index.ts
Comment thread extensions/markdown-language-features/preview-src/index.ts
Comment thread extensions/markdown-language-features/preview-src/index.ts
Comment on lines +456 to +459
pre:hover > .code-block-copy-button,
.code-block-copy-button:focus {
opacity: 1;
}
Comment on lines +437 to +440
.code-block-copy-button {
position: absolute;
top: 8px;
right: 8px;
@rushil-b-patel
Rushil Patel (rusp) (rushil-b-patel) force-pushed the main-copy-code-block-markdown-preview branch from d379b4d to a0e2af4 Compare June 29, 2026 20:41
@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Matt Bierner (@mjbvz), I see you have worked in markdown features.
Can you please check this PR ?
Thanks :)

@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Vritant Bhardwaj (@vritant24) Can you please delegate to appropriate person for this PR ?
Thanks

@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Henning Dieterichs (@hediet) any update here :) !

@hediet
Henning Dieterichs (hediet) enabled auto-merge (rebase) July 7, 2026 08:48
@hediet

Copy link
Copy Markdown
Member

Thanks for the PR!

@rushil-b-patel

Rushil Patel (rusp) (rushil-b-patel) commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Hello Henning Dieterichs (@hediet) Thanks for your time.
Should I fix this failing test & push again ?
Edit: I think rebase should fix this !

auto-merge was automatically disabled July 7, 2026 12:22

Head branch was pushed to by a user without write access

@rushil-b-patel
Rushil Patel (rusp) (rushil-b-patel) force-pushed the main-copy-code-block-markdown-preview branch from a0e2af4 to 1c5ada8 Compare July 7, 2026 12:22
@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Henning Dieterichs (@hediet), I rebase & pushed to avoid that failing check. Hope you won't mind.

Thanks : )

@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Henning Dieterichs (@hediet),

Sorry to bother you again for the approval, but the CI was failing because of outdated rebase so I had to rebase and push it again :)

@rushil-b-patel
Rushil Patel (rusp) (rushil-b-patel) force-pushed the main-copy-code-block-markdown-preview branch from f669823 to a3b7b0c Compare July 15, 2026 04:45
Adds a GitHub-style copy button that appears on hover in the top-right
corner of code blocks in the markdown preview. Clicking copies the code
block content to the clipboard and shows a checkmark confirmation.
@rushil-b-patel
Rushil Patel (rusp) (rushil-b-patel) force-pushed the main-copy-code-block-markdown-preview branch from a3b7b0c to b36a1a3 Compare July 15, 2026 16:42
@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Henning Dieterichs (@hediet) 👀 circling back on this : )

@hediet
Henning Dieterichs (hediet) enabled auto-merge (squash) July 16, 2026 07:58
@rushil-b-patel

Copy link
Copy Markdown
Contributor Author

Hello Benjamin Christopher Simmonds (@benibenj) : )
Checking up on this.

@hediet
Henning Dieterichs (hediet) merged commit 4f612f3 into microsoft:main Jul 28, 2026
29 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.132.0 milestone Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add copy button to code blocks in Markdown preview

6 participants