,
): input is EmailBody {
return (
input.html !== undefined ||
input.markdown !== undefined ||
input.text !== undefined
)
}
/**
* Produces complete HTML and plain-text MIME alternatives.
*
* @param input - One or more caller-supplied body formats.
*/
export function normalizeEmailBody(input: EmailBody): {
html: string
text: string
} {
if (input.markdown !== undefined) {
const markdownHtml = marked.parse(input.markdown, { async: false })
return {
html: input.html ?? markdownHtml,
text: input.text ?? HTML_TO_TEXT(markdownHtml),
}
}
if (input.html !== undefined) {
return {
html: input.html,
text: input.text ?? HTML_TO_TEXT(input.html),
}
}
// EMAIL_BODY_SCHEMA guarantees text in the only remaining union member.
const text = input.text!
return {
html: `${escapeHtml(text).replaceAll(/\r?\n/g, "
")}
`,
text,
}
}