mirror of
https://github.com/thegeeklab/hugo-geekblog.git
synced 2024-11-24 22:10:39 +00:00
fix: fix exception while creating the code copy button (#260)
This commit is contained in:
parent
f9437ae997
commit
4bb0ba4024
@ -1,23 +1,26 @@
|
|||||||
export function createCopyButton(highlightDiv) {
|
export function createCopyButton(highlightDiv) {
|
||||||
const button = document.createElement("span")
|
const button = document.createElement("span")
|
||||||
let selector = "pre > code"
|
|
||||||
|
|
||||||
|
let codeSelector = "pre > code"
|
||||||
if (highlightDiv.querySelector(".lntable")) {
|
if (highlightDiv.querySelector(".lntable")) {
|
||||||
selector = ".lntable .lntd:last-child pre > code"
|
codeSelector = ".lntable .lntd:last-child pre > code"
|
||||||
}
|
}
|
||||||
|
|
||||||
const codeToCopy = highlightDiv.querySelector(selector).innerText.trim()
|
const codeContainer = highlightDiv.querySelector(codeSelector)
|
||||||
|
if (codeContainer !== null) {
|
||||||
|
const codeContent = codeContainer.innerText.trim()
|
||||||
|
|
||||||
button.classList.add("flex", "align-center", "justify-center", "clip", "gblog-post__codecopy")
|
button.classList.add("flex", "align-center", "justify-center", "clip", "gblog-post__codecopy")
|
||||||
button.type = "button"
|
button.type = "button"
|
||||||
button.innerHTML =
|
button.innerHTML =
|
||||||
'<svg class="gblog-icon copy"><use xlink:href="#gblog_copy"></use></svg>' +
|
'<svg class="gblog-icon copy"><use xlink:href="#gblog_copy"></use></svg>' +
|
||||||
'<svg class="gblog-icon check hidden"><use xlink:href="#gblog_check"></use></svg>'
|
'<svg class="gblog-icon check hidden"><use xlink:href="#gblog_check"></use></svg>'
|
||||||
button.setAttribute("data-clipboard-text", codeToCopy)
|
button.setAttribute("data-clipboard-text", codeContent)
|
||||||
button.setAttribute("data-copy-feedback", "Copied!")
|
button.setAttribute("data-copy-feedback", "Copied!")
|
||||||
button.setAttribute("role", "button")
|
button.setAttribute("role", "button")
|
||||||
button.setAttribute("aria-label", "Copy")
|
button.setAttribute("aria-label", "Copy")
|
||||||
|
|
||||||
highlightDiv.classList.add("gblog-post__codecontainer")
|
highlightDiv.classList.add("gblog-post__codecontainer")
|
||||||
highlightDiv.insertBefore(button, highlightDiv.firstChild)
|
highlightDiv.insertBefore(button, highlightDiv.firstChild)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user