From 65637ec3f0f95015e5f0c8691fcd05368dddc329 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Sat, 16 Aug 2025 16:41:46 +0800 Subject: [PATCH 1/3] fix(code.js): filter line number on code copy --- assets/js/code.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/assets/js/code.js b/assets/js/code.js index 8639ad8d..f2efe534 100644 --- a/assets/js/code.js +++ b/assets/js/code.js @@ -17,7 +17,7 @@ function createCopyButton(highlightDiv) { } async function copyCodeToClipboard(button, highlightDiv) { - const codeToCopy = highlightDiv.querySelector(":last-child").innerText; + const codeToCopy = getCleanCodeText(highlightDiv); try { result = await navigator.permissions.query({ name: "clipboard-write" }); if (result.state == "granted" || result.state == "prompt") { @@ -32,6 +32,25 @@ async function copyCodeToClipboard(button, highlightDiv) { } } +function getCleanCodeText(highlightDiv) { + const codeElement = highlightDiv.querySelector("code"); + if (!codeElement) return ""; + + const contentElements = codeElement.querySelectorAll(".cl"); + + if (contentElements.length > 0) { + const lines = Array.from(contentElements).map((el) => el.textContent.replace(/\n$/, "")); + return lines.join("\n"); + } + + const tableCell = highlightDiv.querySelector(".lntable .lntd:last-child code"); + if (tableCell) { + return tableCell.textContent.trim(); + } + + return codeElement.textContent.trim(); +} + function copyCodeBlockExecCommand(codeToCopy, highlightDiv) { const textArea = document.createElement("textArea"); textArea.contentEditable = "true"; From 9d0aecf41fcd10795f47a350148fe656ce49f93f Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Thu, 21 Aug 2025 05:40:30 +0800 Subject: [PATCH 2/3] refactor(code.js): simplify code uses optional chaining inlines short functions --- assets/js/code.js | 80 +++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/assets/js/code.js b/assets/js/code.js index f2efe534..173d8b6e 100644 --- a/assets/js/code.js +++ b/assets/js/code.js @@ -1,10 +1,6 @@ var scriptBundle = document.getElementById("script-bundle"); -var copyText = - scriptBundle && scriptBundle.getAttribute("data-copy") ? scriptBundle.getAttribute("data-copy") : "Copy"; -var copiedText = - scriptBundle && scriptBundle.getAttribute("data-copied") - ? scriptBundle.getAttribute("data-copied") - : "Copied"; +var copyText = scriptBundle?.getAttribute("data-copy") || "Copy"; +var copiedText = scriptBundle?.getAttribute("data-copied") || "Copied"; function createCopyButton(highlightDiv) { const button = document.createElement("button"); @@ -13,26 +9,53 @@ function createCopyButton(highlightDiv) { button.ariaLabel = copyText; button.innerText = copyText; button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv)); - 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); } async function copyCodeToClipboard(button, highlightDiv) { - const codeToCopy = getCleanCodeText(highlightDiv); + const codeToCopy = getCodeText(highlightDiv); + + function fallback(codeToCopy, highlightDiv) { + const textArea = document.createElement("textArea"); + textArea.contentEditable = "true"; + textArea.readOnly = "false"; + textArea.className = "copy-textarea"; + 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); + } + 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); + fallback(codeToCopy, highlightDiv); } } catch (_) { - copyCodeBlockExecCommand(codeToCopy, highlightDiv); + fallback(codeToCopy, highlightDiv); } finally { - codeWasCopied(button); + button.blur(); + button.innerText = copiedText; + setTimeout(function () { + button.innerText = copyText; + }, 2000); } } -function getCleanCodeText(highlightDiv) { +function getCodeText(highlightDiv) { const codeElement = highlightDiv.querySelector("code"); if (!codeElement) return ""; @@ -51,39 +74,6 @@ function getCleanCodeText(highlightDiv) { return codeElement.textContent.trim(); } -function copyCodeBlockExecCommand(codeToCopy, highlightDiv) { - const textArea = document.createElement("textArea"); - textArea.contentEditable = "true"; - textArea.readOnly = "false"; - textArea.className = "copy-textarea"; - 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 = copiedText; - setTimeout(function () { - button.innerText = copyText; - }, 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); -} - window.addEventListener("DOMContentLoaded", (event) => { document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv)); }); From 08401697242753fbafc8bfc1d29f93fc8e2f1a3b Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Thu, 21 Aug 2025 06:00:26 +0800 Subject: [PATCH 3/3] chore(code.js): add linenos comments --- assets/js/code.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/assets/js/code.js b/assets/js/code.js index 173d8b6e..2cc852ec 100644 --- a/assets/js/code.js +++ b/assets/js/code.js @@ -56,22 +56,22 @@ async function copyCodeToClipboard(button, highlightDiv) { } function getCodeText(highlightDiv) { - const codeElement = highlightDiv.querySelector("code"); - if (!codeElement) return ""; + const codeBlock = highlightDiv.querySelector("code"); + const inlineLines = codeBlock?.querySelectorAll(".cl"); // linenos=inline + const tableCodeCell = highlightDiv?.querySelector(".lntable .lntd:last-child code"); // linenos=table - const contentElements = codeElement.querySelectorAll(".cl"); + if (!codeBlock) return ""; - if (contentElements.length > 0) { - const lines = Array.from(contentElements).map((el) => el.textContent.replace(/\n$/, "")); - return lines.join("\n"); + if (inlineLines.length > 0) { + const cleanedLines = Array.from(inlineLines).map((line) => line.textContent.replace(/\n$/, "")); + return cleanedLines.join("\n"); } - const tableCell = highlightDiv.querySelector(".lntable .lntd:last-child code"); - if (tableCell) { - return tableCell.textContent.trim(); + if (tableCodeCell) { + return tableCodeCell.textContent.trim(); } - return codeElement.textContent.trim(); + return codeBlock.textContent.trim(); } window.addEventListener("DOMContentLoaded", (event) => {