-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
63 lines (60 loc) · 1.32 KB
/
script.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
function makeHeadDivs(l,e){
let c = Array.from(e.childNodes)
let h = Array.from(e.getElementsByTagName("h"+String(l)));
let s = [];
h.forEach(function(ch,i){
let li = c.indexOf(ch);
let hi;
if(i==h.length-1){
hi = -1;
}else{
hi = c.indexOf(h[i+1]);
}
if(hi<0){
s.push(c.slice(li));
}else{
s.push(c.slice(li,hi))
}
});
s.forEach(function(hs){
let ne = document.createElement("div");
ne.className = "h"+String(l);
ne.append(...hs);
e.append(ne);
makeHeadDivs(l+1,ne);
});
}
function getTocTree(l,e){
let c = [];
let h = Array.from(e.getElementsByClassName("h"+String(l)));
h.forEach(function(ch,i){
c[i] = {
0: ch.children[0],
1: getTocTree(l+1,ch)
};
});
return c;
}
function makeIDs(t,p){
t.forEach(function(hs,i){
hs[0].id = "h"+p+String(i+1);
makeIDs(hs[1],p+String(i+1)+".");
});
}
function makeToc(t,e){
let ul = document.createElement("ul");
ul.className = "tocUl"
t.forEach(function(hs){
h = hs[0];
li = document.createElement("li");
a = document.createElement("a");
a.href = "#"+h.id;
a.innerHTML = h.textContent;
li.append(h.id.slice(1)," ",a);
ul.append(li);
})
if (ul.children.length>0) e.append(ul);
t.forEach(function(hs,i){
makeToc(hs[1],ul.children[i])
})
}