Skip to content

feat(markdown): add copy-to-clipboard button to code blocks in markdown #4751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benjaminrau
Copy link

@benjaminrau benjaminrau commented May 23, 2025

πŸ’‘ Feature: "Copy to Clipboard" Button for Code Blocks

✨ Description

This feature adds a "Copy to Clipboard" button to every code block rendered from Markdown. It allows users to easily copy code examples β€” especially useful when working with API usage snippets or tutorials.

πŸ”§ Implementation

  • Extended the markdown-it renderer to wrap fenced code blocks in a div.code-block-wrapper and attach a data-copy attribute containing the encoded code content.
  • Used useEffect to dynamically mount a <CopyToClipboard> React component into a placeholder within each code block after rendering.
  • Copy functionality is provided by react-copy-to-clipboard with visual feedback using react-hot-toast.
  • Added scoped styles in StyledWrapper.js:
    • Positioning and hover effect for the copy icon
    • Top padding inside <pre> blocks to make room for the button

πŸ” Preview

image

πŸ§ͺ Testing

Manually tested with Markdown files containing fenced code blocks (e.g., ```js) to ensure the button:

  • is visible,
  • copies content correctly,
  • shows a toast confirmation on click.

πŸ“ Files Changed

  • packages/bruno-app/src/components/MarkDown/index.jsx
  • packages/bruno-app/src/components/MarkDown/StyledWrapper.js

🧭 Branch Name

feature/copy-code-button

Enhances all fenced code blocks in Markdown rendering with a clipboard button.
The button is implemented via react-copy-to-clipboard and mounted dynamically
after Markdown rendering. Also includes styling and toast feedback.

Affects:
- index.jsx
- StyledWrapper.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant