Tools → Newsletter saves drafts (migration 057: newsletters, with content blocks owned 'newsletter'), adds blocks written for the issue and an editable footer (no unsubscribe link: Mailchimp adds one), and turns a draft into an unpublished blog post. The admin engine binds child owner ids as text, so an auto-numbered record's blocks are owned by '1', not '1.0'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
307 lines
14 KiB
TypeScript
307 lines
14 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
NEWSLETTER: email-ready HTML and plain text from picked items
|
|
|
|
The admin's Newsletter tool (AdminNewsletter.tsx) gathers events,
|
|
blog posts and featured cards, with content blocks written for the
|
|
issue; this turns them into one email to paste into Mailchimp,
|
|
Gmail or Church Center. Nothing is sent from here.
|
|
|
|
Blocks draw as their email-safe equivalent: headings, paragraphs
|
|
(with **bold**, *italic* and [links](url)), callouts, quotes,
|
|
lists, link and button rows, images, cards and dividers; a video
|
|
is its still linked to the video. Schedules, tables and people,
|
|
which need the site, are left out.
|
|
|
|
No unsubscribe link: Mailchimp adds its own to every send.
|
|
|
|
Email clients ignore stylesheets, CSS variables and most layout,
|
|
so the HTML is tables with inline styles at 600px, images with
|
|
width and alt, and the palette written out as hex (the one place
|
|
the site's colors are literals: an inbox has no theme tokens). A
|
|
plain-text copy goes alongside for clients that show no HTML.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
export type NewsletterItem = {
|
|
title: string
|
|
/** A line under the title: dates and place, a byline. */
|
|
meta?: string | null
|
|
text?: string | null
|
|
/** An absolute image URL. */
|
|
image?: string | null
|
|
/** An absolute link. */
|
|
href: string
|
|
cta: string
|
|
}
|
|
|
|
export type NewsletterSection = { heading: string; items: NewsletterItem[] }
|
|
|
|
/* A content block as the block editor holds it. */
|
|
export type NewsletterBlock = {
|
|
type: string
|
|
text?: string | null
|
|
media?: string | null
|
|
href?: string | null
|
|
alt?: string | null
|
|
cite?: string | null
|
|
items?: Array<{ text?: string | null; detail?: string | null; url?: string | null; media?: string | null }>
|
|
}
|
|
|
|
export type Newsletter = {
|
|
title: string
|
|
intro?: string | null
|
|
/** Written for this issue, under the intro. */
|
|
blocks?: NewsletterBlock[]
|
|
sections: NewsletterSection[]
|
|
/** The closing lines. Blank: the site's name and address. */
|
|
footer?: string | null
|
|
/** The site's address, for the header link and footer. */
|
|
origin: string
|
|
siteName: string
|
|
/** An absolute URL of the logo for the header. */
|
|
logo?: string | null
|
|
/** A site path or upload as an absolute address (blocks hold paths and bare filenames). */
|
|
absolute: (pathOrFile: string, kind: 'link' | 'image') => string
|
|
}
|
|
|
|
/* The palette as an inbox needs it. */
|
|
const COLOR = {
|
|
ink: '#001011',
|
|
body: '#003a45',
|
|
muted: '#3b5456',
|
|
teal: '#00748a',
|
|
page: '#f5f9f8',
|
|
card: '#ffffff',
|
|
rule: '#d6f5fa',
|
|
}
|
|
|
|
const FONT = "'Poppins', Helvetica, Arial, sans-serif"
|
|
const BODY_FONT = "'Montserrat', Helvetica, Arial, sans-serif"
|
|
|
|
const escape = (value: string) =>
|
|
value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
|
|
|
|
/* A link an email can follow: the site's own paths made absolute,
|
|
anything but http, https and mailto refused. */
|
|
function safeHref(href: string, n: Newsletter): string | null {
|
|
const value = href.trim()
|
|
if (/^(https?:|mailto:)/i.test(value)) return value
|
|
if (value.startsWith('/')) return n.absolute(value, 'link')
|
|
return null
|
|
}
|
|
|
|
/* Escaped text with the editor's **bold**, *italic* and [text](url). */
|
|
function inline(text: string, n: Newsletter): string {
|
|
return escape(text)
|
|
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_whole, label, href) => {
|
|
const url = safeHref(href.replace(/&/g, '&'), n)
|
|
return url ? `<a href="${escape(url)}" style="color:${COLOR.teal};">${label}</a>` : label
|
|
})
|
|
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
.replace(/\*([^*]+)\*/g, '<em>$1</em>')
|
|
}
|
|
|
|
/* Paragraphs from text with blank lines between them, formatted
|
|
when given the newsletter (its links need the site's address). */
|
|
const paragraphs = (text: string, style: string, n?: Newsletter) =>
|
|
text
|
|
.split(/\n\s*\n/)
|
|
.map((part) => part.trim())
|
|
.filter(Boolean)
|
|
.map((part) => `<p style="${style}">${(n ? inline(part, n) : escape(part)).replace(/\n/g, '<br>')}</p>`)
|
|
.join('')
|
|
|
|
const TEXT = `margin:12px 0 0;font-family:${BODY_FONT};font-size:15px;line-height:24px;color:${COLOR.body};`
|
|
const buttonHtml = (href: string, label: string) =>
|
|
`<a href="${escape(href)}" style="display:inline-block;margin:4px 8px 4px 0;padding:10px 20px;border-radius:8px;background:${COLOR.teal};color:#ffffff;font-family:${FONT};font-size:14px;font-weight:700;text-decoration:none;">${escape(label)}</a>`
|
|
|
|
function blockHtml(block: NewsletterBlock, n: Newsletter): string {
|
|
const text = block.text?.trim() ?? ''
|
|
const href = block.href ? safeHref(block.href, n) : null
|
|
const items = block.items ?? []
|
|
switch (block.type) {
|
|
case 'heading':
|
|
return text
|
|
? `<h2 style="margin:24px 0 0;font-family:${FONT};font-size:22px;line-height:28px;font-weight:700;color:${COLOR.teal};">${escape(text)}</h2>`
|
|
: ''
|
|
case 'subheading':
|
|
return text
|
|
? `<h3 style="margin:20px 0 0;font-family:${FONT};font-size:18px;line-height:24px;font-weight:700;color:${COLOR.ink};">${escape(text)}</h3>`
|
|
: ''
|
|
case 'paragraph':
|
|
return text ? paragraphs(text, TEXT, n) : ''
|
|
case 'callout':
|
|
return text ? `<div style="margin:16px 0 0;padding:4px 16px 16px;background:${COLOR.rule};">${paragraphs(text, TEXT, n)}</div>` : ''
|
|
case 'quote':
|
|
return text
|
|
? `<blockquote style="margin:16px 0 0;padding:0 0 0 16px;border-left:4px solid ${COLOR.teal};">${paragraphs(text, `${TEXT}font-style:italic;`, n)}${
|
|
block.cite ? `<p style="${TEXT}color:${COLOR.muted};">${escape(block.cite)}</p>` : ''
|
|
}</blockquote>`
|
|
: ''
|
|
case 'list':
|
|
return items.length
|
|
? `<ul style="margin:12px 0 0;padding-left:20px;font-family:${BODY_FONT};font-size:15px;line-height:24px;color:${COLOR.body};">${items
|
|
.map((item) => {
|
|
const url = item.url ? safeHref(item.url, n) : null
|
|
const label = escape(item.text ?? '')
|
|
const detail = item.detail ? `: ${escape(item.detail)}` : ''
|
|
return `<li>${url ? `<a href="${escape(url)}" style="color:${COLOR.teal};">${label}</a>` : label}${detail}</li>`
|
|
})
|
|
.join('')}</ul>`
|
|
: ''
|
|
case 'links': {
|
|
const buttons = items
|
|
.map((item) => (item.url ? safeHref(item.url, n) : null))
|
|
.map((url, i) => (url ? buttonHtml(url, items[i].text || url) : ''))
|
|
.join('')
|
|
return buttons ? `<p style="margin:16px 0 0;">${buttons}</p>` : ''
|
|
}
|
|
case 'button':
|
|
return href && text ? `<p style="margin:16px 0 0;">${buttonHtml(href, text)}</p>` : ''
|
|
case 'image':
|
|
case 'video': {
|
|
if (!block.media) return ''
|
|
const img = `<img src="${escape(n.absolute(block.media, 'image'))}" width="552" alt="${escape(block.alt ?? '')}" style="display:block;width:100%;max-width:552px;height:auto;border:0;">`
|
|
const caption = text
|
|
? `<p style="margin:6px 0 0;font-family:${BODY_FONT};font-size:13px;line-height:20px;color:${COLOR.muted};">${escape(text)}</p>`
|
|
: ''
|
|
return `<div style="margin:16px 0 0;">${href ? `<a href="${escape(href)}">${img}</a>` : img}${caption}</div>`
|
|
}
|
|
case 'cards':
|
|
return items
|
|
.map((item) => {
|
|
const url = item.url ? safeHref(item.url, n) : null
|
|
if (!url || !item.text) return ''
|
|
return `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin:16px 0 0;background:${COLOR.card};border-left:4px solid ${COLOR.teal};"><tr><td style="padding:16px 20px;">
|
|
<h3 style="margin:0;font-family:${FONT};font-size:18px;line-height:24px;font-weight:700;"><a href="${escape(url)}" style="color:${COLOR.ink};text-decoration:none;">${escape(item.text)}</a></h3>
|
|
${item.detail ? paragraphs(item.detail, TEXT) : ''}</td></tr></table>`
|
|
})
|
|
.join('')
|
|
case 'divider':
|
|
return `<hr style="margin:24px 0 0;border:0;border-top:2px solid ${COLOR.rule};">`
|
|
default:
|
|
return ''
|
|
}
|
|
}
|
|
|
|
function blockText(block: NewsletterBlock, n: Newsletter): string[] {
|
|
const text = block.text?.trim() ?? ''
|
|
const items = block.items ?? []
|
|
const link = (url: string) => safeHref(url, n) ?? url
|
|
switch (block.type) {
|
|
case 'heading':
|
|
return text ? [text.toUpperCase(), ''] : []
|
|
case 'subheading':
|
|
case 'paragraph':
|
|
case 'callout':
|
|
case 'image':
|
|
return text ? [text, ''] : []
|
|
case 'quote':
|
|
return text ? [`"${text}"`, ...(block.cite ? [` ${block.cite}`] : []), ''] : []
|
|
case 'list':
|
|
return items.length
|
|
? [...items.map((item) => `- ${item.text ?? ''}${item.detail ? `: ${item.detail}` : ''}${item.url ? ` (${link(item.url)})` : ''}`), '']
|
|
: []
|
|
case 'links':
|
|
case 'cards': {
|
|
const lines = items.filter((item) => item.url).map((item) => `${item.text ?? ''}: ${link(item.url!)}`)
|
|
return lines.length ? [...lines, ''] : []
|
|
}
|
|
case 'button':
|
|
case 'video':
|
|
return block.href ? [`${text || 'Link'}: ${link(block.href)}`, ''] : []
|
|
case 'divider':
|
|
return ['---', '']
|
|
default:
|
|
return []
|
|
}
|
|
}
|
|
|
|
/* The closing lines: the admin's, or the site's name and address. */
|
|
const footerHtml = (n: Newsletter) =>
|
|
n.footer?.trim()
|
|
? paragraphs(n.footer, `margin:0 0 8px;font-family:${BODY_FONT};font-size:13px;line-height:20px;color:${COLOR.muted};`, n)
|
|
: `${escape(n.siteName)}<br><a href="${escape(n.origin)}" style="color:${COLOR.teal};">${escape(n.origin.replace(/^https?:\/\//, ''))}</a>`
|
|
|
|
function itemHtml(item: NewsletterItem): string {
|
|
const image = item.image
|
|
? `<tr><td><a href="${escape(item.href)}"><img src="${escape(item.image)}" width="552" alt="" style="display:block;width:100%;max-width:552px;height:auto;border:0;"></a></td></tr>`
|
|
: ''
|
|
const meta = item.meta
|
|
? `<p style="margin:4px 0 0;font-family:${BODY_FONT};font-size:14px;line-height:20px;color:${COLOR.muted};">${escape(item.meta)}</p>`
|
|
: ''
|
|
const text = item.text
|
|
? paragraphs(item.text, `margin:12px 0 0;font-family:${BODY_FONT};font-size:15px;line-height:24px;color:${COLOR.body};`)
|
|
: ''
|
|
return `
|
|
<tr><td style="padding:0 24px 24px;">
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:${COLOR.card};border-left:4px solid ${COLOR.teal};">
|
|
${image}
|
|
<tr><td style="padding:20px 24px 24px;">
|
|
<h3 style="margin:0;font-family:${FONT};font-size:20px;line-height:26px;font-weight:700;color:${COLOR.ink};"><a href="${escape(item.href)}" style="color:${COLOR.ink};text-decoration:none;">${escape(item.title)}</a></h3>
|
|
${meta}${text}
|
|
<p style="margin:16px 0 0;"><a href="${escape(item.href)}" style="display:inline-block;padding:10px 20px;border-radius:8px;background:${COLOR.teal};color:#ffffff;font-family:${FONT};font-size:14px;font-weight:700;text-decoration:none;">${escape(item.cta)}</a></p>
|
|
</td></tr>
|
|
</table>
|
|
</td></tr>`
|
|
}
|
|
|
|
export function newsletterHtml(n: Newsletter): string {
|
|
const sections = n.sections
|
|
.filter((section) => section.items.length > 0)
|
|
.map(
|
|
(section) => `
|
|
<tr><td style="padding:16px 24px 12px;">
|
|
<h2 style="margin:0;padding-bottom:8px;border-bottom:2px solid ${COLOR.rule};font-family:${FONT};font-size:22px;line-height:28px;font-weight:700;color:${COLOR.teal};">${escape(section.heading)}</h2>
|
|
</td></tr>${section.items.map(itemHtml).join('')}`,
|
|
)
|
|
.join('')
|
|
|
|
const logo = n.logo
|
|
? `<a href="${escape(n.origin)}"><img src="${escape(n.logo)}" height="48" alt="${escape(n.siteName)}" style="display:block;height:48px;width:auto;border:0;"></a>`
|
|
: `<a href="${escape(n.origin)}" style="font-family:${FONT};font-size:18px;font-weight:700;color:#ffffff;text-decoration:none;">${escape(n.siteName)}</a>`
|
|
|
|
return `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>${escape(n.title)}</title>
|
|
</head>
|
|
<body style="margin:0;padding:0;background:${COLOR.page};">
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:${COLOR.page};">
|
|
<tr><td align="center" style="padding:24px 12px;">
|
|
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="width:100%;max-width:600px;background:${COLOR.page};">
|
|
<tr><td style="padding:20px 24px;background:${COLOR.body};">${logo}</td></tr>
|
|
<tr><td style="padding:28px 24px 8px;">
|
|
<h1 style="margin:0;font-family:${FONT};font-size:28px;line-height:34px;font-weight:700;color:${COLOR.ink};">${escape(n.title)}</h1>
|
|
${n.intro ? paragraphs(n.intro, `margin:12px 0 0;font-family:${BODY_FONT};font-size:16px;line-height:26px;color:${COLOR.body};`, n) : ''}
|
|
${(n.blocks ?? []).map((block) => blockHtml(block, n)).join('')}
|
|
</td></tr>
|
|
${sections}
|
|
<tr><td style="padding:24px;border-top:2px solid ${COLOR.rule};font-family:${BODY_FONT};font-size:13px;line-height:20px;color:${COLOR.muted};">
|
|
${footerHtml(n)}
|
|
</td></tr>
|
|
</table>
|
|
</td></tr>
|
|
</table>
|
|
</body>
|
|
</html>`
|
|
}
|
|
|
|
export function newsletterText(n: Newsletter): string {
|
|
const lines = [n.title, '='.repeat(Math.min(n.title.length, 60)), '']
|
|
if (n.intro) lines.push(n.intro.trim(), '')
|
|
for (const block of n.blocks ?? []) lines.push(...blockText(block, n))
|
|
for (const section of n.sections) {
|
|
if (section.items.length === 0) continue
|
|
lines.push(section.heading.toUpperCase(), '')
|
|
for (const item of section.items) {
|
|
lines.push(item.title)
|
|
if (item.meta) lines.push(item.meta)
|
|
if (item.text) lines.push(item.text.trim())
|
|
lines.push(`${item.cta}: ${item.href}`, '')
|
|
}
|
|
}
|
|
lines.push('--', ...(n.footer?.trim() ? [n.footer.trim()] : [n.siteName, n.origin]))
|
|
return lines.join('\n')
|
|
}
|