-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvdir.js
executable file
·60 lines (47 loc) · 1.23 KB
/
convdir.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
#!/usr/bin/env node
var markdownpdf = require("markdown-pdf");
var ProgressBar = require('ascii-progress');
var fs = require('fs');
var currentDir = process.cwd();
var mdFiles = [];
fs.readdir(currentDir, (err, files) => {
if(err){console.log(err);return;}
files.forEach((fileName) => {
if(fileName.split(".")[1] === 'md') {
mdFiles.push(fileName);
console.log("\x1b[33m", "Converting", fileName);
}
})
if(mdFiles.length === 0){console.log("\x1b[35m", "No markdown files in this directory");return;}
var pdfFiles = mdFiles.map( (d) => {
return "pdfs/" + d.replace(".md", ".pdf")
});
forward();
markdownpdf().from(mdFiles).to(pdfFiles, () => {
pdfFiles.forEach((file) => {
console.log("\x1b[32m", "Created", file);
})
bar.tick(100);
process.exit();
});
});
/**** back and forth progress bar ****/
var bar = new ProgressBar({
schema:' :title [:bar.cyan] :percent.yellow'
});
function forward() {
bar.tick(1, { title: 'Converting.yellow ' });
if (bar.current > 65) {
backward();
} else {
setTimeout(forward, 8);
}
}
function backward() {
bar.tick(-1, { title: 'md --> pdf' });
if (bar.current === 1) {
forward();
} else {
setTimeout(backward, 4);
}
}