Some code formatting

This commit is contained in:
witzig 2017-03-20 21:40:21 +01:00
parent f2b7d4c8df
commit 0b0929aa7b

View file

@ -357,44 +357,52 @@ function filterKeysAndValues(keysIn, valuesIn, method = 'include', prefixes = []
}
function testForMjmlErrors(keys, values) {
let errors = [];
let testLayout = '<mjml><mj-body><mj-container>{{{body}}}</mj-container></mj-body></mjml>';
let isInvalidMjml = (template, layout = testLayout) => {
let hasMjmlError = (template, layout = testLayout) => {
let source = layout.replace(/\{\{\{body\}\}\}/g, template);
let compiled;
try {
compiled = mjml.mjml2html(source);
} catch (err) {
return err;
}
if (compiled.errors.length) {
return compiled.errors[0].message || compiled.errors[0];
}
return null;
};
let errors = [];
keys.forEach((key, index) => {
if (key.startsWith('mail_') || key.startsWith('web_')) {
let template = values[index];
let err = isInvalidMjml(template);
let err = hasMjmlError(template);
err && errors.push(key + ': ' + (err.message || err));
if (key === 'mail_confirm_html' && !template.includes('{{confirmUrl}}')) {
errors.push(key + ': Missing {{confirmUrl}}');
}
key === 'mail_confirm_html' && !template.includes('{{confirmUrl}}') && errors.push(key + ': Missing {{confirmUrl}}');
} else if (key === 'layout') {
let layout = values[index];
let err = isInvalidMjml('', layout);
let err = hasMjmlError('', layout);
err && errors.push('layout: ' + (err.message || err));
!layout.includes('{{{body}}}') && errors.push('layout: {{{body}}} not found');
}
});
if (errors.length) {
errors.forEach((err, index) => {
errors[index] = (index + 1) + ') ' + err;
});
return 'Please Fix These Errors:\n\n' + errors.join('\n');
return 'Please fix these MJML errors:\n\n' + errors.join('\n');
}
return null;