From edb1f01c5d7f2c7ba8609590d68537755d0cdee9 Mon Sep 17 00:00:00 2001 From: John Bowdre Date: Sat, 4 Nov 2023 20:49:39 -0500 Subject: [PATCH] get the code copy button working again --- assets/js/code-copy-button.js | 16 ++++++++++++---- layouts/partials/footer.html | 4 ++-- layouts/partials/head.html | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/assets/js/code-copy-button.js b/assets/js/code-copy-button.js index 75848b3..71dc904 100644 --- a/assets/js/code-copy-button.js +++ b/assets/js/code-copy-button.js @@ -16,10 +16,18 @@ function createCopyButton(highlightDiv) { document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv)); async function copyCodeToClipboard(button, highlightDiv) { - // Need to get just the last-child of each .line element to avoid copying the line numbers - const nodeListToCopy = highlightDiv.querySelectorAll(":last-child > .chroma > code > .line > :last-child"); - // Reduce the nodeList to a string of text with each line separated by a newline - const codeToCopy = Array.from(nodeListToCopy).reduce((accumulator, line) => accumulator + line.innerText, ""); + // capture all code lines in the selected block which aren't classed `cmd_return` + let codeToCopy = highlightDiv.querySelectorAll(":last-child > .torchlight > code > .line:not(.cmd_return)"); + // now remove the first-child of each line which has class `line-number` + codeToCopy = Array.from(codeToCopy).reduce((accumulator, line) => { + if (line.firstChild.className != "line-number") { + return accumulator + line.innerText + "\n"; } + else { + return accumulator + Array.from(line.children).filter( + (child) => child.className != "line-number").reduce( + (accumulator, child) => accumulator + child.innerText, "") + "\n"; + } + }, ""); try { var result = await navigator.permissions.query({ name: "clipboard-write" }); if (result.state == "granted" || result.state == "prompt") { diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index 784aa97..7898ca5 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -4,6 +4,6 @@
View source.

{{ if (findRE " --> + {{ $jsCopy := resources.Get "js/code-copy-button.js" | minify }} + {{ end }} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 1f16216..7140f50 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -25,6 +25,6 @@ {{ if (findRE " - + {{ $copyCss := resources.Get "css/code-copy-button.css" | minify }} + {{ end }}