Skip to content

Commit

Permalink
fix: ensure code exists inside pre before adding clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Aug 20, 2023
1 parent ca17af4 commit 7b7064a
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions quartz/components/scripts/clipboard.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ document.addEventListener("nav", () => {
const els = document.getElementsByTagName("pre")
for (let i = 0; i < els.length; i++) {
const codeBlock = els[i].getElementsByTagName("code")[0]
const source = codeBlock.innerText.replace(/\n\n/g, "\n")
const button = document.createElement("button")
button.className = "clipboard-button"
button.type = "button"
button.innerHTML = svgCopy
button.ariaLabel = "Copy source"
button.addEventListener("click", () => {
navigator.clipboard.writeText(source).then(
() => {
button.blur()
button.innerHTML = svgCheck
setTimeout(() => {
button.innerHTML = svgCopy
button.style.borderColor = ""
}, 2000)
},
(error) => console.error(error),
)
})
els[i].prepend(button)
if (codeBlock) {
const source = codeBlock.innerText.replace(/\n\n/g, "\n")
const button = document.createElement("button")
button.className = "clipboard-button"
button.type = "button"
button.innerHTML = svgCopy
button.ariaLabel = "Copy source"
button.addEventListener("click", () => {
navigator.clipboard.writeText(source).then(
() => {
button.blur()
button.innerHTML = svgCheck
setTimeout(() => {
button.innerHTML = svgCopy
button.style.borderColor = ""
}, 2000)
},
(error) => console.error(error),
)
})
els[i].prepend(button)
}
}
})

0 comments on commit 7b7064a

Please sign in to comment.