-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjavascript.js
More file actions
36 lines (31 loc) · 977 Bytes
/
javascript.js
File metadata and controls
36 lines (31 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"use strict";
// all pre tags on the page
const pres = document.getElementsByTagName("pre"); //
// reformat html of pre tags
if (pres !== null) {
for (let i = 0; i < pres.length; i++) {
// check if its a pre tag with a prism class
if (isPrismClass(pres[i])) {
// insert code and copy element
pres[i].innerHTML = `<div class="copy">copy</div><code class="${pres[i].className}">${pres[i].innerHTML}</code>`;
}
}
}
// create clipboard for every copy element
const clipboard = new Clipboard('.copy', {
target: trigger => {
return trigger.nextElementSibling;
}
});
// do stuff when copy is clicked
clipboard.on('success', event => {
event.trigger.textContent = 'copied!';
setTimeout(() => {
event.clearSelection();
event.trigger.textContent = 'copy';
}, 2000);
});
// helper function
function isPrismClass(preTag) {
return preTag.className.substring(0, 8) === 'language';
}