🐛 fix: avoid copying code block line numbers (#647)
Some checks failed
Build Site / Check and Build for Pull Requests (push) Has been cancelled
Build Site / Build and Deploy on Main Push (push) Has been cancelled

Co-authored-by: welpo <welpo@users.noreply.github.com>
This commit is contained in:
aslowwriter 2026-05-01 09:31:47 +02:00 committed by GitHub
parent 17029f02b1
commit 35098abea3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View file

@ -27,7 +27,13 @@ const copyCodeAndChangeIcon = async (copyDiv, block) => {
}; };
const getNonTableCode = (block) => { const getNonTableCode = (block) => {
return [...block.querySelectorAll('code')].map((code) => code.textContent).join(''); return [...block.querySelectorAll('code')]
.map((code) => {
const clone = code.cloneNode(true);
clone.querySelectorAll('.giallo-ln').forEach(el => el.remove());
return clone.textContent;
})
.join('');
}; };
const getTableCode = (block) => { const getTableCode = (block) => {

View file

@ -1 +1 @@
const copiedText=document.getElementById("copy-success").textContent,initCopyText=document.getElementById("copy-init").textContent,changeIcon=(e,t)=>{e.classList.add(t),e.setAttribute("aria-label",copiedText),setTimeout(()=>{e.classList.remove(t),e.setAttribute("aria-label",initCopyText)},2500)},addCopyEventListenerToDiv=(e,t)=>{e.addEventListener("click",()=>copyCodeAndChangeIcon(e,t))},copyCodeAndChangeIcon=async(t,e)=>{e=(e.querySelector("table")?getTableCode:getNonTableCode)(e);try{await navigator.clipboard.writeText(e),changeIcon(t,"checked")}catch(e){changeIcon(t,"error")}},getNonTableCode=e=>[...e.querySelectorAll("code")].map(e=>e.textContent).join(""),getTableCode=e=>[...e.querySelectorAll("tr")].map(e=>e.querySelector("td:last-child")?.innerText??"").join("");document.querySelectorAll("pre:not(.mermaid)").forEach(e=>{var t=document.createElement("div");t.setAttribute("role","button"),t.setAttribute("aria-label",initCopyText),t.setAttribute("title",initCopyText),t.className="copy-code",e.prepend(t),addCopyEventListenerToDiv(t,e)}); let copiedText=document.getElementById("copy-success").textContent,initCopyText=document.getElementById("copy-init").textContent,changeIcon=(e,t)=>{e.classList.add(t),e.setAttribute("aria-label",copiedText),setTimeout(()=>{e.classList.remove(t),e.setAttribute("aria-label",initCopyText)},2500)},addCopyEventListenerToDiv=(e,t)=>{e.addEventListener("click",()=>copyCodeAndChangeIcon(e,t))},copyCodeAndChangeIcon=async(t,e)=>{e=(e.querySelector("table")?getTableCode:getNonTableCode)(e);try{await navigator.clipboard.writeText(e),changeIcon(t,"checked")}catch(e){changeIcon(t,"error")}},getNonTableCode=e=>[...e.querySelectorAll("code")].map(e=>{e=e.cloneNode(!0);return e.querySelectorAll(".giallo-ln").forEach(e=>e.remove()),e.textContent}).join(""),getTableCode=e=>[...e.querySelectorAll("tr")].map(e=>e.querySelector("td:last-child")?.innerText??"").join("");document.querySelectorAll("pre:not(.mermaid)").forEach(e=>{var t=document.createElement("div");t.setAttribute("role","button"),t.setAttribute("aria-label",initCopyText),t.setAttribute("title",initCopyText),t.className="copy-code",e.prepend(t),addCopyEventListenerToDiv(t,e)});