-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
143 lines (137 loc) · 4 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function makeToc(contentElement, tocSelector, options) {
if (options == null) {
options = {};
}
if (contentElement == null) {
throw new Error('need to provide a selector where to scan for headers');
}
if (tocSelector == null) {
throw new Error('need to provide a selector where inject the TOC');
}
if (typeof contentElement === 'string') {
contentElement = document.querySelectorAll(contentElement + ' > *');
} else {
contentElement = contentElement.children;
}
var allChildren = Array.prototype.slice.call(contentElement);
var min = 6;
var headers = allChildren.filter(function(item) {
var classesList = item.className.split(' ');
if (classesList.indexOf("toc-ignore") != -1) {
return false;
}
if ((options.ignore || []).indexOf(getText(item)) != -1) {
return false;
}
var splitted = item.nodeName.split('');
var headingNumber = parseInt(splitted[1]);
if (splitted[0] === 'H' && headingNumber >= 1 && headingNumber <= (options.max || 6)) {
min = Math.min(min, headingNumber);
return true;
}
});
var hierarchy = createHierarchy(headers, min);
var toc = parseNodes(hierarchy.nodes);
var container = document.querySelector(tocSelector);
setText(container, '');
container.appendChild(toc);
}
function createHierarchy(headers, minLevel) {
var hierarchy = { nodes: [] };
window.hierarchy = hierarchy;
var previousNode = { parent: hierarchy };
var level = minLevel;
var init = false;
headers.forEach(function(header) {
var headingNumber = parseInt(header.nodeName.substr(1));
var object = {
title: getText(header),
link: window.location.pathname + '#' + header.id,
originLevel: headingNumber,
nodes: []
};
if (headingNumber === level) {
object.parent = previousNode.parent;
// keep level
} else if (headingNumber - level >= 1) {
// go one step deeper, regardless how much
// the difference between headingNumber and level is
if (init === false) {
var missingParent = {
parent: previousNode.parent,
title: '',
link: '',
originLevel: NaN,
nodes: []
};
previousNode.parent.nodes.push(missingParent);
previousNode = missingParent;
}
object.parent = previousNode;
level++;
} else if (level - headingNumber >= 1) {
// go one or more step up again
var ref = previousNode.parent;
while (level - headingNumber >= 1) {
ref = ref.parent;
level--;
}
object.parent = ref;
} else {
console.error('unkown toc path');
}
object.parent.nodes.push(object);
previousNode = object;
init = true;
});
return hierarchy;
}
function parseNodes(nodes) {
var ul = document.createElement("UL");
for(var i=0; i<nodes.length; i++) {
ul.appendChild(parseNode(nodes[i]));
}
return ul;
}
function parseNode(node) {
var li = document.createElement("LI");
var a = document.createElement("A");
setText(a, node.title);
a.href = node.link;
li.appendChild(a);
if(node.nodes) {
li.appendChild(parseNodes(node.nodes));
}
return li;
}
function getText(elem) {
if (elem.textContent != null) {
return elem.textContent;
} else {
elem.innerText;
}
}
function setText(elem, value) {
if (elem.textContent != null) {
elem.textContent = value;
} else {
elem.innerText = value;
}
}
module.exports = makeToc;
module.exports.update = function() {
var element = document.querySelector("[data-toc]");
if (element != null) {
var options = {};
var ignore = (element.attributes.getNamedItem("data-toc-ignore")||{}).value
var max = (element.attributes.getNamedItem("data-toc-max")||{}).value
if (ignore != null) {
options.ignore = ignore;
}
if (max != null) {
options.max = parseInt(max);
}
makeToc(element.parentNode, '[data-toc]', options);
}
};
window.addEventListener('load', module.exports.update);