mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Replace Handlebars with flexible template engine system
- Replace handlebars dependency with optional liquidjs and mustache
- Add templateEngine string configuration ('liquidjs', 'mustache', 'simple')
- Add custom templateRenderer hook for maximum flexibility
- Implement graceful fallbacks when optional dependencies unavailable
- Fix webpack compatibility issues with require.extensions
- Maintain backward compatibility with existing templates
- Add comprehensive template syntax documentation
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-mailing",
|
"name": "@xtr-dev/payload-mailing",
|
||||||
"version": "0.0.8",
|
"version": "0.0.9",
|
||||||
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@@ -47,9 +47,12 @@
|
|||||||
"payload": "^3.37.0"
|
"payload": "^3.37.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"handlebars": "^4.7.8",
|
|
||||||
"nodemailer": "^6.9.8"
|
"nodemailer": "^6.9.8"
|
||||||
},
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"liquidjs": "^10.19.0",
|
||||||
|
"mustache": "^4.2.0"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
"@payloadcms/db-mongodb": "^3.37.0",
|
"@payloadcms/db-mongodb": "^3.37.0",
|
||||||
|
|||||||
53
pnpm-lock.yaml
generated
53
pnpm-lock.yaml
generated
@@ -8,9 +8,9 @@ importers:
|
|||||||
|
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
handlebars:
|
liquidjs:
|
||||||
specifier: ^4.7.8
|
specifier: ^10.19.0
|
||||||
version: 4.7.8
|
version: 10.21.1
|
||||||
nodemailer:
|
nodemailer:
|
||||||
specifier: ^6.9.8
|
specifier: ^6.9.8
|
||||||
version: 6.10.1
|
version: 6.10.1
|
||||||
@@ -2637,6 +2637,10 @@ packages:
|
|||||||
colorette@2.0.20:
|
colorette@2.0.20:
|
||||||
resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==}
|
resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==}
|
||||||
|
|
||||||
|
commander@10.0.1:
|
||||||
|
resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==}
|
||||||
|
engines: {node: '>=14'}
|
||||||
|
|
||||||
commander@2.20.3:
|
commander@2.20.3:
|
||||||
resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==}
|
resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==}
|
||||||
|
|
||||||
@@ -3540,11 +3544,6 @@ packages:
|
|||||||
resolution: {integrity: sha512-mS1lbMsxgQj6hge1XZ6p7GPhbrtFwUFYi3wRzXAC/FmYnyXMTvvI3td3rjmQ2u8ewXueaSvRPWaEcgVVOT9Jnw==}
|
resolution: {integrity: sha512-mS1lbMsxgQj6hge1XZ6p7GPhbrtFwUFYi3wRzXAC/FmYnyXMTvvI3td3rjmQ2u8ewXueaSvRPWaEcgVVOT9Jnw==}
|
||||||
engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0}
|
engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0}
|
||||||
|
|
||||||
handlebars@4.7.8:
|
|
||||||
resolution: {integrity: sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==}
|
|
||||||
engines: {node: '>=0.4.7'}
|
|
||||||
hasBin: true
|
|
||||||
|
|
||||||
has-bigints@1.1.0:
|
has-bigints@1.1.0:
|
||||||
resolution: {integrity: sha512-R3pbpkcIqv2Pm3dUwgjclDRVmWpTJW2DcMzcIhEXEx1oh/CEMObMm3KLmRJOdvhM7o4uQBnwr8pzRK2sJWIqfg==}
|
resolution: {integrity: sha512-R3pbpkcIqv2Pm3dUwgjclDRVmWpTJW2DcMzcIhEXEx1oh/CEMObMm3KLmRJOdvhM7o4uQBnwr8pzRK2sJWIqfg==}
|
||||||
engines: {node: '>= 0.4'}
|
engines: {node: '>= 0.4'}
|
||||||
@@ -3922,6 +3921,11 @@ packages:
|
|||||||
lines-and-columns@1.2.4:
|
lines-and-columns@1.2.4:
|
||||||
resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==}
|
resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==}
|
||||||
|
|
||||||
|
liquidjs@10.21.1:
|
||||||
|
resolution: {integrity: sha512-NZXmCwv3RG5nire3fmIn9HsOyJX3vo+ptp0yaXUHAMzSNBhx74Hm+dAGJvscUA6lNqbLuYfXgNavRQ9UbUJhQQ==}
|
||||||
|
engines: {node: '>=14'}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
locate-path@5.0.0:
|
locate-path@5.0.0:
|
||||||
resolution: {integrity: sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==}
|
resolution: {integrity: sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
@@ -4202,9 +4206,6 @@ packages:
|
|||||||
natural-compare@1.4.0:
|
natural-compare@1.4.0:
|
||||||
resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==}
|
resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==}
|
||||||
|
|
||||||
neo-async@2.6.2:
|
|
||||||
resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==}
|
|
||||||
|
|
||||||
new-find-package-json@2.0.0:
|
new-find-package-json@2.0.0:
|
||||||
resolution: {integrity: sha512-lDcBsjBSMlj3LXH2v/FW3txlh2pYTjmbOXPYJD93HI5EwuLzI11tdHSIpUMmfq/IOsldj4Ps8M8flhm+pCK4Ew==}
|
resolution: {integrity: sha512-lDcBsjBSMlj3LXH2v/FW3txlh2pYTjmbOXPYJD93HI5EwuLzI11tdHSIpUMmfq/IOsldj4Ps8M8flhm+pCK4Ew==}
|
||||||
engines: {node: '>=12.22.0'}
|
engines: {node: '>=12.22.0'}
|
||||||
@@ -5170,11 +5171,6 @@ packages:
|
|||||||
engines: {node: '>=14.17'}
|
engines: {node: '>=14.17'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
uglify-js@3.19.3:
|
|
||||||
resolution: {integrity: sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ==}
|
|
||||||
engines: {node: '>=0.8.0'}
|
|
||||||
hasBin: true
|
|
||||||
|
|
||||||
uint8array-extras@1.5.0:
|
uint8array-extras@1.5.0:
|
||||||
resolution: {integrity: sha512-rvKSBiC5zqCCiDZ9kAOszZcDvdAHwwIKJG33Ykj43OKcWsnmcBRL09YTU4nOeHZ8Y2a7l1MgTd08SBe9A8Qj6A==}
|
resolution: {integrity: sha512-rvKSBiC5zqCCiDZ9kAOszZcDvdAHwwIKJG33Ykj43OKcWsnmcBRL09YTU4nOeHZ8Y2a7l1MgTd08SBe9A8Qj6A==}
|
||||||
engines: {node: '>=18'}
|
engines: {node: '>=18'}
|
||||||
@@ -5377,9 +5373,6 @@ packages:
|
|||||||
resolution: {integrity: sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==}
|
resolution: {integrity: sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==}
|
||||||
engines: {node: '>=0.10.0'}
|
engines: {node: '>=0.10.0'}
|
||||||
|
|
||||||
wordwrap@1.0.0:
|
|
||||||
resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==}
|
|
||||||
|
|
||||||
wrap-ansi@7.0.0:
|
wrap-ansi@7.0.0:
|
||||||
resolution: {integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==}
|
resolution: {integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
@@ -8540,6 +8533,8 @@ snapshots:
|
|||||||
|
|
||||||
colorette@2.0.20: {}
|
colorette@2.0.20: {}
|
||||||
|
|
||||||
|
commander@10.0.1: {}
|
||||||
|
|
||||||
commander@2.20.3: {}
|
commander@2.20.3: {}
|
||||||
|
|
||||||
commander@6.2.1: {}
|
commander@6.2.1: {}
|
||||||
@@ -9689,15 +9684,6 @@ snapshots:
|
|||||||
|
|
||||||
graphql@16.11.0: {}
|
graphql@16.11.0: {}
|
||||||
|
|
||||||
handlebars@4.7.8:
|
|
||||||
dependencies:
|
|
||||||
minimist: 1.2.8
|
|
||||||
neo-async: 2.6.2
|
|
||||||
source-map: 0.6.1
|
|
||||||
wordwrap: 1.0.0
|
|
||||||
optionalDependencies:
|
|
||||||
uglify-js: 3.19.3
|
|
||||||
|
|
||||||
has-bigints@1.1.0: {}
|
has-bigints@1.1.0: {}
|
||||||
|
|
||||||
has-flag@4.0.0: {}
|
has-flag@4.0.0: {}
|
||||||
@@ -10044,6 +10030,10 @@ snapshots:
|
|||||||
|
|
||||||
lines-and-columns@1.2.4: {}
|
lines-and-columns@1.2.4: {}
|
||||||
|
|
||||||
|
liquidjs@10.21.1:
|
||||||
|
dependencies:
|
||||||
|
commander: 10.0.1
|
||||||
|
|
||||||
locate-path@5.0.0:
|
locate-path@5.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
p-locate: 4.1.0
|
p-locate: 4.1.0
|
||||||
@@ -10439,8 +10429,6 @@ snapshots:
|
|||||||
|
|
||||||
natural-compare@1.4.0: {}
|
natural-compare@1.4.0: {}
|
||||||
|
|
||||||
neo-async@2.6.2: {}
|
|
||||||
|
|
||||||
new-find-package-json@2.0.0:
|
new-find-package-json@2.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
debug: 4.4.1
|
debug: 4.4.1
|
||||||
@@ -11571,9 +11559,6 @@ snapshots:
|
|||||||
|
|
||||||
typescript@5.9.2: {}
|
typescript@5.9.2: {}
|
||||||
|
|
||||||
uglify-js@3.19.3:
|
|
||||||
optional: true
|
|
||||||
|
|
||||||
uint8array-extras@1.5.0: {}
|
uint8array-extras@1.5.0: {}
|
||||||
|
|
||||||
unbox-primitive@1.1.0:
|
unbox-primitive@1.1.0:
|
||||||
@@ -11820,8 +11805,6 @@ snapshots:
|
|||||||
|
|
||||||
word-wrap@1.2.5: {}
|
word-wrap@1.2.5: {}
|
||||||
|
|
||||||
wordwrap@1.0.0: {}
|
|
||||||
|
|
||||||
wrap-ansi@7.0.0:
|
wrap-ansi@7.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
ansi-styles: 4.3.0
|
ansi-styles: 4.3.0
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Payload } from 'payload'
|
import { Payload } from 'payload'
|
||||||
import Handlebars from 'handlebars'
|
import { Liquid } from 'liquidjs'
|
||||||
import nodemailer, { Transporter } from 'nodemailer'
|
import nodemailer, { Transporter } from 'nodemailer'
|
||||||
import {
|
import {
|
||||||
MailingPluginConfig,
|
MailingPluginConfig,
|
||||||
@@ -18,6 +18,7 @@ export class MailingService implements IMailingService {
|
|||||||
private transporter!: Transporter | any
|
private transporter!: Transporter | any
|
||||||
private templatesCollection: string
|
private templatesCollection: string
|
||||||
private emailsCollection: string
|
private emailsCollection: string
|
||||||
|
private liquid: Liquid | null = null
|
||||||
|
|
||||||
constructor(payload: Payload, config: MailingPluginConfig) {
|
constructor(payload: Payload, config: MailingPluginConfig) {
|
||||||
this.payload = payload
|
this.payload = payload
|
||||||
@@ -30,7 +31,7 @@ export class MailingService implements IMailingService {
|
|||||||
this.emailsCollection = typeof emailsConfig === 'string' ? emailsConfig : 'emails'
|
this.emailsCollection = typeof emailsConfig === 'string' ? emailsConfig : 'emails'
|
||||||
|
|
||||||
this.initializeTransporter()
|
this.initializeTransporter()
|
||||||
this.registerHandlebarsHelpers()
|
this.initializeTemplateEngine()
|
||||||
}
|
}
|
||||||
|
|
||||||
private initializeTransporter(): void {
|
private initializeTransporter(): void {
|
||||||
@@ -62,39 +63,65 @@ export class MailingService implements IMailingService {
|
|||||||
return fromEmail || ''
|
return fromEmail || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerHandlebarsHelpers(): void {
|
private initializeTemplateEngine(): void {
|
||||||
Handlebars.registerHelper('formatDate', (date: Date, format?: string) => {
|
// Skip initialization if custom template renderer is provided
|
||||||
if (!date) return ''
|
if (this.config.templateRenderer) {
|
||||||
const d = new Date(date)
|
return
|
||||||
if (format === 'short') {
|
}
|
||||||
return d.toLocaleDateString()
|
|
||||||
}
|
// Use specified template engine or default to 'liquidjs'
|
||||||
if (format === 'long') {
|
const engine = this.config.templateEngine || 'liquidjs'
|
||||||
return d.toLocaleDateString('en-US', {
|
|
||||||
year: 'numeric',
|
if (engine === 'liquidjs') {
|
||||||
month: 'long',
|
this.initializeLiquidJS()
|
||||||
day: 'numeric'
|
} else if (engine === 'mustache') {
|
||||||
|
// Mustache doesn't need initialization, we'll use it directly in renderTemplate
|
||||||
|
this.liquid = null
|
||||||
|
} else if (engine === 'simple') {
|
||||||
|
this.liquid = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private initializeLiquidJS(): void {
|
||||||
|
try {
|
||||||
|
const { Liquid: LiquidEngine } = require('liquidjs')
|
||||||
|
this.liquid = new LiquidEngine()
|
||||||
|
|
||||||
|
// Register custom filters (equivalent to Handlebars helpers)
|
||||||
|
if (this.liquid) {
|
||||||
|
this.liquid.registerFilter('formatDate', (date: any, format?: string) => {
|
||||||
|
if (!date) return ''
|
||||||
|
const d = new Date(date)
|
||||||
|
if (format === 'short') {
|
||||||
|
return d.toLocaleDateString()
|
||||||
|
}
|
||||||
|
if (format === 'long') {
|
||||||
|
return d.toLocaleDateString('en-US', {
|
||||||
|
year: 'numeric',
|
||||||
|
month: 'long',
|
||||||
|
day: 'numeric'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return d.toLocaleString()
|
||||||
|
})
|
||||||
|
|
||||||
|
this.liquid.registerFilter('formatCurrency', (amount: any, currency = 'USD') => {
|
||||||
|
if (typeof amount !== 'number') return amount
|
||||||
|
return new Intl.NumberFormat('en-US', {
|
||||||
|
style: 'currency',
|
||||||
|
currency: currency
|
||||||
|
}).format(amount)
|
||||||
|
})
|
||||||
|
|
||||||
|
this.liquid.registerFilter('capitalize', (str: any) => {
|
||||||
|
if (typeof str !== 'string') return str
|
||||||
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return d.toLocaleString()
|
} catch (error) {
|
||||||
})
|
console.warn('LiquidJS not available. Falling back to simple variable replacement. Install liquidjs or use a different templateEngine.')
|
||||||
|
this.liquid = null
|
||||||
Handlebars.registerHelper('formatCurrency', (amount: number, currency = 'USD') => {
|
}
|
||||||
if (typeof amount !== 'number') return amount
|
|
||||||
return new Intl.NumberFormat('en-US', {
|
|
||||||
style: 'currency',
|
|
||||||
currency: currency
|
|
||||||
}).format(amount)
|
|
||||||
})
|
|
||||||
|
|
||||||
Handlebars.registerHelper('ifEquals', function(this: any, arg1: any, arg2: any, options: any) {
|
|
||||||
return (arg1 === arg2) ? options.fn(this) : options.inverse(this)
|
|
||||||
})
|
|
||||||
|
|
||||||
Handlebars.registerHelper('capitalize', (str: string) => {
|
|
||||||
if (typeof str !== 'string') return str
|
|
||||||
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendEmail(options: SendEmailOptions): Promise<string> {
|
async sendEmail(options: SendEmailOptions): Promise<string> {
|
||||||
@@ -123,7 +150,7 @@ export class MailingService implements IMailingService {
|
|||||||
const renderedContent = await this.renderEmailTemplate(template, variables)
|
const renderedContent = await this.renderEmailTemplate(template, variables)
|
||||||
html = renderedContent.html
|
html = renderedContent.html
|
||||||
text = renderedContent.text
|
text = renderedContent.text
|
||||||
subject = this.renderHandlebarsTemplate(template.subject, variables)
|
subject = await this.renderTemplate(template.subject, variables)
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Email template not found: ${options.templateSlug}`)
|
throw new Error(`Email template not found: ${options.templateSlug}`)
|
||||||
}
|
}
|
||||||
@@ -356,14 +383,49 @@ export class MailingService implements IMailingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private renderHandlebarsTemplate(template: string, variables: Record<string, any>): string {
|
private async renderTemplate(template: string, variables: Record<string, any>): Promise<string> {
|
||||||
try {
|
// Use custom template renderer if provided
|
||||||
const compiled = Handlebars.compile(template)
|
if (this.config.templateRenderer) {
|
||||||
return compiled(variables)
|
try {
|
||||||
} catch (error) {
|
return await this.config.templateRenderer(template, variables)
|
||||||
console.error('Handlebars template rendering error:', error)
|
} catch (error) {
|
||||||
return template
|
console.error('Custom template renderer error:', error)
|
||||||
|
return template
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const engine = this.config.templateEngine || 'liquidjs'
|
||||||
|
|
||||||
|
// Use LiquidJS if available and configured
|
||||||
|
if (engine === 'liquidjs' && this.liquid) {
|
||||||
|
try {
|
||||||
|
return await this.liquid.parseAndRender(template, variables)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('LiquidJS template rendering error:', error)
|
||||||
|
return template
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use Mustache if configured
|
||||||
|
if (engine === 'mustache') {
|
||||||
|
try {
|
||||||
|
const Mustache = require('mustache')
|
||||||
|
return Mustache.render(template, variables)
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Mustache not available. Falling back to simple variable replacement. Install mustache package.')
|
||||||
|
return this.simpleVariableReplacement(template, variables)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to simple variable replacement
|
||||||
|
return this.simpleVariableReplacement(template, variables)
|
||||||
|
}
|
||||||
|
|
||||||
|
private simpleVariableReplacement(template: string, variables: Record<string, any>): string {
|
||||||
|
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
||||||
|
const value = variables[key]
|
||||||
|
return value !== undefined ? String(value) : match
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async renderEmailTemplate(template: EmailTemplate, variables: Record<string, any> = {}): Promise<{ html: string; text: string }> {
|
private async renderEmailTemplate(template: EmailTemplate, variables: Record<string, any> = {}): Promise<{ html: string; text: string }> {
|
||||||
@@ -375,9 +437,9 @@ export class MailingService implements IMailingService {
|
|||||||
let html = serializeRichTextToHTML(template.content)
|
let html = serializeRichTextToHTML(template.content)
|
||||||
let text = serializeRichTextToText(template.content)
|
let text = serializeRichTextToText(template.content)
|
||||||
|
|
||||||
// Apply Handlebars variables to the rendered content
|
// Apply template variables to the rendered content
|
||||||
html = this.renderHandlebarsTemplate(html, variables)
|
html = await this.renderTemplate(html, variables)
|
||||||
text = this.renderHandlebarsTemplate(text, variables)
|
text = await this.renderTemplate(text, variables)
|
||||||
|
|
||||||
return { html, text }
|
return { html, text }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ export interface EmailObject {
|
|||||||
|
|
||||||
export type EmailWrapperHook = (email: EmailObject) => EmailObject | Promise<EmailObject>
|
export type EmailWrapperHook = (email: EmailObject) => EmailObject | Promise<EmailObject>
|
||||||
|
|
||||||
|
export type TemplateRendererHook = (template: string, variables: Record<string, any>) => string | Promise<string>
|
||||||
|
|
||||||
|
export type TemplateEngine = 'liquidjs' | 'mustache' | 'simple'
|
||||||
|
|
||||||
export interface MailingPluginConfig {
|
export interface MailingPluginConfig {
|
||||||
collections?: {
|
collections?: {
|
||||||
templates?: string | Partial<CollectionConfig>
|
templates?: string | Partial<CollectionConfig>
|
||||||
@@ -28,6 +32,8 @@ export interface MailingPluginConfig {
|
|||||||
retryAttempts?: number
|
retryAttempts?: number
|
||||||
retryDelay?: number
|
retryDelay?: number
|
||||||
emailWrapper?: EmailWrapperHook
|
emailWrapper?: EmailWrapperHook
|
||||||
|
templateRenderer?: TemplateRendererHook
|
||||||
|
templateEngine?: TemplateEngine
|
||||||
richTextEditor?: RichTextField['editor']
|
richTextEditor?: RichTextField['editor']
|
||||||
onReady?: (payload: any) => Promise<void>
|
onReady?: (payload: any) => Promise<void>
|
||||||
initOrder?: 'before' | 'after'
|
initOrder?: 'before' | 'after'
|
||||||
|
|||||||
160
template-syntax-migration.md
Normal file
160
template-syntax-migration.md
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
# Template Engine Options
|
||||||
|
|
||||||
|
The plugin now supports flexible template rendering with multiple options:
|
||||||
|
|
||||||
|
1. **String-based Configuration** (easy setup with built-in engines)
|
||||||
|
2. **Custom Template Renderer Hook** (maximum flexibility)
|
||||||
|
3. **Simple Variable Replacement** (fallback, no dependencies)
|
||||||
|
|
||||||
|
## Configuration Options
|
||||||
|
|
||||||
|
### String-based Template Engine Configuration
|
||||||
|
Easy setup using built-in template engines:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Using LiquidJS (default, requires: npm install liquidjs)
|
||||||
|
mailingPlugin({
|
||||||
|
templateEngine: 'liquidjs'
|
||||||
|
})
|
||||||
|
|
||||||
|
// Using Mustache (requires: npm install mustache)
|
||||||
|
mailingPlugin({
|
||||||
|
templateEngine: 'mustache'
|
||||||
|
})
|
||||||
|
|
||||||
|
// Using simple variable replacement (no dependencies)
|
||||||
|
mailingPlugin({
|
||||||
|
templateEngine: 'simple'
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Custom Template Renderer Hook
|
||||||
|
```typescript
|
||||||
|
// Example with Handlebars
|
||||||
|
import Handlebars from 'handlebars'
|
||||||
|
|
||||||
|
mailingPlugin({
|
||||||
|
templateRenderer: async (template: string, variables: Record<string, any>) => {
|
||||||
|
const compiled = Handlebars.compile(template)
|
||||||
|
return compiled(variables)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Example with Mustache
|
||||||
|
import Mustache from 'mustache'
|
||||||
|
|
||||||
|
mailingPlugin({
|
||||||
|
templateRenderer: async (template: string, variables: Record<string, any>) => {
|
||||||
|
return Mustache.render(template, variables)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Example with Nunjucks
|
||||||
|
import nunjucks from 'nunjucks'
|
||||||
|
|
||||||
|
mailingPlugin({
|
||||||
|
templateRenderer: async (template: string, variables: Record<string, any>) => {
|
||||||
|
return nunjucks.renderString(template, variables)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using LiquidJS (Optional)
|
||||||
|
Install the optional dependency:
|
||||||
|
```bash
|
||||||
|
npm install liquidjs
|
||||||
|
# or
|
||||||
|
pnpm add liquidjs
|
||||||
|
```
|
||||||
|
|
||||||
|
### Fallback Mode
|
||||||
|
If no custom renderer is provided and neither LiquidJS nor Mustache are installed, simple `{{variable}}` replacement is used.
|
||||||
|
|
||||||
|
## Template Syntax Reference
|
||||||
|
|
||||||
|
### Mustache Syntax (Logic-less)
|
||||||
|
```mustache
|
||||||
|
Hello {{user.name}},
|
||||||
|
|
||||||
|
{{#user.isPremium}}
|
||||||
|
Welcome to premium! Your balance is {{balance}}.
|
||||||
|
{{/user.isPremium}}
|
||||||
|
|
||||||
|
{{#orders}}
|
||||||
|
Order: {{id}} - {{date}}
|
||||||
|
{{/orders}}
|
||||||
|
```
|
||||||
|
|
||||||
|
### LiquidJS Syntax (With Logic)
|
||||||
|
```liquid
|
||||||
|
Hello {{user.name}},
|
||||||
|
|
||||||
|
{% if user.isPremium %}
|
||||||
|
Welcome to premium! Your balance is {{balance | formatCurrency}}.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% for order in orders %}
|
||||||
|
Order: {{order.id}} - {{order.date | formatDate: "short"}}
|
||||||
|
{% endfor %}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Simple Variable Replacement
|
||||||
|
```
|
||||||
|
Hello {{user.name}},
|
||||||
|
Your balance is {{balance}}.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Migration from Handlebars
|
||||||
|
|
||||||
|
### Variables
|
||||||
|
- **Handlebars**: `{{variable}}`
|
||||||
|
- **LiquidJS**: `{{variable}}` (same)
|
||||||
|
|
||||||
|
### Conditionals
|
||||||
|
- **Handlebars**: `{{#if condition}}content{{/if}}`
|
||||||
|
- **LiquidJS**: `{% if condition %}content{% endif %}`
|
||||||
|
|
||||||
|
### Loops
|
||||||
|
- **Handlebars**: `{{#each items}}{{this}}{{/each}}`
|
||||||
|
- **LiquidJS**: `{% for item in items %}{{item}}{% endfor %}`
|
||||||
|
|
||||||
|
### Filters/Helpers
|
||||||
|
- **Handlebars**: `{{formatDate date "short"}}`
|
||||||
|
- **LiquidJS**: `{{date | formatDate: "short"}}`
|
||||||
|
|
||||||
|
### Available Filters
|
||||||
|
- `formatDate` - Format dates (short, long, or default)
|
||||||
|
- `formatCurrency` - Format currency amounts
|
||||||
|
- `capitalize` - Capitalize first letter
|
||||||
|
|
||||||
|
### Comparison Operations (LiquidJS Advantage)
|
||||||
|
- **Handlebars**: Required `{{#ifEquals}}` helper
|
||||||
|
- **LiquidJS**: Built-in: `{% if user.role == "admin" %}`
|
||||||
|
|
||||||
|
## Example Migration
|
||||||
|
|
||||||
|
### Before (Handlebars)
|
||||||
|
```handlebars
|
||||||
|
Hello {{user.name}},
|
||||||
|
|
||||||
|
{{#if user.isPremium}}
|
||||||
|
Welcome to premium! Your balance is {{formatCurrency balance}}.
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
{{#each orders}}
|
||||||
|
Order: {{this.id}} - {{formatDate this.date "short"}}
|
||||||
|
{{/each}}
|
||||||
|
```
|
||||||
|
|
||||||
|
### After (LiquidJS)
|
||||||
|
```liquid
|
||||||
|
Hello {{user.name}},
|
||||||
|
|
||||||
|
{% if user.isPremium %}
|
||||||
|
Welcome to premium! Your balance is {{balance | formatCurrency}}.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% for order in orders %}
|
||||||
|
Order: {{order.id}} - {{order.date | formatDate: "short"}}
|
||||||
|
{% endfor %}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user