forked from ryanhanwu/How-To-Ask-Questions-The-Smart-Way
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.js
22 lines (21 loc) · 768 Bytes
/
toc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(function(window) {
String.prototype.repeat = function(num) {
return new Array(num + 1).join(this);
};
var url = document.URL,
headers = jQuery("article :header"),
result;
for (var i = 3; i < headers.length; i++) { //skip H1, history, and toc
var header = headers[i],
headerText = header.textContent.trim();
var anchorText = headerText.toLowerCase();
anchorText = anchorText.replace(" ", "-");
anchorText = anchorText.replace(/,|:|、/g, "");
var hIndex = parseInt(header.nodeName.substring(1)) - 1,
indent = " ".repeat(hIndex),
link = ['<pre>', indent, '* [', headerText, '](', '#', anchorText, ')', '\n', '</pre>'];
result += link.join('');
}
var win = window.open("", "win");
win.document.body.innerHTML = result;
})(window);