packages/ at markup-kit dd1e6eb5270e
A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
This commit is contained in:
commit
306b143104
72 changed files with 10915 additions and 0 deletions
45
email/template.weft
Normal file
45
email/template.weft
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// Email templates are Weft components: write one with the components in `components/`, render it with `render` from `std/app`,
|
||||
// and give the result to `document`. Everything shown in a component is escaped, so values from users are safe in it.
|
||||
/// An email rendered both ways: what `render` from `std/app` gives.
|
||||
export type Rendered = { html: string, text: string }
|
||||
|
||||
fn escape(s: string): string {
|
||||
return s.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll("\"", """)
|
||||
}
|
||||
|
||||
/// Whether an address may go in a link or an image: `http`, `https`, `mailto`, `tel` or `cid`.
|
||||
export fn isSafeUrl(url: string): boolean {
|
||||
let lower = url.trim().toLowerCase()
|
||||
return lower.startsWith("http://") ||
|
||||
lower.startsWith("https://") ||
|
||||
lower.startsWith("mailto:") ||
|
||||
lower.startsWith("tel:") ||
|
||||
lower.startsWith("cid:")
|
||||
}
|
||||
|
||||
/// What surrounds an email's content. `preheader` is the line inboxes show beside the subject; `footer` is the small print;
|
||||
/// `lang` is the language of the text (`en`).
|
||||
export type DocumentOptions = { title?: string, preheader?: string, footer?: string, lang?: string }
|
||||
|
||||
/// A rendered template as a whole email: its content in a centred column, with the preheader and footer.
|
||||
///
|
||||
/// ```weft
|
||||
/// let mail = document(render(Receipt, { name: "Ana", total: "19,99 €" }), { title: "Your receipt", footer: "Example d.o.o." })
|
||||
/// ```
|
||||
export fn document(content: Rendered, options: DocumentOptions = {}): Rendered {
|
||||
let body = content.html
|
||||
let preheader = options.preheader ?? ""
|
||||
let hidden = preheader != ""
|
||||
? `<div style="display:none;max-height:0;overflow:hidden;opacity:0">${escape(preheader)}</div>`
|
||||
: ""
|
||||
let footer = options.footer ?? ""
|
||||
let small = footer != ""
|
||||
? `<p style="margin:24px 0 0;font-size:12px;line-height:1.5;color:#8a8a84">${escape(footer)}</p>`
|
||||
: ""
|
||||
let html = `<!doctype html><html lang="${escape(options.lang ?? "en")}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escape(options.title ?? "")}</title></head><body style="margin:0;padding:0;background:#f6f6f3">${hidden}<table role="presentation" width="100%" style="border-collapse:collapse"><tr><td align="center" style="padding:24px 12px"><table role="presentation" width="100%" style="max-width:560px;border-collapse:collapse;background:#ffffff;border-radius:8px"><tr><td style="padding:28px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:15px;line-height:1.55;color:#1c1c1a">${body}${small}</td></tr></table></td></tr></table></body></html>`
|
||||
let plain = content.text
|
||||
return { html, text: footer != "" ? `${plain}\n\n--\n${footer}\n` : `${plain}\n` }
|
||||
}
|
||||
Loading…
Reference in a new issue