forked from 1000ch/node-github-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (53 loc) · 1.32 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
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var minimist = require('minimist');
var glob = require('glob');
var async = require('async');
var GFM = require('./gfm');
var util = require('./lib/util');
var argv = minimist(process.argv.slice(2));
if (argv._.length === 0) {
throw new Error('There is no arguments.');
}
var targetFiles = [];
argv._.filter(function (arg) {
return fs.existsSync(arg);
}).forEach(function (arg) {
if (util.isFile(arg)) {
targetFiles.push(arg);
} else if (util.isDirectory(arg)) {
fs.readdirSync(arg).forEach(function (file) {
targetFiles.push(file);
});
} else {
glob(arg, function (error, files) {
files.forEach(function (file) {
targetFiles.push(file);
});
});
}
});
if (targetFiles.length === 0) {
throw new Error('There is no markdown files.');
}
async.each(targetFiles, function (file, index, files) {
var name = path.basename(file, '.md') + '.html';
var dest;
if (argv.dest) {
dest = path.join(path.dirname(argv.dest), name);
} else {
dest = path.join(process.cwd(), name);
}
new GFM(file).render(function (html) {
fs.writeFileSync(dest, html, {
encoding: 'utf8',
flag: 'w'
});
});
}, function (error, result) {
if (error) {
throw error;
}
console.log(result);
});