diff --git a/quartz/components/scripts/clipboard.inline.ts b/quartz/components/scripts/clipboard.inline.ts index e16c11299..56a60ef38 100644 --- a/quartz/components/scripts/clipboard.inline.ts +++ b/quartz/components/scripts/clipboard.inline.ts @@ -3,6 +3,7 @@ const svgCopy = const svgCheck = '' +// Code block copy button document.addEventListener("nav", () => { const els = document.getElementsByTagName("pre") for (let i = 0; i < els.length; i++) { @@ -35,3 +36,28 @@ document.addEventListener("nav", () => { } } }) + +// Header anchor copy button +document.addEventListener("nav", () => { + const els = document.querySelectorAll("a[role=anchor]") + for (let i = 0; i < els.length; i++) { + const href = els[i].getAttribute("href") + if (href) { + const anchorLink = new URL(href, window.location.href).toString() + const svgAnchor = els[i].innerHTML + function onClick() { + navigator.clipboard.writeText(anchorLink).then( + () => { + els[i].innerHTML = svgCheck + setTimeout(() => { + els[i].innerHTML = svgAnchor + }, 2000) + }, + (error) => console.error(error), + ) + } + els[i].addEventListener("click", onClick) + window.addCleanup(() => els[i].removeEventListener("click", onClick)) + } + } +})