code block line numbers

This commit is contained in:
Joshua Seigler 2025-07-10 23:50:28 -04:00
parent 2766453d75
commit 0226171b58
7 changed files with 159 additions and 99 deletions

View file

@ -58,6 +58,19 @@ function addEffect({ target }: UIEvent) {
})
}
function addCodeBlockCopiers() {
document.querySelectorAll('pre[class^=language]').forEach(el => {
const copyButton = document.createElement('button')
copyButton.classList.add('copy-button')
copyButton.addEventListener('click', () => {
if (el instanceof HTMLElement) {
navigator.clipboard.writeText(el.innerText)
}
})
el.appendChild(copyButton)
})
}
function attend({ target}: UIEvent) {
if (!isElement(target) || !target.matches("a[href][target=_blank]")) {
return
@ -75,3 +88,5 @@ document.addEventListener("mouseleave", removeEffect, true)
document.addEventListener("blur", removeEffect, true)
document.addEventListener("click", attend, true)
document.addEventListener("DOMContentLoaded", addCodeBlockCopiers)