-
Notifications
You must be signed in to change notification settings - Fork 0
/
.mjmlconfig.js
56 lines (44 loc) · 1.21 KB
/
.mjmlconfig.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
const fs = require('fs');
const path = require('path');
const Handlebars = require('handlebars');
const data = require("./data.json");
const getFilesRecursively = (directory) => {
let files = [];
const filesInDirectory = fs.readdirSync(directory);
for (const file of filesInDirectory) {
const absolute = path.join(directory, file);
if (fs.statSync(absolute).isDirectory()) {
getFilesRecursively(absolute);
} else {
files.push(absolute);
}
}
return files;
};
const registerPartials = () => {
const partialPaths = getFilesRecursively('partials');
for (const partialPath of partialPaths) {
const partialName = path.basename(partialPath, '.mjml');
const partialContent = fs.readFileSync(partialPath, 'utf8');
Handlebars.registerPartial(partialName, partialContent);
}
};
const compileTemplate = (content) => {
const handlebarsTemplate = Handlebars.compile(content);
return handlebarsTemplate(data);
};
const options = {
preprocessors: [
(rawMJML) => {
try {
registerPartials();
return compileTemplate(rawMJML);
} catch (e) {
console.error(e);
return rawMJML;
}
},
],
packages: [],
};
module.exports = options;