28 lines
830 B
TypeScript
28 lines
830 B
TypeScript
|
import mjml2html from "mjml";
|
||
|
import MailError from "../mail/MailError";
|
||
|
import NunjucksViewEngine from "./NunjucksViewEngine";
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|