-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
135 lines (112 loc) · 3.3 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
/*!
* verb-toc <https://github.com/jonschlinkert/verb-toc>
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var isObject = require('isobject');
var extend = require('extend-shallow');
var toc = require('markdown-toc');
module.exports = function(app) {
app.postLayout(/\.md/, createToc('postLayout', app));
app.postRender(/\.md/, createToc('postRender', app));
app.postRender(/\.md/, createToc('preRender', app));
app.preWrite(/\.md/, injectToc(app));
};
function createToc(method, app) {
return function(file, next) {
var opts = createOpts(app, file);
if (method !== opts.toc.method) {
next(null, file);
return;
}
if (opts.toc.render !== true) {
next(null, file);
return;
}
opts.linkify = opts.linkify || function(tok, name, slug, tocOpts) {
if (/^[{<]%?/.test(tok.content)) {
var view = app.view('temp' + file.extname, {content: tok.content});
app.compile(view, opts);
name = tok.content = view.fn(app.cache.data);
}
toc.linkify(tok, name, slug, tocOpts);
return tok;
};
file.toc = toc(file.content, opts);
if (app.hasListeners('toc')) {
app.emit('toc', file, next);
return;
}
next(null, file);
};
};
function injectToc(app) {
return function(file, next) {
var opts = createOpts(app, file);
var str = file.contents.toString();
if (opts.toc.method === 'preWrite') {
file.toc = toc(file.content, opts);
}
var tocString = (file.toc && file.toc.content) ? file.toc.content : '';
var min = opts.toc.minLevels;
// does the TOC have the minimum expected levels to render?
if (typeof min === 'number') {
opts.toc.render = hasMinimumLevels(tocString, min);
}
if (tocString === '' || opts.toc.render !== true) {
str = str.replace(/^#+ (TOC|table of contents)/gmi, '');
tocString = '';
} else {
tocString += opts.toc.footer || '';
}
// don't render toc comments in backticks
str = str.replace(/(?!`)<!-- toc -->(?!`)/g, tocString);
// fix escaped code comments (used as macros)
str = str.split('<!!--').join('<!--');
str = str.replace(/\n{2,}/g, '\n\n');
file.contents = new Buffer(str);
next(null, file);
};
}
function createOpts(app, file) {
var opts = extend({toc: {}}, app.options, file.options, file.data);
if (typeof opts.toc === 'string') {
if (opts.toc === 'collapsible') {
opts.toc = { method: 'postLayout', collapsible: true };
} else {
opts.toc = { method: opts.toc };
}
}
if (typeof opts.toc === 'boolean') {
opts.toc = { render: opts.toc };
}
if (!isObject(opts.toc)) {
opts.toc = {};
}
if (typeof opts.toc.method === 'string' || opts.toc.match || typeof opts.toc.minLevels === 'number') {
opts.toc.render = true;
} else {
opts.toc.method = 'postLayout';
}
opts = extend({}, opts, opts.toc);
return opts;
}
function hasMinimumLevels(str, min) {
var lines = str.split('\n');
var len = lines.length;
var max = 0;
while (len--) {
var line = lines[len];
var ws = /^\s+/.exec(line);
if (!ws) continue;
var wlen = ws[0].length;
if (wlen > max) {
max = wlen;
}
}
return max >= min;
}
module.exports.createToc = createToc;
module.exports.injectToc = injectToc;