-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
153 lines (144 loc) Β· 6.35 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
const read = require('read-directory');
const defaults = require('defaults');
const compose = require('lodash/flow');
const commonmark = require('commonmark');
const matter = require('gray-matter');
const fs = require('fs');
var reader = new commonmark.Parser();
var writer = new commonmark.HtmlRenderer();
function markdown(str) {
var parsed = reader.parse(str);
return writer.render(parsed);
}
var mdOptions = {
md: markdown,
frontmatter: matter,
filter: '**/*.md',
encoding: 'utf8',
original: false,
};
function parseContent(markdown) {
return function (obj) {
obj.content = markdown(obj.content);
return obj;
};
}
function removeOriginal(obj) {
delete obj.orig;
return obj;
}
/**
* Read the contents of a directory and convert to Markdown asynchronously
* @name parseDir
* @param {String} dir β The directory to read
* @param {Object} opts
* @param {Function} opts.md - alternate function to parse markdown, default: commonmark
* @param {Function} opts.frontmatter - alternate function to parse frontmatter, default: gray-matter
* @param {String} opts.encoding β encoding of files, default: `utf8`
* @param {String} opts.filter β glob pattern for filtering files, default: `**\/*.md`
* @param {String} opts.ignore β glob pattern for ignoring files
* @param {Array} opts.ignore β array of glob patterns for ignoring files
* @param {Boolean} opts.extensions β include or exclude file extensions in keys of returned object, default: `false`
* @param {Boolean} opts.dirnames β include or exclude subdirectory names in keys of returned object, default: `false`
* @param {Boolean} opts.original β include original file contents in returned object, default: `false`
* @param {Function} opts.transform β A function you can use to transform the contents of files after they are converted
* @example
* var md = require('md-directory')
* md.parseDir('./posts', function (err, contents) {
* console.log(contents)
* })
**/
module.exports.parseDir = function parseDir(dir, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = defaults(opts, mdOptions);
var transforms = [opts.frontmatter, parseContent(opts.md)];
if (!opts.original) transforms.push(removeOriginal);
if (opts.transform) transforms.push(opts.transform);
opts.transform = compose(transforms);
read(dir, opts, cb);
};
/**
* Read the contents of a directory and convert to Markdown synchronously
* @name parseDirSync
* @param {String} dir β The directory to read
* @param {Object} opts
* @param {Function} opts.md - alternate function to parse markdown, default: commonmark
* @param {Function} opts.frontmatter - alternate function to parse frontmatter, default: gray-matter
* @param {String} opts.encoding β encoding of files, default: `utf8`
* @param {String} opts.filter β glob pattern for filtering files, default: `**\/*.md`
* @param {String} opts.ignore β glob pattern for ignoring files
* @param {Array} opts.ignore β array of glob patterns for ignoring files
* @param {Boolean} opts.extensions β include or exclude file extensions in keys of returned object, default: `false`
* @param {Boolean} opts.dirnames β include or exclude subdirectory names in keys of returned object, default: `false`
* @param {Boolean} opts.original β include original file contents in returned object, default: `false`
* @param {Function} opts.transform β A function you can use to transform the contents of files after they are converted
* @example
* var md = require('md-directory')
* var contents = md.parseDirSync('./posts')
**/
module.exports.parseDirSync = function parseDirSync(dir, opts) {
opts = defaults(opts, mdOptions);
var transforms = [opts.frontmatter, parseContent(opts.md)];
if (!opts.original) transforms.push(removeOriginal);
if (opts.transform) transforms.push(opts.transform);
opts.transform = compose(transforms);
return read.sync(dir, opts);
};
/**
* Read the contents of a file and convert to Markdown asynchronously
* @name parse
* @param {String} filename β The filename to read
* @param {Object} opts
* @param {Function} opts.md - alternate function to parse markdown, default: commonmark
* @param {Function} opts.frontmatter - alternate function to parse frontmatter, default: gray-matter
* @param {String} opts.encoding β encoding of files, default: `utf8`
* @param {Boolean} opts.original β include original file contents in returned object, default: `false`
* @param {Function} opts.transform β A function you can use to transform the contents of files after they are converted
* @example
* var md = require('md-directory')
* md.parse('./post.md', function (err, contents) {
* console.log(contents)
* })
**/
module.exports.parse = function parse(filename, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = defaults(opts, mdOptions);
var transforms = [opts.frontmatter, parseContent(opts.md)];
if (!opts.original) transforms.push(removeOriginal);
if (opts.transform) transforms.push(opts.transform);
opts.transform = compose(transforms);
fs.readFile(filename, { encoding: opts.encoding }, function (err, data) {
if (err) return cb(err);
var data = opts.transform(data);
cb(null, data);
});
};
/**
* Read the contents of a file and convert to Markdown synchronously
* @name parseSync
* @param {String} filename β The filename to read
* @param {Object} opts
* @param {Function} opts.md - alternate function to parse markdown, default: commonmark
* @param {Function} opts.frontmatter - alternate function to parse frontmatter, default: gray-matter
* @param {String} opts.encoding β encoding of files, default: `utf8`
* @param {Boolean} opts.original β include original file contents in returned object, default: `false`
* @param {Function} opts.transform β A function you can use to transform the contents of files after they are converted
* @example
* var md = require('md-directory')
* var contents = md.parseSync('./post.md')
**/
module.exports.parseSync = function parseSync(filename, opts) {
opts = defaults(opts, mdOptions);
var transforms = [opts.frontmatter, parseContent(opts.md)];
if (!opts.original) transforms.push(removeOriginal);
if (opts.transform) transforms.push(opts.transform);
opts.transform = compose(transforms);
var data = fs.readFileSync(filename, { encoding: opts.encoding });
return opts.transform(data);
};