forked from jonschlinkert/markdown-toc
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
269 lines (223 loc) · 5.71 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict';
/**
* Module dependencies
*/
var utils = require('./lib/utils');
/**
* expose `toc`
*/
module.exports = toc;
/**
* Load `generate` as a remarkable plugin and
* expose the `toc` function.
*
* @param {String} `str` String of markdown
* @param {Object} `options`
* @return {String} Markdown-formatted table of contents
*/
function toc(str, options) {
return new utils.Remarkable()
.use(generate(options))
.render(str);
}
/**
* Expose `insert` method
*/
toc.insert = require('./lib/insert');
/**
* Generate a markdown table of contents. This is the
* function that does all of the main work with Remarkable.
*
* @param {Object} `options`
* @return {String}
*/
function generate(options) {
var opts = utils.merge({firsth1: true, maxdepth: 6}, options);
var stripFirst = opts.firsth1 === false;
return function(md) {
md.renderer.render = function(tokens) {
tokens = tokens.slice();
var len = tokens.length, i = 0, num = 0;
var tocstart = -1;
var seen = {};
var arr = [];
var res = {};
while (len--) {
var token = tokens[i++];
if (/<!--[ \t]*toc[ \t]*-->/.test(token.content)) {
tocstart = token.lines[1];
}
if (token.type === 'heading_open') {
tokens[i].lvl = tokens[i - 1].hLevel;
tokens[i].i = num++;
arr.push(tokens[i]);
}
}
var result = [];
res.json = [];
// exclude headings that come before the actual
// table of contents.
var alen = arr.length, j = 0;
while (alen--) {
var tok = arr[j++];
if (tok.lines[0] > tocstart) {
if (!seen.hasOwnProperty(tok.content)) {
seen[tok.content] = 0;
} else {
seen[tok.content]++;
}
tok.seen = seen[tok.content];
tok.slug = _getSlug(tok, opts);
res.json.push(utils.pick(tok, ['content', 'slug', 'lvl', 'i', 'seen']));
result.push(linkify(tok, opts));
}
}
opts.highest = highest(result);
res.highest = opts.highest;
res.tokens = tokens;
if (stripFirst) result = result.slice(1);
res.content = bullets(result, opts);
res.content += (opts.append || '');
return res;
};
};
}
/**
* Render markdown list bullets
*
* @param {Array} `arr` Array of listitem objects
* @param {Object} `opts`
* @return {String}
*/
function bullets(arr, options) {
var opts = utils.merge({indent: ' '}, options);
opts.chars = opts.chars || opts.bullets || ['-', '*', '+'];
var unindent = 0;
var listitem = utils.li(opts);
var fn = typeof opts.filter === 'function'
? opts.filter
: null;
// Keep the first h1? This is `true` by default
if (opts && opts.firsth1 === false) {
unindent = 1;
}
var len = arr.length;
var res = [];
var i = 0;
while (i < len) {
var ele = arr[i++];
ele.lvl -= unindent;
if (fn && !fn(ele.content, ele, arr)) {
continue;
}
if (ele.lvl > opts.maxdepth) {
continue;
}
var lvl = ele.lvl - opts.highest;
res.push(listitem(lvl, ele.content, opts));
}
return res.join('\n');
}
/**
* Get the highest heading level in the array, so
* we can un-indent the proper number of levels.
*
* @param {Array} `arr` Array of tokens
* @return {Number} Highest level
*/
function highest(arr) {
var res = arr.slice().sort(function(a, b) {
return a.lvl - b.lvl;
});
if (res && res.length) {
return res[0].lvl;
}
return 0;
}
/**
* Turn headings into anchors
*/
function linkify(tok, opts) {
if (tok && tok.content) {
var text = titleize(tok.content, opts);
var slug = _getSlug(tok, opts);
if (opts && typeof opts.linkify === 'function') {
return opts.linkify(tok, text, slug, opts);
}
tok.content = utils.mdlink(text, '#' + slug);
}
return tok;
}
function _getSlug( tok, opts ) {
var slug = slugify(tok.content, opts);
if (tok.seen > 0) {
slug += '-' + tok.seen;
}
return slug;
}
/**
* Slugify the url part of a markdown link.
*
* @name options.slugify
* @param {String} `str` The string to slugify
* @param {Object} `opts` Pass a custom slugify function on `slugify`
* @return {String}
* @api public
*/
function slugify(str, opts) {
if (opts && opts.slugify === false) return str;
if (opts && typeof opts.slugify === 'function') {
return opts.slugify(str, opts);
}
str = str.split('.').join('').toLowerCase();
str = str.split(' ').join('-');
str = str.split(/\t/).join('----');
return str.replace(/[^a-z0-9]/g, '-');
}
/**
* Titleize the title part of a markdown link.
*
* @name options.titleize
* @param {String} `str` The string to titleize
* @param {Object} `opts` Pass a custom titleize function on `titleize`
* @return {String}
* @api public
*/
function titleize(str, opts) {
if (opts && opts.strip) { return strip(str, opts); }
if (opts && opts.titleize === false) return str;
if (opts && typeof opts.titleize === 'function') {
return opts.titleize(str, opts);
}
return str;
}
/**
* Optionally strip specified words from heading text (not url)
*
* @name options.strip
* @param {String} `str`
* @param {String} `opts`
* @return {String}
*/
function strip(str, opts) {
opts = opts || {};
if (!opts.strip) return str;
if (typeof opts.strip === 'function') {
return opts.strip(str, opts);
}
if (Array.isArray(opts.strip) && opts.strip.length) {
var res = opts.strip.join('|');
var re = new RegExp(res, 'g');
str = str.trim().replace(re, '');
return str.replace(/^-|-$/g, '');
}
return str;
}
/**
* Expose utils
*/
toc.bullets = bullets;
toc.linkify = linkify;
toc.slugify = slugify;
toc.titleize = titleize;
toc.strip = strip;