-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
66 lines (49 loc) · 1.42 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
const stat = require('fs').stat;
const spawn = require('child_process').spawn;
module.exports = function (src, options = [], pandocPath) {
if (typeof pandocPath === 'undefined') {
pandocPath = 'pandoc'
}
return new Promise((resolve, reject) => {
let pdSpawn;
let result = "";
let isURL;
// Event Handlers
let onStdOutData;
let onStdOutEnd;
let onStdErrData;
let onStatCheck;
isURL = function (src) {
return /^(https?|ftp):\/\//i.test(src);
};
onStdOutData = function (data) {
result += data;
};
onStdOutEnd = function () {
resolve(result);
};
onStdErrData = function (err) {
reject(new Error(err));
};
onStatCheck = function (err, stats) {
// If src is a file or valid web URL, push the src back into args array
if ((stats && stats.isFile()) || isURL) {
options.unshift(src);
}
// Create child_process.spawn
pdSpawn = spawn(pandocPath, options);
// If src is not a file, assume a string input.
if ((typeof stats === "undefined") && !isURL) {
pdSpawn.stdin.end(src, 'utf-8');
}
// Set handlers...
pdSpawn.stdout.on('data', onStdOutData);
pdSpawn.stdout.on('end', onStdOutEnd);
pdSpawn.on('error', onStdErrData);
};
// Check if src is URL match.
isURL = isURL(src);
// Check file status of src
stat(src, onStatCheck);
})
};