@eristack/email-template
Version:
{{var}} HTML/text email template render and key extraction — pair with @eristack/comms
25 lines • 808 B
JavaScript
// src/core/template.ts
var PLACEHOLDER_RE = /\{\{\s*([\w.]+)\s*\}\}/g;
function extractTemplateKeys(template) {
const keys = /* @__PURE__ */ new Set();
for (const match of template.matchAll(PLACEHOLDER_RE)) {
keys.add(match[1]);
}
return [...keys].sort();
}
function htmlEscape(value) {
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """).replaceAll("'", "'");
}
function renderEmailTemplate(template, vars, options) {
const escape = options?.escapeHtml ?? false;
return template.replace(PLACEHOLDER_RE, (_full, key) => {
const raw = vars[key];
if (raw === void 0) return "";
return escape ? htmlEscape(raw) : raw;
});
}
export {
extractTemplateKeys,
renderEmailTemplate
};
//# sourceMappingURL=index.js.map