import { ASSETS, getAssetPath } from "../assetUtils";
import { communityPossessive } from "../create/ruleInviteCardMeta";
import { RULE_MD_FILENAME } from "../create/ruleExportModel";
import { mailLegalFooterHtml, mailLegalFooterUrls } from "./mailLegalFooter";
export const RULE_FOLLOW_UP_LOGO_PATH = getAssetPath(ASSETS.LOGO_MARK);
export type RuleFollowUpMailInput = {
to: string;
origin: string;
communityName: string;
pdfUrl: string;
markdownUrl: string;
};
export type RuleFollowUpMailContent = {
subject: string;
previewText: string;
text: string;
html: string;
};
function escapeHtml(value: string): string {
return value
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """);
}
function originHost(origin: string): string {
try {
return new URL(origin).host;
} catch {
return origin.replace(/^https?:\/\//, "").replace(/\/$/, "");
}
}
export function buildRuleFollowUpMail(
input: RuleFollowUpMailInput,
): RuleFollowUpMailContent {
const communityName = input.communityName.trim() || "A community";
const possessive = communityPossessive(communityName);
const origin = input.origin.replace(/\/$/, "");
const kitUrl = `${origin}/kit`;
const host = originHost(origin);
const subject = `${possessive} Rule, printed`;
const previewText = `Here is ${possessive} Rule as a PDF, ready to print.`;
const intro = `Here is ${possessive} Rule as a PDF, ready to print or drop into your onboarding doc.`;
const putIt =
"Put it where members will see it, on the wall where you meet or at the top of the doc new members read.";
const fixNow =
"If anything in it already feels wrong, that is normal, and it is easier to fix now than in a year.";
const officeHours = `Bring it to office hours if you want a second read. No signup, and the next one is listed at ${host}/kit.`;
const signOff = "CommunityRule";
const why = `Sent because you finished a Rule at ${host}.`;
const madeBy =
"CommunityRule is made by the Media Economies Design Lab at the University of Colorado Boulder.";
const { privacyUrl } = mailLegalFooterUrls(origin);
const logoUrl = `${origin}${RULE_FOLLOW_UP_LOGO_PATH}`;
const text = [
"Your Rule is ready to print",
"",
intro,
"",
"Download the PDF",
input.pdfUrl,
"",
`Or download ${RULE_MD_FILENAME} for a wiki or a repository.`,
input.markdownUrl,
"",
putIt,
"",
fixNow,
"",
officeHours,
kitUrl,
"",
signOff,
"",
why,
"",
madeBy,
...(privacyUrl ? [`Privacy ${privacyUrl}`] : []),
].join("\n");
const html = followUpHtml({
subject,
previewText,
intro,
pdfUrl: input.pdfUrl,
markdownUrl: input.markdownUrl,
putIt,
fixNow,
officeHours,
kitUrl,
signOff,
why,
madeBy,
origin,
logoUrl,
kitHostPath: `${host}/kit`,
});
return { subject, previewText, text, html };
}
function followUpHtml(opts: {
subject: string;
previewText: string;
intro: string;
pdfUrl: string;
markdownUrl: string;
putIt: string;
fixNow: string;
officeHours: string;
kitUrl: string;
signOff: string;
why: string;
madeBy: string;
origin: string;
logoUrl: string;
kitHostPath: string;
}): string {
const pdfHref = escapeHtml(opts.pdfUrl);
const mdHref = escapeHtml(opts.markdownUrl);
const kitHref = escapeHtml(opts.kitUrl);
const kitHostPath = escapeHtml(opts.kitHostPath);
const officeLead = "Bring it to office hours if you want a second read. No signup, and the next one is listed at ";
const officeHtml = opts.officeHours.startsWith(officeLead)
? `${escapeHtml(officeLead)}${kitHostPath}.`
: escapeHtml(opts.officeHours);
return `
${escapeHtml(opts.subject)}
${escapeHtml(
opts.previewText,
)}
| CommunityRule |
|
|
Your Rule is ready to print
${escapeHtml(opts.intro)}
Download the PDF
Or download ${RULE_MD_FILENAME} for a wiki or a repository.
${escapeHtml(opts.putIt)}
${escapeHtml(opts.fixNow)}
${officeHtml}
${escapeHtml(opts.signOff)}
${escapeHtml(opts.why)}
${escapeHtml(opts.madeBy)}
${mailLegalFooterHtml(opts.origin)}
|
|
`;
}