-
Notifications
You must be signed in to change notification settings - Fork 2
/
MarkdownFile.js
51 lines (47 loc) · 1.53 KB
/
MarkdownFile.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
/**
* Created by mostekcm on 9/7/16.
* The purpose of this module is to provide a promise capability to process the results.
*/
var Promise = require('bluebird');
var fs = require('fs');
var readline = require('readline');
/**
* Constructor
* @param fileName The name of the file to process that contains markdown.
*/
function MarkdownFile(fileName) {
this.fileName = fileName;
this.lineReader = readline.createInterface({
input: fs.createReadStream(this.fileName)
});
}
/**
* Process the markdown file
* @param processor The processor that will process the markdown. Must have a method for processLine(line) and finish(), both should return a promise object. Finish.then will return an array of lines in dokuwiki format.
* @return a promise object
*/
MarkdownFile.prototype.process = function (processor) {
var mf = this;
return new Promise(function (resolve, reject) {
mf.lineReader.on('line', function (line) {
processor.processLine(line)
.then(function () {
// Just pass on through
})
.catch(function (err) {
reject(err);
});
});
mf.lineReader.on('close', function () {
processor.finish()
.then(function (dokuwikiLines) {
resolve(dokuwikiLines);
})
.catch(function (err) {
reject(err);
});
});
});
}
//Export the class
module.exports = MarkdownFile;