mirror of
https://github.com/nunocoracao/blowfish.git
synced 2026-01-30 15:31:52 +00:00
fix(code.js): filter line number on code copy
This commit is contained in:
+20
-1
@@ -17,7 +17,7 @@ function createCopyButton(highlightDiv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function copyCodeToClipboard(button, highlightDiv) {
|
async function copyCodeToClipboard(button, highlightDiv) {
|
||||||
const codeToCopy = highlightDiv.querySelector(":last-child").innerText;
|
const codeToCopy = getCleanCodeText(highlightDiv);
|
||||||
try {
|
try {
|
||||||
result = await navigator.permissions.query({ name: "clipboard-write" });
|
result = await navigator.permissions.query({ name: "clipboard-write" });
|
||||||
if (result.state == "granted" || result.state == "prompt") {
|
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) {
|
function copyCodeBlockExecCommand(codeToCopy, highlightDiv) {
|
||||||
const textArea = document.createElement("textArea");
|
const textArea = document.createElement("textArea");
|
||||||
textArea.contentEditable = "true";
|
textArea.contentEditable = "true";
|
||||||
|
|||||||
Reference in New Issue
Block a user