fix: don't add copy button if already present

This commit is contained in:
Stevan Freeborn
2024-04-14 13:03:26 -05:00
parent 5b22042ba1
commit 0bc94e2a59
+4 -15
View File
@@ -1,30 +1,15 @@
let initialized = false;
export function onLoad() { export function onLoad() {
if (initialized) {
return;
}
init(); init();
} }
export function onUpdate() { export function onUpdate() {
if (initialized) {
return;
}
init(); init();
} }
export function onDispose() {
initialized = false;
}
function init(){ function init(){
addAnchors(); addAnchors();
addClipboard(); addClipboard();
Prism.highlightAll(); Prism.highlightAll();
initialized = true;
} }
/** /**
@@ -53,6 +38,10 @@ function addClipboard() {
const codeBlocks = document.querySelectorAll('pre'); const codeBlocks = document.querySelectorAll('pre');
codeBlocks.forEach((block) => { codeBlocks.forEach((block) => {
if (block.querySelector('button.copy-button')) {
return;
}
const button = document.createElement('button'); const button = document.createElement('button');
button.className = 'copy-button'; button.className = 'copy-button';
button.type = 'button'; button.type = 'button';