-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
55 lines (46 loc) · 1.51 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
const through = require("through2");
const mjmlDefaultEngine = require("mjml");
const replaceExt = require("replace-ext");
const PluginError = require("plugin-error");
const NAME = "MJML";
function error(file) {
return function(message) {
return new PluginError(NAME, "Error in file " + file + ": " + message);
};
}
module.exports = function mjml(mjmlEngine, options) {
if (!mjmlEngine) {
mjmlEngine = mjmlDefaultEngine;
}
if (options === undefined) {
options = {};
}
return through.obj(function(file, enc, callback) {
// Not a big fan of this deep copy methods
// But it will work regardless of Node version
const localOptions = JSON.parse(JSON.stringify(options));
if (localOptions.filePath === undefined) {
localOptions.filePath = file.path.toString();
}
const raise = error(localOptions.filePath);
if (file.isStream()) {
this.emit("error", raise("Streams are not supported!"));
return callback();
}
if (file.isBuffer()) {
const output = file.clone();
let render;
try {
render = mjmlEngine(file.contents.toString(), localOptions);
} catch (e) {
this.emit("error", raise(e.message));
return callback();
}
// [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues
output.contents = Buffer.from(render.html);
output.path = replaceExt(file.path.toString(), localOptions.fileExt || ".html");
this.push(output);
}
return callback();
});
};