forked from kelseythejackson/inky
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (47 loc) · 1.43 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
var cheerio = require("cheerio");
var path = require("path");
var through = require("through2");
var vfs = require("vinyl-fs");
var Inky = require("./lib/inky");
var fs = require("fs");
var mkdirp = require("mkdirp");
var inky;
module.exports = function (opts, cb) {
var stream;
opts = opts || {};
opts.cheerio = Inky.mergeCheerioOpts(opts.cheerio);
if (typeof inky === "undefined") {
inky = new Inky(opts);
}
// If the user passed in source files, create a stream
if (opts.src) {
stream = vfs.src(opts.src).pipe(transform());
if (opts.dest && typeof cb === "function") {
stream.on("finish", cb);
}
}
// Otherwise, return the transform function
else {
return transform();
}
// This transform function takes in a Vinyl HTML file, converts the code from Inky to HTML, and returns the modified file.
function transform() {
return through.obj(function (file, enc, callback) {
var convertedHtml = inky.releaseTheKraken(
file.contents.toString(),
opts.cheerio
);
file.contents = new Buffer.from(convertedHtml);
// Write to disk manually if the user specified it
if (opts.dest) {
var outputPath = path.join(opts.dest, path.basename(file.path));
mkdirp(opts.dest, function () {
fs.writeFile(outputPath, convertedHtml, callback);
});
} else {
callback(null, file);
}
});
}
};
module.exports.Inky = Inky;