Skip to content

Commit

Permalink
feat: add mjml component to process mjml markup
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 9, 2024
1 parent b795c6b commit 3ad4e4b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"./types": "./build/src/types.js",
"./commands": "./build/commands/main.js",
"./services/main": "./build/services/main.js",
"./plugins/edge": "./build/src/plugins/edge.js",
"./mail_provider": "./build/providers/mail_provider.js",
"./transports/ses": "./build/src/transports/ses.js",
"./transports/json": "./build/src/transports/json.js",
Expand Down Expand Up @@ -62,6 +63,7 @@
"@swc/core": "^1.3.102",
"@types/async-retry": "^1.4.8",
"@types/luxon": "^3.4.0",
"@types/mjml": "^4.7.4",
"@types/node": "^20.10.7",
"@types/sinon": "^17.0.2",
"async-retry": "^1.3.3",
Expand All @@ -75,6 +77,7 @@
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"luxon": "^3.4.3",
"mjml": "^4.14.1",
"np": "^9.2.0",
"prettier": "^3.1.1",
"sinon": "^17.0.1",
Expand Down Expand Up @@ -151,6 +154,7 @@
"entry": [
"index.ts",
"src/types.ts",
"src/plugins/edge.ts",
"services/main.ts",
"commands/make_mail.ts",
"providers/mail_provider.ts",
Expand Down
2 changes: 2 additions & 0 deletions providers/mail_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export default class MailProvider {
return edge.default.share(helpers).render(templatePath, data)
},
}
const { mailPluginEdge } = await import('../src/plugins/edge.js')
edge.default.use(mailPluginEdge)
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/plugins/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* @adonisjs/mail
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import type { PluginFn } from 'edge.js/types'
import debug from '../debug.js'

/**
* Registers mjml component to convert email markup
* to HTML
*/
export const mailPluginEdge: PluginFn<undefined> = (edge) => {
debug('detected edge. registering mjml component')

edge.global('mail', {
async processMjml(markup: string, options: any) {
const mjml = await import('mjml')
return mjml.default(markup, options).html
},
})

edge.registerTemplate('mjml', {
template: `{{{ await mail.processMjml(await $slots.main(), $props.all()) }}}`,
})
}
36 changes: 36 additions & 0 deletions tests/integration/plugins/edge.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* @adonisjs/mail
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import mjml2html from 'mjml'
import { Edge } from 'edge.js'
import { test } from '@japa/runner'
import { mailPluginEdge } from '../../../src/plugins/edge.js'

test.group('Edge plugin', () => {
test('render mjml markup to HTML', async ({ assert, fs }) => {
const edge = new Edge()
edge.mount(fs.baseUrl)
edge.use(mailPluginEdge)

const markup = `<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text>
Hello World!
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>`

const html = await edge.renderRaw(`@mjml() \n ${markup} \n @end`)
assert.equal(html, mjml2html(markup).html)
})
})

0 comments on commit 3ad4e4b

Please sign in to comment.