-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (96 loc) · 2.88 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
'use strict';
var path = require('path');
var File = require('vinyl');
var PluginError = require('plugin-error');
var through = require('through2');
var toc = require('markdown-toc');
module.exports = function(options) {
var opts = Object.assign({}, options);
var files = [];
return through.obj(function(file, enc, next) {
if (file.isNull()) {
next(null, file);
return;
}
if (typeof opts.filterFiles === 'function' && !opts.filterFiles(file)) {
next();
return;
}
try {
var str = file.contents.toString();
file.toc = toc(str, opts);
file.contents = new Buffer(toc.insert(str, opts));
} catch (err) {
var errorOpts = {fileName: file.path, showStack: true};
this.emit('error', new PluginError('gulp-markdown-toc', err, errorOpts));
return;
}
files.push(file);
next(null, file);
}, function(cb) {
if (!opts.index) {
cb();
return;
}
var index = 'toc.md';
if (typeof opts.index === 'string') {
index = opts.index;
}
var len = files.length;
var output = ['# Table of contents', ''];
if (typeof opts.sortFiles === 'function') {
files.sort(opts.sortFiles);
}
if (len > 1) {
for (var i = 0; i < len; i++) {
var file = files[i];
var name = headingName(file, opts);
var link = headingLink(file, opts);
output.push('## [' + name + '](' + link + ')', '');
output.push(prefixPath(file.toc.content, file.relative), '');
}
}
var tocIndex = new File({path: index});
tocIndex.contents = new Buffer(output.join('\n'));
if (typeof opts.index === 'function') {
prepareDest(tocIndex, opts.index, opts);
}
this.push(tocIndex);
cb();
});
};
function headingName(file, options) {
if (typeof options.headingName === 'function') {
return options.headingName(file);
}
return file.relative;
}
function headingLink(file, options) {
if (typeof options.headingLink === 'function') {
return options.headingLink(file);
}
return file.relative;
}
function prefixPath(toc, relative) {
return toc.split('](').join('](' + relative);
}
function prepareDest(file, dest, options) {
var opts = Object.assign({cwd: process.cwd()}, options);
var cwd = path.resolve(opts.cwd);
var destDir = typeof dest === 'function' ? dest(file) : dest;
if (typeof destDir !== 'string') {
throw new TypeError('expected destination directory to be a string');
}
var baseDir = typeof opts.base === 'function'
? opts.base(file)
: path.resolve(cwd, destDir);
if (typeof baseDir !== 'string') {
throw new TypeError('expected base directory to be a string');
}
var cloned = file.clone({contents: false});
var writePath = path.resolve(baseDir, file.relative);
cloned.cwd = cwd;
cloned.base = cwd;
cloned.path = writePath;
file.path = path.join(file.base, cloned.relative);
}