-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathglobal.js
64 lines (49 loc) · 1.91 KB
/
global.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Website scripts
import "https://prismjs.com/prism.js";
import "https://prismjs.com/plugins/normalize-whitespace/prism-normalize-whitespace.js";
import "https://prismjs.com/components/prism-typescript.js";
const $$ = document.querySelectorAll.bind(document);
function renderDemos () {
for (let code of $$("pre > code.language-html, pre.language-html > code")) {
let pre = code.parentNode;
if (!pre.previousElementSibling?.matches(".demo, .demo-ignore")) {
code.parentNode.insertAdjacentHTML("beforebegin", `<div class="demo">${code.textContent}</div>`);
let demoDiv = pre.previousElementSibling;
let scripts = demoDiv.querySelectorAll("script");
if (scripts.length > 0) {
for (let script of scripts) {
script.replaceWith( clone(script) );
}
}
}
}
}
// Wrap all elements with data-alternates attribute in a div.alternates-container
for (let el of $$("[data-alternates]")) {
let alternates = el.dataset.alternates.split("\n");
// alternates.splice(0, 0, el.textContent);
let wrappedAlternates = alternates.map(alternate => `<div class="alternate">${alternate}</div>`).join("\n");
el.innerHTML = wrappedAlternates;
let activeChild = 0;
el.children[activeChild].classList.add("active");
setInterval(() => {
el.children[activeChild].classList.remove("active");
activeChild = (activeChild + 1) % el.children.length;
el.children[activeChild].classList.add("active");
}, 2500);
// let wrapper = Object.assign(document.createElement("div"), {
// className: "alternates-container",
// });
// wrapper.style.setProperty("--alternates-count", el.dataset.alternates.split("\n").length);
// el.parentNode.insertBefore(wrapper, el);
// wrapper.appendChild(el);
}
renderDemos();
function clone (node) {
let ret = document.createElement(node.tagName);
ret.innerHTML = node.innerHTML;
for (let attr of node.getAttributeNames()) {
ret.setAttribute(attr, node.getAttribute(attr));
}
return ret;
}