-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
88 lines (72 loc) · 2.18 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
module.exports = function headerSections(md) {
function addSections(state) {
var tokens = []; // output
var Token = state.Token;
var sections = [];
var nestedLevel = 0;
function openSection(attrs) {
var t = new Token('section_open', 'section', 1);
t.block = true;
t.attrs = attrs && attrs.map(function (attr) { return [attr[0], attr[1]] }); // copy
return t;
}
function closeSection() {
var t = new Token('section_close', 'section', -1);
t.block = true;
return t;
}
function closeSections(section) {
while (last(sections) && section.header <= last(sections).header) {
sections.pop();
tokens.push(closeSection());
}
}
function closeSectionsToCurrentNesting(nesting) {
while (last(sections) && nesting < last(sections).nesting) {
sections.pop();
tokens.push(closeSection());
}
}
function closeAllSections() {
while (sections.pop()) {
tokens.push(closeSection());
}
}
for (var i = 0, l = state.tokens.length; i < l; i++) {
var token = state.tokens[i];
// record level of nesting
if (token.type.search('heading') !== 0) {
nestedLevel += token.nesting;
}
if (last(sections) && nestedLevel < last(sections).nesting) {
closeSectionsToCurrentNesting(nestedLevel);
}
// add sections before headers
if (token.type == 'heading_open') {
var section = {
header: headingLevel(token.tag),
nesting: nestedLevel
};
if (last(sections) && section.header <= last(sections).header) {
closeSections(section);
}
tokens.push(openSection(token.attrs));
if (token.attrIndex('id') !== -1) {
// remove ID from token
token.attrs.splice(token.attrIndex('id'), 1);
}
sections.push(section);
}
tokens.push(token);
} // end for every token
closeAllSections();
state.tokens = tokens;
}
md.core.ruler.push('header_sections', addSections);
};
function headingLevel(header) {
return parseInt(header.charAt(1));
}
function last(arr) {
return arr.slice(-1)[0];
}