chore(code.js): add linenos comments

This commit is contained in:
ZhenShuo Leo
2025-08-21 06:00:26 +08:00
parent 9d0aecf41f
commit 0840169724

View File

@@ -56,22 +56,22 @@ async function copyCodeToClipboard(button, highlightDiv) {
} }
function getCodeText(highlightDiv) { function getCodeText(highlightDiv) {
const codeElement = highlightDiv.querySelector("code"); const codeBlock = highlightDiv.querySelector("code");
if (!codeElement) return ""; 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) { if (inlineLines.length > 0) {
const lines = Array.from(contentElements).map((el) => el.textContent.replace(/\n$/, "")); const cleanedLines = Array.from(inlineLines).map((line) => line.textContent.replace(/\n$/, ""));
return lines.join("\n"); return cleanedLines.join("\n");
} }
const tableCell = highlightDiv.querySelector(".lntable .lntd:last-child code"); if (tableCodeCell) {
if (tableCell) { return tableCodeCell.textContent.trim();
return tableCell.textContent.trim();
} }
return codeElement.textContent.trim(); return codeBlock.textContent.trim();
} }
window.addEventListener("DOMContentLoaded", (event) => { window.addEventListener("DOMContentLoaded", (event) => {