-
Notifications
You must be signed in to change notification settings - Fork 2
/
toc.js
170 lines (128 loc) Β· 3.82 KB
/
toc.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var defaultOptions = {
headings: 'h1, h2, h3, h4, h5, h6',
scope: '.markdown-section',
// To make work
title: 'On this page',
listType: 'ul',
}
// Element builders
var tocHeading = function(Title) {
return document.createElement('h2').appendChild(
document.createTextNode(Title)
)
}
var aTag = function(src) {
var a = document.createElement('a');
var content = src.firstChild.innerHTML;
// Use this to clip text w/ HTML in it.
// https://github.com/arendjr/text-clipper
a.innerHTML = content;
a.href = src.firstChild.href;
a.onclick = tocClick
// In order to remove this gotta fix the styles.
a.setAttribute('class', 'anchor');
return a
};
var tocClick = function(e) {
var divs = document.querySelectorAll('.page_toc .active');
// Remove the previous classes
[].forEach.call(divs, function(div) {
div.setAttribute('class', 'anchor')
});
// Make sure this is attached to the parent not itself
e.target.parentNode.setAttribute('class')
};
var createList = function(wrapper, count) {
while (count--) {
wrapper = wrapper.appendChild(
document.createElement('ul')
);
if (count) {
wrapper = wrapper.appendChild(
document.createElement('li')
);
}
}
return wrapper;
};
//------------------------------------------------------------------------
var getHeaders = function(selector) {
var headings2 = document.querySelectorAll(selector);
var ret = [];
[].forEach.call(headings2, function(heading) {
ret = ret.concat(heading);
});
return ret;
};
var getLevel = function(header) {
var decs = header.match(/\d/g);
return decs ? Math.min.apply(null, decs) : 1;
};
var jumpBack = function(currentWrapper, offset) {
while (offset--) {
currentWrapper = currentWrapper.parentElement;
}
return currentWrapper;
};
var buildTOC = function(options) {
var ret = document.createElement('ul');
var wrapper = ret;
var lastLi = null;
var selector = options.scope + ' ' + options.headings
var headers = getHeaders(selector)
headers.reduce(function(prev, curr, index) {
var currentLevel = getLevel(curr.tagName);
var offset = currentLevel - prev;
wrapper = (offset > 0)
? createList(lastLi, offset)
: jumpBack(wrapper, -offset * 2)
wrapper = wrapper || ret;
var li = document.createElement('li');
wrapper.appendChild(li).appendChild(aTag(curr));
lastLi = li;
return currentLevel;
}, getLevel(options.headings));
return ret;
};
// Docsify plugin functions
function plugin(hook, vm) {
var userOptions = vm.config.toc;
hook.mounted(function () {
var content = window.Docsify.dom.find(".content");
if (content) {
var nav = window.Docsify.dom.create("aside", "");
window.Docsify.dom.toggleClass(nav, "add", "nav");
window.Docsify.dom.before(content, nav);
}
});
hook.doneEach(function () {
var nav = document.querySelectorAll('.nav')[0]
var t = Array.from(document.querySelectorAll('.nav'))
if (!nav) {
return;
}
const toc = buildTOC(userOptions);
// Just unset it for now.
if (!toc.innerHTML) {
nav.innerHTML = null
return;
}
// Fix me in the future
var title = document.createElement('p');
title.innerHTML = userOptions.title;
title.setAttribute('class', 'title');
var container = document.createElement('div');
container.setAttribute('class', 'page_toc');
container.appendChild(title);
container.appendChild(toc);
// Existing TOC
var tocChild = document.querySelectorAll('.nav .page_toc');
if (tocChild.length > 0) {
tocChild[0].parentNode.removeChild(tocChild[0]);
}
nav.appendChild(container);
});
}
// Docsify plugin options
window.$docsify['toc'] = Object.assign(defaultOptions, window.$docsify['toc']);
window.$docsify.plugins = [].concat(plugin, window.$docsify.plugins);