-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
38 lines (31 loc) · 898 Bytes
/
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
var mjml = require('mjml');
var path = require('path');
var fs = require('fs');
var mailplate = function(options){
var renderer;
if(options.renderer){
renderer = options.renderer
}
this.render = function(templateName, data, callback){
function renderMJML(err, markup){
if(err) return callback(err);
try{
markup = mjml.mjml2html(markup).replace(/\r?\n|\r/g, '');
callback(null, markup);
} catch (e) {
callback(e);
}
}
if(options.templateDir){
templateName = path.resolve(options.templateDir, templateName);
fs.readFile(templateName, 'utf8', function (err, templateString){
if(err) callback(err);
renderer.renderString(templateString, data, renderMJML);
})
} else {
renderer.render(templateName, data, renderMJML)
}
};
return 'mailplate';
};
module.exports = mailplate;