swaf/src/frontend/MailViewEngine.ts

29 lines
837 B
TypeScript

import mjml2html from "mjml";
import MailError from "../mail/MailError.js";
import NunjucksViewEngine from "./NunjucksViewEngine.js";
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;
}
}