79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
/**
|
|
* Renders the email that greets a new account. `sendWelcomeEmail`
|
|
* (`user-service.ts`) sends it after sign-up, in the instance's style and
|
|
* language. Its body is Markdown: the instance's `GERBEUR_WELCOME_EMAIL_BODY`,
|
|
* or the built-in one.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { Marked } from "marked";
|
|
|
|
import {
|
|
DEFAULT_STYLE,
|
|
EMAIL_LOCALE,
|
|
FRONTEND_URL,
|
|
OG_SITE_NAME,
|
|
WELCOME_EMAIL_BODY,
|
|
} from "../config.ts";
|
|
import { EmailKit, esc, type RenderedEmail } from "./layout.ts";
|
|
import { STRINGS } from "./strings.ts";
|
|
|
|
/**
|
|
* Returns a Markdown renderer whose blocks are `kit`'s, so the body takes the
|
|
* email's style. Raw HTML in the Markdown passes through: the body comes from
|
|
* whoever runs the instance, never from a user.
|
|
*/
|
|
function markdownIn(kit: EmailKit): Marked {
|
|
return new Marked({
|
|
renderer: {
|
|
heading({ tokens, depth }) {
|
|
const html = this.parser.parseInline(tokens);
|
|
return depth === 1 ? kit.heading(html, 20) : kit.subhead(html);
|
|
},
|
|
paragraph({ tokens }) {
|
|
return kit.para(this.parser.parseInline(tokens));
|
|
},
|
|
link({ href, tokens }) {
|
|
return kit.link(this.parser.parseInline(tokens), href);
|
|
},
|
|
strong({ tokens }) {
|
|
return `<strong style="font-weight:${
|
|
Math.max(600, kit.style.heavy)
|
|
};">${this.parser.parseInline(tokens)}</strong>`;
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns the welcome email of `username`, with `{{username}}` and
|
|
* `{{site_name}}` filled into the body.
|
|
*/
|
|
export function renderWelcomeEmail(username: string): RenderedEmail {
|
|
const t = STRINGS[EMAIL_LOCALE].welcome;
|
|
const kit = new EmailKit(
|
|
{ style: DEFAULT_STYLE, scheme: "auto" },
|
|
EMAIL_LOCALE,
|
|
);
|
|
const markdown = (WELCOME_EMAIL_BODY ?? t.body)
|
|
.replaceAll("{{username}}", username)
|
|
.replaceAll("{{site_name}}", OG_SITE_NAME);
|
|
const subject = t.subject(OG_SITE_NAME);
|
|
const body = markdownIn(kit).parse(markdown, { async: false }) +
|
|
kit.button(esc(t.button(OG_SITE_NAME)), FRONTEND_URL);
|
|
|
|
return {
|
|
subject,
|
|
html: kit.document({
|
|
subject,
|
|
preheader: t.preheader,
|
|
body,
|
|
footer: kit.footer(esc(t.reason(OG_SITE_NAME))),
|
|
}),
|
|
text: `${markdown}\n\n${t.button(OG_SITE_NAME)}: ${FRONTEND_URL}\n\n--\n${
|
|
t.reason(OG_SITE_NAME)
|
|
}`,
|
|
};
|
|
}
|