2021-04-28 14:53:46 +02:00
|
|
|
import mjml2html from "mjml";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
|
|
|
import MailError from "../mail/MailError.js";
|
|
|
|
import NunjucksViewEngine from "./NunjucksViewEngine.js";
|
2021-04-28 14:53:46 +02:00
|
|
|
|
|
|
|
export default class MailViewEngine extends NunjucksViewEngine {
|
|
|
|
public getExtension(): string {
|
|
|
|
return 'mnjk';
|
|
|
|
}
|
|
|
|
|
|
|
|
public async render(
|
|
|
|
file: string,
|
|
|
|
locals: Record<string, unknown>,
|
|
|
|
textOnly: boolean = false,
|
|
|
|
): Promise<string> {
|
|
|
|
locals.text = textOnly;
|
|
|
|
const nunjucksResult = await super.render(file, locals);
|
|
|
|
if (textOnly) return nunjucksResult;
|
|
|
|
|
|
|
|
const mjmlResult = mjml2html(nunjucksResult, {});
|
|
|
|
|
|
|
|
if (mjmlResult.errors.length > 0) {
|
|
|
|
throw new MailError(`Error while parsing mail template ${file}: ${JSON.stringify(mjmlResult.errors, null, 4)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mjmlResult.html;
|
|
|
|
}
|
|
|
|
}
|