-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (35 loc) · 1.13 KB
/
index.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
Array.from(document.querySelectorAll("pre code")).forEach(
(code) => (code.textContent = code.innerHTML.trim())
);
requestAnimationFrame(() => {
document.querySelectorAll(".input").forEach((input) => {
const wrapper = input.closest(".wrapper");
const wc = wrapper.querySelector(".wc");
const output = wrapper.querySelector(".output");
output.textContent = process(wc.innerHTML);
});
});
function process(str) {
var div = document.createElement("div");
div.innerHTML = str.trim();
return format(div, 0).innerHTML;
}
function format(node, level) {
const indentBefore = new Array(level++ + 1).join(" ");
const indentAfter = new Array(level - 1).join(" ");
for (let i = 0; i < node.children.length; i++) {
let textNode;
if (level === 1 && i === 0) {
textNode = document.createTextNode(indentBefore);
} else {
textNode = document.createTextNode("\n" + indentBefore);
}
node.insertBefore(textNode, node.children[i]);
format(node.children[i], level);
if (node.lastElementChild == node.children[i]) {
textNode = document.createTextNode("\n" + indentAfter);
node.appendChild(textNode);
}
}
return node;
}