Skip to content

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

Open
rushil-b-patel wants to merge 1 commit into
microsoft:mainfrom
rushil-b-patel:main-copy-code-block-markdown-preview
Open

feat: add copy button to code blocks in markdown preview#323609
rushil-b-patel wants to merge 1 commit into
microsoft:mainfrom
rushil-b-patel:main-copy-code-block-markdown-preview

Conversation

@rushil-b-patel

@rushil-b-patel rushil-b-patel commented Jun 29, 2026

Copy link
Copy Markdown

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
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-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
Author

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

@rushil-b-patel

Copy link
Copy Markdown
Author

Hello @vritant24 Can you please delegate to appropriate person for this PR ?
Thanks

@vritant24 vritant24 requested a review from hediet July 1, 2026 20:04
@rushil-b-patel

Copy link
Copy Markdown
Author

Hello @hediet any update here :) !

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

hediet commented Jul 7, 2026

Copy link
Copy Markdown
Member

Thanks for the PR!

@rushil-b-patel

rushil-b-patel commented Jul 7, 2026

Copy link
Copy Markdown
Author

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

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.
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-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
Author

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

Thanks : )

@rushil-b-patel rushil-b-patel requested a review from hediet July 7, 2026 12:24
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

5 participants