-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc.js
More file actions
33 lines (25 loc) · 902 Bytes
/
Copy pathtoc.js
File metadata and controls
33 lines (25 loc) · 902 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
// Get ToC div
toc = document.getElementById("ToC");
//Add a header
tocHeader = document.createElement("h2");
tocHeader.innerText="Table of contents";
toc.appendChild(tocHeader);
// Create a list for the ToC entries
tocList = document.createElement("ul");
// Get the h1 tags - ToC entries
headers = document.getElementsByTagName("h1");
// For each h1
for (i = 0; i < headers.length; i++){
// Create an id
name = "h"+i;
headers[i].id=name;
// a list item for the entry
tocListItem = document.createElement("li");
// a link for the h1
tocEntry = document.createElement("a");
tocEntry.setAttribute("href","#"+name);
tocEntry.innerText=headers[i].innerText;
tocListItem.appendChild(tocEntry);
tocList.appendChild(tocListItem);
}
toc.appendChild(tocList);