Files
payload/packages/plugin-form-builder/src/utilities/replaceDoubleCurlys.ts
Paul 9821aeb67a feat(plugin-form-builder): new wildcards {{*}} and {{*:table}} are now supported in email bodies and emails fall back to a global configuration value in addition to base email configuration (#8271)
Email bodies in the plugin form builder now support wildcards `{{*}}`
and `{{*:table}}` to export all the form submission data in key:value
pairs with the latter formatted as a table.

Emails also fallback to a global plugin configuration item and then to
the `defaultFromAddress` address in email transport config.
2024-09-18 00:28:52 +00:00

39 lines
1.0 KiB
TypeScript

import { keyValuePairToHtmlTable } from './keyValuePairToHtmlTable.js'
interface EmailVariable {
field: string
value: string
}
type EmailVariables = EmailVariable[]
export const replaceDoubleCurlys = (str: string, variables?: EmailVariables): string => {
const regex = /\{\{(.+?)\}\}/g
if (str && variables) {
return str.replace(regex, (_, variable: string) => {
if (variable.includes('*')) {
if (variable === '*') {
return variables.map(({ field, value }) => `${field} : ${value}`).join(' <br /> ')
} else if (variable === '*:table') {
return keyValuePairToHtmlTable(
variables.reduce((acc, { field, value }) => {
acc[field] = value
return acc
}, {}),
)
}
} else {
const foundVariable = variables.find(({ field: fieldName }) => {
return variable === fieldName
})
if (foundVariable) {
return foundVariable.value
}
}
return variable
})
}
return str
}