First commit for the website
This commit is contained in:
32
themes/terminal/assets/js/code.js
Normal file
32
themes/terminal/assets/js/code.js
Normal file
@@ -0,0 +1,32 @@
|
||||
const blockcodes = document.querySelectorAll(".chroma code[data-lang]");
|
||||
|
||||
for (const bc of blockcodes) {
|
||||
const parent = bc.parentElement;
|
||||
const content = bc.innerText.split("\n").filter(Boolean).join("\n");
|
||||
|
||||
// Code title
|
||||
const title = document.createElement("div");
|
||||
const lang = bc.dataset.lang;
|
||||
title.classList.add("code-title");
|
||||
title.innerText = lang;
|
||||
|
||||
// Copy to clipboard
|
||||
if (navigator.clipboard !== undefined) {
|
||||
const cpbutton = document.createElement("button");
|
||||
cpbutton.classList.add("copy-button");
|
||||
cpbutton.innerText = "Copy";
|
||||
|
||||
cpbutton.addEventListener("click", () => {
|
||||
cpbutton.innerText = "Copied";
|
||||
setTimeout(() => {
|
||||
cpbutton.innerText = "Copy";
|
||||
}, 1000);
|
||||
|
||||
navigator.clipboard.writeText(content);
|
||||
});
|
||||
|
||||
title.append(cpbutton);
|
||||
}
|
||||
|
||||
parent.closest(".highlight").prepend(title);
|
||||
}
|
||||
Reference in New Issue
Block a user