-
Notifications
You must be signed in to change notification settings - Fork 162
[Repo Assist] Add copy-to-clipboard button to code blocks #998
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
Merged
nojaf
merged 7 commits into
main
from
repo-assist/fix-issue-72-copy-code-to-clipboard-e89f0c91d528a99a
Feb 26, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7242cc
Add copy-to-clipboard button to code blocks in generated docs
github-actions[bot] e17e000
Merge branch 'main' into repo-assist/fix-issue-72-copy-code-to-clipbo…
dsyme dea204e
Merge branch 'main' into repo-assist/fix-issue-72-copy-code-to-clipbo…
dsyme 1dbcd8a
Merge branch 'main' into repo-assist/fix-issue-72-copy-code-to-clipbo…
dsyme 63efe20
Use navigator.clipboard in Clipboard_CopyTo with execCommand fallback
github-actions[bot] a4214b3
ci: trigger CI checks
github-actions[bot] c22f5f2
Merge branch 'main' into repo-assist/fix-issue-72-copy-code-to-clipbo…
dsyme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Adds a "Copy" button to every code block so readers can easily copy snippets. | ||
| function createCopyButton() { | ||
| const button = document.createElement('button') | ||
| button.className = 'copy-code-button' | ||
| button.setAttribute('aria-label', 'Copy code to clipboard') | ||
| button.textContent = 'Copy' | ||
| return button | ||
| } | ||
|
|
||
| function attachCopyHandler(button, getText) { | ||
| button.addEventListener('click', function () { | ||
| const text = getText() | ||
| if (navigator.clipboard && navigator.clipboard.writeText) { | ||
| navigator.clipboard.writeText(text).then( | ||
| function () { | ||
| button.textContent = 'Copied!' | ||
| setTimeout(function () { | ||
| button.textContent = 'Copy' | ||
| }, 2000) | ||
| }, | ||
| function () { | ||
| button.textContent = 'Failed' | ||
| setTimeout(function () { | ||
| button.textContent = 'Copy' | ||
| }, 2000) | ||
| } | ||
| ) | ||
| } else { | ||
| // Fallback for non-HTTPS environments | ||
| const el = document.createElement('textarea') | ||
| el.value = text | ||
| document.body.appendChild(el) | ||
| el.select() | ||
| document.execCommand('copy') | ||
| document.body.removeChild(el) | ||
| button.textContent = 'Copied!' | ||
| setTimeout(function () { | ||
| button.textContent = 'Copy' | ||
| }, 2000) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function () { | ||
| // table.pre blocks (F# highlighted code, sometimes with line numbers) | ||
| document.querySelectorAll('table.pre').forEach(function (table) { | ||
| const wrapper = document.createElement('div') | ||
| wrapper.className = 'code-block-wrapper' | ||
| table.parentNode.insertBefore(wrapper, table) | ||
| wrapper.appendChild(table) | ||
|
|
||
| const button = createCopyButton() | ||
| wrapper.appendChild(button) | ||
|
|
||
| const snippet = table.querySelector('.snippet pre') | ||
| attachCopyHandler(button, function () { | ||
| return (snippet || table).innerText | ||
| }) | ||
| }) | ||
|
|
||
| // Standard pre > code blocks (Markdown fenced code, standalone fssnip, etc.) | ||
| // Skip those already handled inside table.pre above. | ||
| document.querySelectorAll('pre > code').forEach(function (code) { | ||
| if (code.closest('table.pre')) return | ||
| const pre = code.parentElement | ||
| pre.classList.add('has-copy-button') | ||
|
|
||
| const button = createCopyButton() | ||
| pre.appendChild(button) | ||
|
|
||
| attachCopyHandler(button, function () { | ||
| return code.innerText | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/repo-assist while on topic, please update
docs/content/fsdocs-tips.jsas well to use navigator.clipboard.