1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 17:46:39 -04:00

trim surrounding newlines from copied code

This commit is contained in:
2021-08-16 08:42:34 -04:00
parent fdb9532cc5
commit 6e27cecba1
3 changed files with 13 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import ClipboardJS from "clipboard";
import trimNewlines from "trim-newlines";
// the default text of the copy button:
const copyTerm = "Copy";
@ -15,8 +16,12 @@ if (ClipboardJS.isSupported()) {
highlightDiv.insertBefore(button, highlightDiv.firstChild);
new ClipboardJS(button, {
// actual code element will have class "language-*", even if plaintext
text: (trigger) => trigger.parentElement.querySelector('code[class^="language-"]').innerText, // eslint-disable-line quotes
text: (trigger) => {
// actual code element will (should) have class "language-*", even if plaintext
const fenceElement = trigger.parentElement.querySelector('code[class^="language-"]'); // eslint-disable-line quotes
return fenceElement ? trimNewlines(fenceElement.innerText) : false;
},
}).on("success", (e) => {
// show a subtle indication of success
e.trigger.innerText = "✓";