feat(scripts): add mustache render-template via Responses pages
Add HTML template kind to Responses with a seeded system email-default template, render-template.js using Mustache, and html body support in send-email. Templates are editable but system pages cannot be deleted. Co-authored-by: Nasyarobby Putra <nasyarobby@gmail.com>
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
import Mustache from "mustache";
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
*/
|
||||
function htmlToText(html) {
|
||||
return html
|
||||
.replace(/<style[\s\S]*?<\/style>/gi, "")
|
||||
.replace(/<script[\s\S]*?<\/script>/gi, "")
|
||||
.replace(/<br\s*\/?>/gi, "\n")
|
||||
.replace(/<\/(p|div|h[1-6]|li|tr)>/gi, "\n")
|
||||
.replace(/<li[^>]*>/gi, "- ")
|
||||
.replace(/<[^>]+>/g, "")
|
||||
.replace(/ /g, " ")
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {unknown} partialsConfig
|
||||
*/
|
||||
async function loadPartials(partialsConfig) {
|
||||
/** @type {Record<string, string>} */
|
||||
const partials = {};
|
||||
if (partialsConfig == null) return partials;
|
||||
if (typeof partialsConfig !== "object" || Array.isArray(partialsConfig)) {
|
||||
throw new Error("config.partials must be an object");
|
||||
}
|
||||
|
||||
for (const [partialName, pageName] of Object.entries(
|
||||
/** @type {Record<string, unknown>} */ (partialsConfig),
|
||||
)) {
|
||||
if (typeof pageName !== "string" || pageName.length === 0) {
|
||||
throw new Error(`config.partials.${partialName} must be a template page name`);
|
||||
}
|
||||
partials[partialName] = await $responses.getTemplate(pageName);
|
||||
}
|
||||
return partials;
|
||||
}
|
||||
|
||||
async function renderTemplate(ctx) {
|
||||
const templateName = ctx.config?.template;
|
||||
if (typeof templateName !== "string" || templateName.length === 0) {
|
||||
throw new Error("config.template is required");
|
||||
}
|
||||
|
||||
const vars = ctx.data?.vars ?? ctx.data;
|
||||
if (vars == null || typeof vars !== "object" || Array.isArray(vars)) {
|
||||
throw new Error("data.vars must be an object");
|
||||
}
|
||||
|
||||
const template = await $responses.getTemplate(templateName);
|
||||
const partials = await loadPartials(ctx.config?.partials);
|
||||
|
||||
log.info(
|
||||
{
|
||||
template: templateName,
|
||||
partials: Object.keys(partials),
|
||||
varKeys: Object.keys(vars),
|
||||
},
|
||||
"render-template: rendering mustache template",
|
||||
);
|
||||
|
||||
const html = Mustache.render(template, vars, partials);
|
||||
const text = htmlToText(html);
|
||||
|
||||
return {
|
||||
html,
|
||||
text,
|
||||
template: templateName,
|
||||
};
|
||||
}
|
||||
|
||||
renderTemplate.meta = {
|
||||
description:
|
||||
"Render an HTML template from Responses (kind: template) with Mustache and return html + plain-text fallback",
|
||||
config: {
|
||||
template: {
|
||||
type: "string",
|
||||
required: true,
|
||||
description: "Responses page name with kind=template",
|
||||
},
|
||||
partials: {
|
||||
type: "object",
|
||||
required: false,
|
||||
description: "Map of partial name to template page name (e.g. { item: email-item })",
|
||||
},
|
||||
},
|
||||
input: {
|
||||
vars: {
|
||||
type: "object",
|
||||
required: true,
|
||||
description: "Mustache view data (title, items, etc.)",
|
||||
},
|
||||
},
|
||||
output: {
|
||||
html: { type: "string", description: "Rendered HTML" },
|
||||
text: { type: "string", description: "Plain-text fallback stripped from HTML" },
|
||||
template: { type: "string", description: "Template page name used" },
|
||||
},
|
||||
example: {
|
||||
data: {
|
||||
vars: {
|
||||
title: "Daily digest",
|
||||
message: "Latest items from your feed.",
|
||||
items: [
|
||||
{
|
||||
title: "Example post",
|
||||
link: "https://example.com/post",
|
||||
summary: "A short summary.",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
config: {
|
||||
template: "email-default",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default renderTemplate;
|
||||
@@ -147,8 +147,11 @@ async function sendEmail(ctx) {
|
||||
}
|
||||
|
||||
const text = ctx.data?.text ?? ctx.data?.body ?? ctx.data?.message;
|
||||
if (typeof text !== "string" || text.length === 0) {
|
||||
throw new Error("data.text is required (plain-text body)");
|
||||
const html = ctx.data?.html;
|
||||
const hasText = typeof text === "string" && text.length > 0;
|
||||
const hasHtml = typeof html === "string" && html.length > 0;
|
||||
if (!hasText && !hasHtml) {
|
||||
throw new Error("data.text or data.html is required");
|
||||
}
|
||||
|
||||
const cc = normalizeRecipients(ctx.data?.cc, "data.cc");
|
||||
@@ -174,8 +177,9 @@ async function sendEmail(ctx) {
|
||||
from,
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
};
|
||||
if (hasText) mail.text = text;
|
||||
if (hasHtml) mail.html = html;
|
||||
if (cc) mail.cc = cc;
|
||||
if (bcc) mail.bcc = bcc;
|
||||
if (replyTo) mail.replyTo = replyTo;
|
||||
@@ -203,9 +207,10 @@ async function sendEmail(ctx) {
|
||||
cc: cc ?? null,
|
||||
bcc: bcc ? "[redacted]" : null,
|
||||
subjectLength: subject.length,
|
||||
textLength: text.length,
|
||||
textLength: hasText ? text.length : 0,
|
||||
htmlLength: hasHtml ? html.length : 0,
|
||||
},
|
||||
"send-email: sending plain-text message",
|
||||
"send-email: sending message",
|
||||
);
|
||||
|
||||
const info = await transporter.sendMail(mail);
|
||||
@@ -224,7 +229,7 @@ async function sendEmail(ctx) {
|
||||
}
|
||||
|
||||
sendEmail.meta = {
|
||||
description: "Send a plain-text email via SMTP (nodemailer)",
|
||||
description: "Send an email via SMTP (nodemailer); plain text, HTML, or both",
|
||||
config: {
|
||||
service: {
|
||||
type: "string",
|
||||
@@ -387,9 +392,14 @@ sendEmail.meta = {
|
||||
subject: { type: "string", required: true, description: "Email subject" },
|
||||
text: {
|
||||
type: "string",
|
||||
required: true,
|
||||
required: false,
|
||||
description: "Plain-text body (aliases: body, message)",
|
||||
},
|
||||
html: {
|
||||
type: "string",
|
||||
required: false,
|
||||
description: "HTML body (e.g. from render-template.js)",
|
||||
},
|
||||
priority: {
|
||||
type: "string",
|
||||
required: false,
|
||||
|
||||
Reference in New Issue
Block a user