Skip to content

Add copy-to-clipboard support to code blocks #1273

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 9 commits into
base: main
Choose a base branch
from

Conversation

DebugSteven
Copy link
Contributor

@DebugSteven DebugSteven commented Aug 6, 2025

Summary

This PR adds support for a new copy-to-clipboard button in code blocks. When this feature is enabled through the enable-experimental-code-block flag, a copy-to-clipboard button is rendered in the top-right corner of all code blocks, allowing users to easily copy its contents. When an author does not want the contents of a code block to be copyable, a nocopy option can be used to disable the copy-to-clipboard button.

User Experience

When the enable-experimental-code-block flag is used, a copy button will appear in the top-right corner of all code blocks. Clicking the button copies the full contents of the code block to the clipboard and displays a checkmark to confirm success. If a code block includes the nocopy keyword in the language line, the copy button will not appear on that code block.

Implementation Overview

  • In swift-docc, this change adds a feature flag enable-experimental-code-block, which enables a copy-to-clipboard button on code blocks by default.
  • Parses the nocopy option from the language line in triple-backtick code blocks to disable the copy button on that code block.
  • A copyToClipboard property was added to RenderBlockContent.codeListing passed to the renderer.
  • This flag is forwarded to swift-docc-render. The accompanying branch/PR is here: Add copy-to-clipboard support to code blocks swift-docc-render#961

Dependencies

This PR depends on associated changes in swift-docc-render to actually render and handle the copy button.

Testing

Setup

  1. Use the codeblock-copy branches for swift-docc and swift-docc-render with the copy-to-clipboard changes.
  2. Rebuild documentation using swift-docc with the feature flag enable-experimental-code-block and serve it using a local build of swift-docc-render.

How to Test

  1. In all code listings with the enable-experimental-code-block flag, a copy button will appear in the top-right corner.
  2. Click the copy icon. The code should be copied to your clipboard and the icon should update to a checkmark briefly. Paste to verify the copy functionality works.

To disable the copy icon with the feature flag enabled:

  1. In any code listing using ``` or by adding a code listing, add the nocopy option like this:
```swift, nocopy
print(“Hello, world!”)
```


or like this:


```nocopy
print(“Hello, world!”)
```


2. Verify the copy button does not appear on this code block.

Screenshot 2025-08-11 at 5 47 54 PM

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests – testCopyToClipboard() in RenderContentCompilerTests.swift
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary – I added copyToClipboard as a property of CodeListing in RenderNode.spec.json. I also added a subsection to Formatting Your Documentation Content. Please let me know if there’s any other documentation I should update.

Shown with cursor hovering over the 2nd copy-to-clipboard button on screen:
Screenshot 2025-08-11 at 4 58 58 PM
Screenshot 2025-08-11 at 5 34 53 PM

Shown with a narrow viewport:
Screenshot 2025-08-11 at 4 59 54 PM

Screen.Recording.2025-08-11.at.5.00.31.PM.mov

@heckj
Copy link
Member

heckj commented Aug 6, 2025

@swift-ci please test

@d-ronnqvist d-ronnqvist added the needs forum discussion Needs to be discussed in the Swift Forums label Aug 7, 2025
@d-ronnqvist
Copy link
Contributor

Since this is adding new user-facing syntax I'm adding the needs-forum-discussion tag until the community has had time to discuss the new syntax.

@d-ronnqvist
Copy link
Contributor

There is a forum thread for this here. It would be good for that thread to cover some discussion on future directions so that we feel comfortable that this syntax can scale to accommodate those future directions (or intentionally not support them if we believe that each would benefit from a different user-facing syntax).

@heckj heckj self-assigned this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs forum discussion Needs to be discussed in the Swift Forums
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants