-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable code blocks copying Ref: https://aaronluna.dev/blog/add-copy-button-to-code-blocks-hugo-chroma/ Supersedes #5235 Signed-off-by: Noel Georgi <git@frezbo.dev>
- Loading branch information
Showing
6 changed files
with
178 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@docsearch/css@3"/> | ||
<link rel="stylesheet" type="text/css" href="/asciinema/asciinema-player.css" /> | ||
|
||
<!--- https://aaronluna.dev/blog/add-copy-button-to-code-blocks-hugo-chroma/ --> | ||
{{ if (findRE "<pre" .Content 1) }} | ||
<link rel="stylesheet" href="/css/clipboard.css"> | ||
{{ end }} |
This file contains 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,67 @@ | ||
.highlight-wrapper { | ||
display: block; | ||
} | ||
|
||
.highlight { | ||
position: relative; | ||
z-index: 0; | ||
padding: 0; | ||
margin: 0; | ||
border-radius: 4px; | ||
} | ||
|
||
code[data-lang]:before { | ||
color: #d0d0d0; | ||
background-color: #002b36; | ||
position: absolute; | ||
z-index: 0; | ||
top: -22px; | ||
left: 0px; | ||
content: attr(data-lang); | ||
border-top-left-radius: 4px; | ||
border-top-right-radius: 4px; | ||
border-bottom-left-radius: 0; | ||
border-bottom-right-radius: 0; | ||
padding: 5px 10px 7px; | ||
} | ||
|
||
.copy-code-button { | ||
position: absolute; | ||
z-index: 2; | ||
right: 0; | ||
top: 0; | ||
font-size: 13px; | ||
font-weight: 700; | ||
line-height: 14px; | ||
letter-spacing: 0.5px; | ||
width: 65px; | ||
color: #232326; | ||
background-color: #7f7f7f; | ||
border: 1.25px solid #232326; | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 4px; | ||
border-bottom-right-radius: 0; | ||
border-bottom-left-radius: 4px; | ||
white-space: nowrap; | ||
padding: 4px 4px 5px 4px; | ||
margin: 0 0 0 1px; | ||
cursor: pointer; | ||
opacity: 0.6; | ||
} | ||
|
||
.copy-code-button:hover, | ||
.copy-code-button:focus, | ||
.copy-code-button:active, | ||
.copy-code-button:active:hover { | ||
color: #222225; | ||
background-color: #b3b3b3; | ||
opacity: 0.8; | ||
} | ||
|
||
.copyable-text-area { | ||
position: absolute; | ||
height: 0; | ||
z-index: -1; | ||
opacity: .01; | ||
} | ||
|
This file contains 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,62 @@ | ||
function createCopyButton(highlightDiv) { | ||
const button = document.createElement("button"); | ||
button.className = "copy-code-button"; | ||
button.type = "button"; | ||
button.innerText = "Copy"; | ||
button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv)); | ||
addCopyButtonToDom(button, highlightDiv); | ||
} | ||
|
||
async function copyCodeToClipboard(button, highlightDiv) { | ||
const codeToCopy = highlightDiv.querySelector(":last-child > code").innerText; | ||
try { | ||
result = await navigator.permissions.query({ name: "clipboard-write" }); | ||
if (result.state == "granted" || result.state == "prompt") { | ||
await navigator.clipboard.writeText(codeToCopy); | ||
} else { | ||
copyCodeBlockExecCommand(codeToCopy, highlightDiv); | ||
} | ||
} catch (_) { | ||
copyCodeBlockExecCommand(codeToCopy, highlightDiv); | ||
} | ||
finally { | ||
codeWasCopied(button); | ||
} | ||
} | ||
|
||
function copyCodeBlockExecCommand(codeToCopy, highlightDiv) { | ||
const textArea = document.createElement("textArea"); | ||
textArea.contentEditable = 'true' | ||
textArea.readOnly = 'false' | ||
textArea.className = "copyable-text-area"; | ||
textArea.value = codeToCopy; | ||
highlightDiv.insertBefore(textArea, highlightDiv.firstChild); | ||
const range = document.createRange() | ||
range.selectNodeContents(textArea) | ||
const sel = window.getSelection() | ||
sel.removeAllRanges() | ||
sel.addRange(range) | ||
textArea.setSelectionRange(0, 999999) | ||
document.execCommand("copy"); | ||
highlightDiv.removeChild(textArea); | ||
} | ||
|
||
function codeWasCopied(button) { | ||
button.blur(); | ||
button.innerText = "Copied!"; | ||
setTimeout(function() { | ||
button.innerText = "Copy"; | ||
}, 2000); | ||
} | ||
|
||
function addCopyButtonToDom(button, highlightDiv) { | ||
highlightDiv.insertBefore(button, highlightDiv.firstChild); | ||
const wrapper = document.createElement("div"); | ||
wrapper.className = "highlight-wrapper"; | ||
highlightDiv.parentNode.insertBefore(wrapper, highlightDiv); | ||
wrapper.appendChild(highlightDiv); | ||
} | ||
|
||
document.querySelectorAll(".highlight") | ||
.forEach(highlightDiv => createCopyButton(highlightDiv)); | ||
|