forked from alexseman/markdown-dir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (42 loc) · 963 Bytes
/
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
/**
* Module dependencies
*/
var markdown = require('./lib/markdown');
var readFile = require('fs').readFile;
var matter = require('./lib/matter');
var Batch = require('batch');
var glob = require('glob');
/**
* Export `Markdown`
*/
module.exports = Markdown;
/**
* Initialize `Markdown`
*/
function Markdown(path, options, fn) {
if (arguments.length == 2) {
fn = options;
options = {};
}
glob(path, options, function (err, files) {
if (err) return fn(err);
var batch = new Batch;
var pending = 0;
var out = {};
files.forEach(function(file) {
batch.push(function(done) {
readFile(file, 'utf8', function(err, str) {
if (err) return done(err);
var obj = matter(str);
obj.content = markdown(obj.content);
out[file] = obj;
done();
})
})
});
batch.end(function(err) {
if (err) return fn(err);
fn(null, out);
})
})
}