UNPKG

@ackplus/nest-dynamic-templates

Version:

A powerful and flexible dynamic template rendering library for NestJS applications with support for Nunjucks, Handlebars, EJS, Pug, MJML, Markdown, and more.

41 lines 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MjmlEngine = void 0; const template_types_1 = require("../../interfaces/template.types"); const language_engine_1 = require("../language-engine"); const load_peer_1 = require("../load-peer"); class MjmlEngine extends language_engine_1.LanguageEngine { options; static engineName = template_types_1.TemplateLanguageEnum.MJML; static displayName = 'MJML'; static peerPackage = 'mjml'; constructor(options = {}) { super(); this.options = options; } async render(content) { const mod = await (0, load_peer_1.loadPeer)('mjml', 'mjml', 'language'); const mjml2html = mod.default ?? mod; const result = mjml2html(content, { validationLevel: 'soft', ...this.options }); if (result.errors && result.errors.length > 0) { const message = result.errors .map((e) => e?.formattedMessage || e?.message || String(e)) .join('; '); throw new Error(`MJML: ${message}`); } return result.html; } async validate(content) { try { const mod = await (0, load_peer_1.loadPeer)('mjml', 'mjml', 'language'); const mjml2html = mod.default ?? mod; const result = mjml2html(content, { validationLevel: 'soft', ...this.options }); return !result.errors || result.errors.length === 0; } catch { return false; } } } exports.MjmlEngine = MjmlEngine; //# sourceMappingURL=mjml.engine.js.map