-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add copy button to markdown code blocks #17638
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
Merged
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
46b9a27
Add copy button to markdown code blocks
silverwind e6a4733
add svg module tests
silverwind a742cf7
fix sanitizer regexp
silverwind d360b09
remove outdated comment
silverwind eb1344d
vertically center button in issue comments as well
silverwind 466d4f6
add comment to css
silverwind a169d36
fix undefined on view file line copy
silverwind 028e533
combine animation less files
silverwind b5323e5
Update modules/markup/markdown/markdown.go
silverwind 139d7d2
add test for different sizes
silverwind 0b73749
add cloneNode and add tests for it
silverwind 9e854e1
use deep clone
silverwind 970285e
remove useless optional chaining
silverwind 5ae4fc5
remove the svg node cache
silverwind b136b51
unify clipboard copy string and i18n
silverwind 01fe505
remove unused var
silverwind 610d0e4
remove unused localization
silverwind 5b7b65e
Merge branch 'main' into code-copy
wxiaoguang 1b83022
minor css tweaks to the button
silverwind b51cc9d
comment tweak
silverwind f7ce664
remove useless attribute
silverwind 57818e0
Merge branch 'main' into code-copy
wxiaoguang 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,17 @@ | ||
import {svg} from '../svg.js'; | ||
|
||
export function renderCodeCopy() { | ||
const els = document.querySelectorAll('.markup .code-block code'); | ||
if (!els.length) return; | ||
|
||
const button = document.createElement('button'); | ||
button.classList.add('code-copy', 'ui', 'button'); | ||
button.setAttribute('data-variation', 'inverted tiny'); | ||
button.innerHTML = svg('octicon-copy'); | ||
|
||
for (const el of els) { | ||
const btn = button.cloneNode(true); | ||
btn.setAttribute('data-clipboard-text', el.textContent); | ||
el.after(btn); | ||
} | ||
} |
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
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,7 @@ | ||
import {svg} from './svg.js'; | ||
|
||
test('svg', () => { | ||
expect(svg('octicon-repo')).toStartWith('<svg'); | ||
expect(svg('octicon-repo', 16)).toInclude('width="16"'); | ||
expect(svg('octicon-repo', 32)).toInclude('width="32"'); | ||
}); |
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,32 @@ | ||
.markup .code-block { | ||
position: relative; | ||
} | ||
|
||
.markup .code-copy { | ||
position: absolute; | ||
top: .5rem; | ||
right: .5rem; | ||
padding: 10px; | ||
visibility: hidden; | ||
animation: fadeout .2s both; | ||
} | ||
|
||
/* comment content has 14px font size, reduce padding to make the button appear | ||
vertically centered on single-line content, like it does elsewhere */ | ||
.repository.view.issue .comment-list .comment .markup .code-copy { | ||
padding: 9px; | ||
} | ||
|
||
/* can not use regular transparent button colors for hover and active states because | ||
we need opaque colors here as code can appear behind the button */ | ||
.markup .code-copy:hover { | ||
background: var(--color-secondary) !important; | ||
} | ||
.markup .code-copy:active { | ||
background: var(--color-secondary-dark-1) !important; | ||
} | ||
|
||
.markup .code-block:hover .code-copy { | ||
visibility: visible; | ||
animation: fadein .2s both; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.