-
Notifications
You must be signed in to change notification settings - Fork 147
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
base: main
Are you sure you want to change the base?
Conversation
@swift-ci please test |
Since this is adding new user-facing syntax I'm adding the |
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). |
c89047e
to
d0b75d8
Compare
d0b75d8
to
1f4894d
Compare
…rd and other code block annotations
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, anocopy
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 thenocopy
keyword in the language line, the copy button will not appear on that code block.Implementation Overview
swift-docc
, this change adds a feature flagenable-experimental-code-block
, which enables a copy-to-clipboard button on code blocks by default.nocopy
option from the language line in triple-backtick code blocks to disable the copy button on that code block.copyToClipboard
property was added toRenderBlockContent.codeListing
passed to the renderer.swift-docc-render
. The accompanying branch/PR is here: Add copy-to-clipboard support to code blocks swift-docc-render#961Dependencies
This PR depends on associated changes in
swift-docc-render
to actually render and handle the copy button.Testing
Setup
swift-docc
andswift-docc-render
with the copy-to-clipboard changes.swift-docc
with the feature flagenable-experimental-code-block
and serve it using a local build ofswift-docc-render
.How to Test
enable-experimental-code-block
flag, a copy button will appear in the top-right corner.To disable the copy icon with the feature flag enabled:
```
or by adding a code listing, add thenocopy
option like this:
or like this:
2. Verify the copy button does not appear on this code block.
Checklist
Make sure you check off the following items. If they cannot be completed, provide a reason.
testCopyToClipboard()
inRenderContentCompilerTests.swift
./bin/test
script and it succeededcopyToClipboard
as a property ofCodeListing
inRenderNode.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:


Shown with a narrow viewport:

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