From 051d5a0934962b23ccb030dccd7c29ba1f566097 Mon Sep 17 00:00:00 2001 From: Emile Bangma Date: Thu, 27 Mar 2025 17:30:29 +0000 Subject: [PATCH] feat(clipboard): clicking heading anchors copies link to clipboard --- quartz/components/scripts/clipboard.inline.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/quartz/components/scripts/clipboard.inline.ts b/quartz/components/scripts/clipboard.inline.ts index e16c11299..7a29259f7 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,27 @@ 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 anchorLink = `${window.location.href}${els[i].getAttribute("href")}` + if (els[i].getAttribute("href")) { + 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)) + } + } +})