This repository was archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.js
404 lines (347 loc) · 10.5 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
var through = require('through3')
, ast = require('mkast')
, Node = ast.Node
, collect = ast.NodeWalker.collect
, MARKER = '@toc'
, ORDERED = 'ordered'
, BULLET = 'bullet'
, BULLET_HYPHEN = '-'
, BULLET_PLUS = '+'
, BULLET_STAR = '*'
, HASH = '#'
, DELIMITER_PAREN = ')'
, DELIMITER_DOT = '.';
/**
* Create a table of contents index stream.
*
* Note that in order to build a complete index all data must be read so this
* implementation buffers incoming nodes and flushes them when the stream
* is ended writing the index nodes where necessary.
*
* When the first child of a heading is a link it is preserved and no
* automatic link is created, otherwise when creating links inline markup
* in the heading is discarded.
*
* If the `standalone` option is given then the incoming data is discarded
* and the document representing the index is flushed.
*
* When a `destination` function is specified it is passed a string
* literal of the heading text and a reference to the `slug` function and
* should return a URL, the function is invoked in the scope of this stream.
*
* Typically `prefix` will be either a `/`, `#` or the empty string
* depending upon whether you want absolute, anchor or relative links. The
* default is to use `#` for anchor links on the same page.
*
* If the `bullet` option is given it must be one of `-`, `+` or `*`.
*
* If the `delimiter` option is given it must a period `.` or right
* parenthesis `)`.
*
* @constructor Toc
* @param {Object} [opts] processing options.
*
* @option {Boolean} [standalone] discard incoming data.
* @option {String=bullet} [type] list output type, `bullet` or `ordered`.
* @option {Boolean=true} [link] whether to create links in the output lists.
* @option {Number=1} [depth] ignore headings below this level.
* @option {Number=6} [max] ignore headings above this level.
* @option {Function} [destination] builds the link URLs.
* @option {Function} [slug] function to generate the slug id.
* @option {String=#} [prefix] default link prefix.
* @option {String} [base] a base path for absolute links.
* @option {String=-} [bullet] character for bullet lists.
* @option {String=)} [delimiter] delimiter for ordered lists.
*/
function Toc(opts) {
// when standalone all other data apart from the generated TOC
// document is removed from the stream
this.standalone = opts.standalone !== undefined
? opts.standalone : false;
this.type = opts.type === ORDERED || opts.type === BULLET
? opts.type : BULLET;
// do we create links, calls destination()
this.link = opts.link !== undefined ? opts.link : true;
// initial depth, headings below this depth are ignored
this.depth = typeof opts.depth === 'number' && opts.depth > 0
? opts.depth : 1;
// maximum depth, headings above this depth are ignored
this.max = typeof opts.max === 'number' && opts.max > 0
? opts.max : 6;
// set up default destination function
var dest = typeof opts.destination === 'function'
? opts.destination : destination;
this.destination = dest.bind(this);
this.slug = opts.slug || slug;
// string base and prefix for URLs
this.prefix = typeof opts.prefix === 'string' ? opts.prefix : HASH;
this.base = typeof opts.base === 'string' ? opts.base : '';
// list character options
this.bulletChar =
opts.bullet === BULLET_HYPHEN
|| opts.bullet === BULLET_PLUS
|| opts.bullet === BULLET_STAR ? opts.bullet : BULLET_HYPHEN;
this.delimiter =
opts.delimiter === DELIMITER_DOT
|| opts.delimiter === DELIMITER_PAREN ? opts.delimiter : DELIMITER_PAREN;
// document to hold the TOC list
this.doc = Node.createDocument();
// input chunks we received
this.input = [];
// nodes to push() between document and EOF
this.nodes = [];
// if user specifed a title then append it to the beginning
if(opts.title) {
var title = Node.createNode(Node.HEADING, {level: opts.level || 1});
title.appendChild(Node.createNode(Node.TEXT, {literal: opts.title}));
this.nodes.push(title);
}
// current list or item
this.current = null;
// current level
this.level = 0;
if(this.type === ORDERED) {
this.counters = [];
}
this.currentDepth = 0;
}
/**
* Stream transform.
*
* @private
*/
function transform(chunk, encoding, cb) {
// list item
var item
// container element: paragraph or link
, container
// collection of child text nodes
, text
// current target item or list
, target
// literal string of all text nodes
, literal = '';
if(!this.standalone) {
this.input.push(chunk);
}
if(Node.is(chunk, Node.HEADING)) {
// ignore these levels
if(chunk.level < this.depth || chunk.level > this.max) {
return cb();
}
if(!this.current) {
this.list = this.getList(chunk);
this.nodes.push(this.list);
this.current = this.list;
}
if(this.counters) {
this.counters[chunk.level] = this.counters[chunk.level] || 0;
this.counters[chunk.level]++;
}
if(this.counters && chunk.level > this.level) {
this.counters[chunk.level] = 1;
}
item = Node.createNode(
Node.ITEM,
this.getListData(chunk.level, chunk.level === this.depth ? 0 : 2));
// preserve headings that are already links
if(Node.is(chunk.firstChild, Node.LINK)) {
item.appendChild(Node.deserialize(chunk.firstChild));
}else{
text = collect(chunk, Node.TEXT);
if(this.link) {
container = Node.createNode(Node.LINK);
}else{
container = Node.createNode(Node.PARAGRAPH);
// preserve inline markup when not automatic linking
var next = chunk.firstChild;
while(next) {
container.appendChild(Node.deserialize(next));
next = next.next;
}
}
if(this.link) {
text.forEach(function(txt) {
literal += txt.literal;
container.appendChild(Node.deserialize(txt));
})
container.destination = this.destination(literal, this.slug);
}
item.appendChild(container);
}
// top level headings go into primary list
if(chunk.level === this.depth) {
// coming back from deeper - create a new list
if(this.level > this.depth) {
this.currentDepth++;
this.list = this.getList(chunk);
this.nodes.push(this.list);
}
this.list.appendChild(item);
this.current = item;
// other headings look for parents
}else{
target = this.current;
// same level - append item to current target list
if(chunk.level === this.level) {
this.current.appendChild(item);
// descending into a nested level - create a new list
}else if(chunk.level > this.level) {
this.currentDepth++;
target = this.getList(chunk);
this.current.appendChild(target);
target.appendChild(item);
this.current = target;
// ascending back up levels - find or create list
}else{
this.currentDepth--;
var diff = this.level - chunk.level;
target = this.current.parent;
while(--diff && target) {
target = target.parent;
}
if(!target) {
var lvl = chunk.level - 1;
if(this.lists[lvl] && this.lists[lvl].length) {
target = this.lists[lvl][this.lists[lvl].length - 1];
}
}
target.appendChild(item);
this.current = target;
}
}
// store current level
this.level = chunk.level;
}
cb();
}
/**
* Print the index document to the stream.
*
* @private
*/
function print() {
// nothing to do
if(!this.nodes.length) {
return;
}
// document
this.push(this.doc);
// list nodes
for(var i = 0;i < this.nodes.length;i++) {
this.push(this.nodes[i]);
}
// eof
this.push(Node.createNode(Node.EOF));
}
/**
* Flush the data when the stream ends.
*
* @private
*/
function flush(cb) {
var i
, chunk
, node
, j = this.nodes.length;
while(j--) {
chunk = this.nodes[j];
if(Node.is(chunk, Node.LIST)) {
chunk._lastLineBlank = true;
break;
}
}
if(this.standalone) {
this.print();
}else{
// pass through input chunks
for(i = 0;i < this.input.length;i++) {
chunk = this.input[i];
if(Node.is(chunk, Node.HTML_BLOCK)
&& (chunk._htmlBlockType === 2 || chunk.htmlBlockType === 2)
&& chunk.literal
&& ~chunk.literal.indexOf(MARKER)) {
// consume the TOC nodes so nothing is printed at the end
while((node = this.nodes.shift())) {
this.push(node);
}
// skip the injection marker
continue;
}
this.push(chunk);
}
// apppend TOC document
this.print();
}
cb();
}
function getList(header, padding, markerOffset) {
this.lists = this.lists || [];
var data = this.getListData(header.level, padding, markerOffset)
, list = Node.createNode(Node.LIST, data);
this.lists[this.currentDepth] = this.lists[this.currentDepth] || [];
this.lists[this.currentDepth].push(list);
return list;
}
/**
* Get a list data object.
*
* @private
*/
function getListData(level, padding, markerOffset) {
var data = {
_listData: {
type: this.type,
tight: true,
// NOTE: extend the listData with this field
level: level,
padding: padding !== undefined ? padding : 2,
markerOffset: markerOffset || 0
}
}
if(this.counters) {
data._listData.start = this.counters[level];
data._listData.delimiter = this.delimiter;
if(this.counters[level] !== undefined) {
// the +1 accounts for the single space
data._listData.padding =
(this.counters[level] + this.delimiter).length + 1;
}
}else{
data._listData.bulletChar = this.bulletChar;
}
return data;
}
/**
* Github style automatic header identifiers.
*
* @private
*/
function slug(text) {
text = text.toLowerCase();
text = text.replace(/[^A-Z0-9a-z _-]/g, '');
text = text.replace(/ /g, '-');
//text = text.replace(/-{2,}/g, '-');
return text;
}
/**
* Default destination function.
*
* @private
*/
function destination(literal, slug) {
if(!this.seen) {
this.seen = {};
}
var str = slug(literal);
if(this.seen[str] !== undefined) {
this.seen[str]++;
str += '-' + this.seen[str];
}
this.seen[str] = 0;
return this.base + this.prefix + str;
}
Toc.prototype.print = print;
Toc.prototype.getList = getList;
Toc.prototype.getListData = getListData;
module.exports = through.transform(transform, flush, {ctor: Toc});