diff --git a/lib/adapters/liquid.adapter.ts b/lib/adapters/liquid.adapter.ts new file mode 100644 index 00000000..5f4d857a --- /dev/null +++ b/lib/adapters/liquid.adapter.ts @@ -0,0 +1,39 @@ +/** Dependencies **/ +import * as path from 'path'; +import { get } from 'lodash'; + +/** Interfaces **/ +import { MailerOptions } from '../interfaces/mailer-options.interface'; +import { TemplateAdapter } from '../interfaces/template-adapter.interface'; +import { Liquid } from 'liquidjs'; + +export class LiquidAdapter implements TemplateAdapter { + private config: Partial; + + constructor(config?: Partial) { + Object.assign(this.config, config); + } + + public compile(mail: any, callback: any, mailerOptions: MailerOptions): void { + const { context, template } = mail.data; + + const templateExt = path.extname(template) || '.liquid'; + const templateName = path.basename(template, path.extname(template)); + const templateDir = path.isAbsolute(template) + ? path.dirname(template) + : path.join(get(mailerOptions, 'template.dir', ''), path.dirname(template)); + + const engine = new Liquid({ + extname: templateExt, + root: templateDir, + ...this.config.globals + }); + + engine.renderFile(templateName, context).then((body) => { + mail.data.html = body; + return callback(); + }).catch((err) => { + return callback(err); + }); + } +} diff --git a/package.json b/package.json index bb394835..6558efc4 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "handlebars": ">=4.7.6", "mjml": ">=4.15.3", "nodemailer": ">=6.4.6", - "pug": ">=3.0.1" + "pug": ">=3.0.1", + "liquidjs": "^10.8.2" } }