Newsletter: every part editable and movable

A draft is one ordered list of parts (migration 060, newsletters.layout
as JSON), edited with the content block editor so any part can be
moved, copied or removed, each aligned left or center:
- header: words, a PNG/JPEG logo, band color or none
- title
- any email block; dividers take color, thickness and spacing
- picked items: events, posts or front page cards picked in order,
  with the heading, its underline, the card bar and the button label
- footer: its lines and the line above

Colors come from the palette or none. 057's fixed fields and blocks
are dropped; that migration never reached a live database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-01 19:37:26 -05:00
parent 222c9e69a2
commit 3194fdbb7d
5 changed files with 561 additions and 397 deletions

View file

@ -1,18 +1,27 @@
/* ═══════════════════════════════════════════════════════════════
NEWSLETTER: email-ready HTML and plain text from picked items
NEWSLETTER: email-ready HTML and plain text from a list of parts
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.
The admin's Newsletter tool (AdminNewsletter.tsx) edits a draft as
one ordered list of parts (060_newsletter_layout.sql); this turns
it into one email to paste into Mailchimp, Gmail or Church Center.
Nothing is sent from here. The parts, each aligned left or center:
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.
header the band above the title: text, a logo, its color
(or no band); left out when the part is removed
title the email's heading
picks a section of picked events, blog posts or front page
cards: its heading and underline, each card's left
bar, and the button label
footer the closing lines (blank: the site's name and
address) and the rule above them
any block drawn as its email-safe equivalent: headings,
paragraphs (with **bold**, *italic* and [links](url)),
callouts, quotes, lists, link and button rows, images,
cards and dividers (with their color, thickness and
spacing); a video is its still linked to the video
No unsubscribe link: Mailchimp adds its own to every send.
Colors are the site's palette (PALETTE) or "none"; blank is the
default. 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
@ -21,6 +30,8 @@
plain-text copy goes alongside for clients that show no HTML.
═══════════════════════════════════════════════════════════════ */
import { readableOn } from './color.ts'
export type NewsletterItem = {
title: string
/** A line under the title: dates and place, a byline. */
@ -33,32 +44,41 @@ export type NewsletterItem = {
cta: string
}
export type NewsletterSection = { heading: string; items: NewsletterItem[] }
export type PickSource = 'events' | 'posts' | 'cards'
/* A content block as the block editor holds it. */
export type NewsletterBlock = {
/* One part of the email, as the builder's editor holds it. Blocks
use the content block fields; the other parts add their own. */
export type NewsletterPart = {
type: string
align?: 'left' | 'center' | '' | null
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 }>
/** The header's band, a divider's line. */
color?: string | null
/** A divider's thickness and the space above it, in pixels. */
size?: string | number | null
space?: string | number | null
/** A picks section: what it picks from, its picks in order, its button label. */
source?: PickSource | '' | null
picked?: Array<{ ref?: string | null }>
cta?: string | null
/** A picks section's underline, the footer's rule. */
rule?: string | null
/** The bar down the left of a picks section's cards. */
bar?: 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
parts: NewsletterPart[]
/** A picks part's items, ready to draw (picked, published, in order). */
itemsFor: (part: NewsletterPart) => NewsletterItem[]
/** 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
}
@ -74,12 +94,38 @@ const COLOR = {
rule: '#d6f5fa',
}
/* What each picks section is called and says on its buttons, unless
the part says otherwise. */
export const PICK_DEFAULTS: Record<PickSource, { heading: string; cta: string }> = {
events: { heading: 'Coming up', cta: 'Event details' },
posts: { heading: 'From the blog', cta: 'Read the post' },
cards: { heading: 'Featured', cta: 'Learn more' },
}
export const DEFAULT_TITLE = (siteName: string) => `News from ${siteName}`
const FONT = "'Poppins', Helvetica, Arial, sans-serif"
const BODY_FONT = "'Montserrat', Helvetica, Arial, sans-serif"
const escape = (value: string) =>
value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
/* A part's color: blank is `fallback`, "none" is no color at all,
and anything but a hex color (it lands in a style attribute) is
the fallback too. */
function colorOf(value: string | null | undefined, fallback: string): string | null {
if (value === 'none') return null
return value && /^#[0-9a-f]{6}$/i.test(value) ? value : fallback
}
/* A whole number of pixels within bounds, or the fallback. */
function px(value: string | number | null | undefined, fallback: number, min: number, max: number): number {
const n = Math.round(Number(value))
return value === '' || value == null || !Number.isFinite(n) ? fallback : Math.min(max, Math.max(min, n))
}
const alignOf = (part: NewsletterPart) => (part.align === 'center' ? 'center' : 'left')
/* 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 {
@ -111,13 +157,18 @@ const paragraphs = (text: string, style: string, n?: Newsletter) =>
.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>`
const buttonHtml = (href: string, label: string, align: string) =>
`<a href="${escape(href)}" style="display:inline-block;margin:${align === 'center' ? '4px' : '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 {
/* A full-width picture; centered by its row when it's narrower. */
const imageTag = (src: string, alt: string) =>
`<img src="${escape(src)}" width="552" alt="${escape(alt)}" style="display:block;margin:0 auto;width:100%;max-width:552px;height:auto;border:0;">`
function blockHtml(block: NewsletterPart, n: Newsletter): string {
const text = block.text?.trim() ?? ''
const href = block.href ? safeHref(block.href, n) : null
const items = block.items ?? []
const align = alignOf(block)
switch (block.type) {
case 'heading':
return text
@ -139,7 +190,7 @@ function blockHtml(block: NewsletterBlock, n: Newsletter): string {
: ''
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
? `<ul style="margin:12px 0 0;padding-left:${align === 'center' ? '0' : '20px'};list-style-position:${align === 'center' ? 'inside' : 'outside'};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 ?? '')
@ -151,16 +202,16 @@ function blockHtml(block: NewsletterBlock, n: Newsletter): string {
case 'links': {
const buttons = items
.map((item) => (item.url ? safeHref(item.url, n) : null))
.map((url, i) => (url ? buttonHtml(url, items[i].text || url) : ''))
.map((url, i) => (url ? buttonHtml(url, items[i].text || url, align) : ''))
.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>` : ''
return href && text ? `<p style="margin:16px 0 0;">${buttonHtml(href, text, align)}</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 img = imageTag(n.absolute(block.media, 'image'), block.alt ?? '')
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>`
: ''
@ -171,19 +222,124 @@ function blockHtml(block: NewsletterBlock, n: Newsletter): string {
.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;">
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;text-align:${align};">
<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};">`
case 'divider': {
const color = colorOf(block.color, COLOR.rule)
// "none" keeps the space without a line.
const line = color ? `border-top:${px(block.size, 2, 1, 8)}px solid ${color};` : ''
return `<hr style="margin:${px(block.space, 24, 0, 64)}px 0 0;border:0;${line}">`
}
default:
return ''
}
}
function blockText(block: NewsletterBlock, n: Newsletter): string[] {
function headerHtml(part: NewsletterPart, n: Newsletter): string {
const band = colorOf(part.color, COLOR.body)
const ink = band ? readableOn(band) : COLOR.ink
const align = alignOf(part)
const logo = part.media
? `<a href="${escape(n.origin)}"><img src="${escape(n.absolute(part.media, 'image'))}" height="48" alt="${escape(part.text?.trim() || n.siteName)}" style="display:${align === 'center' ? 'block;margin:0 auto' : 'inline-block'};height:48px;width:auto;border:0;"></a>`
: ''
// With a logo, words only when written; without one, the site's name.
const words = part.text?.trim() || (part.media ? '' : n.siteName)
const name = words
? `<a href="${escape(n.origin)}" style="font-family:${FONT};font-size:18px;font-weight:700;color:${ink};text-decoration:none;">${escape(words)}</a>`
: ''
return `<tr><td style="padding:20px 24px;text-align:${align};${band ? `background:${band};` : ''}">${logo}${logo && name ? `<div style="margin-top:8px;">${name}</div>` : name}</td></tr>`
}
function itemHtml(item: NewsletterItem, part: NewsletterPart, align: string): string {
const bar = colorOf(part.bar, COLOR.teal)
const image = item.image
? `<tr><td><a href="${escape(item.href)}">${imageTag(item.image, '')}</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, TEXT) : ''
return `
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin:16px 0 0;background:${COLOR.card};${bar ? `border-left:4px solid ${bar};` : ''}">
${image}
<tr><td style="padding:20px 24px 24px;text-align:${align};">
<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;">${buttonHtml(item.href, item.cta, align)}</p>
</td></tr>
</table>`
}
function picksHtml(part: NewsletterPart, n: Newsletter): string {
const items = n.itemsFor(part)
if (items.length === 0) return ''
const source = (part.source || 'events') as PickSource
const heading = part.text?.trim() || PICK_DEFAULTS[source].heading
const rule = colorOf(part.rule, COLOR.rule)
const align = alignOf(part)
return `<tr><td style="padding:24px 24px 0;text-align:${align};">
<h2 style="margin:0;padding-bottom:8px;${rule ? `border-bottom:2px solid ${rule};` : ''}font-family:${FONT};font-size:22px;line-height:28px;font-weight:700;color:${COLOR.teal};">${escape(heading)}</h2>
${items.map((item) => itemHtml(item, part, align)).join('')}
</td></tr>`
}
function footerHtml(part: NewsletterPart, n: Newsletter): string {
const rule = colorOf(part.rule, COLOR.rule)
const lines = part.text?.trim()
? paragraphs(part.text, `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>`
return `<tr><td style="padding:24px;text-align:${alignOf(part)};"><div style="padding-top:24px;${rule ? `border-top:2px solid ${rule};` : ''}font-family:${BODY_FONT};font-size:13px;line-height:20px;color:${COLOR.muted};">
${lines}
</div></td></tr>`
}
function partHtml(part: NewsletterPart, n: Newsletter): string {
switch (part.type) {
case 'header':
return headerHtml(part, n)
case 'title':
return `<tr><td style="padding:28px 24px 0;text-align:${alignOf(part)};"><h1 style="margin:0;font-family:${FONT};font-size:28px;line-height:34px;font-weight:700;color:${COLOR.ink};">${escape(part.text?.trim() || DEFAULT_TITLE(n.siteName))}</h1></td></tr>`
case 'picks':
return picksHtml(part, n)
case 'footer':
return footerHtml(part, n)
default: {
const html = blockHtml(part, n)
return html ? `<tr><td style="padding:0 24px;text-align:${alignOf(part)};">${html}</td></tr>` : ''
}
}
}
/* The email's title, for the HTML's <title> and the drafts list. */
export function newsletterTitle(n: Pick<Newsletter, 'parts' | 'siteName'>): string {
return n.parts.find((part) => part.type === 'title')?.text?.trim() || DEFAULT_TITLE(n.siteName)
}
export function newsletterHtml(n: Newsletter): string {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${escape(newsletterTitle(n))}</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};">
${n.parts.map((part) => partHtml(part, n)).join('\n ')}
<tr><td style="padding:0 0 24px;"></td></tr>
</table>
</td></tr>
</table>
</body>
</html>`
}
function blockText(block: NewsletterPart, n: Newsletter): string[] {
const text = block.text?.trim() ?? ''
const items = block.items ?? []
const link = (url: string) => safeHref(url, n) ?? url
@ -216,92 +372,34 @@ function blockText(block: NewsletterBlock, n: Newsletter): string[] {
}
}
/* 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>`
function partText(part: NewsletterPart, n: Newsletter): string[] {
switch (part.type) {
case 'header':
return [part.text?.trim() || n.siteName, '']
case 'title': {
const title = part.text?.trim() || DEFAULT_TITLE(n.siteName)
return [title, '='.repeat(Math.min(title.length, 60)), '']
}
case 'picks': {
const items = n.itemsFor(part)
if (items.length === 0) return []
const source = (part.source || 'events') as PickSource
const lines = [(part.text?.trim() || PICK_DEFAULTS[source].heading).toUpperCase(), '']
for (const item of 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}`, '')
}
return lines
}
case 'footer':
return ['--', ...(part.text?.trim() ? [part.text.trim()] : [n.siteName, n.origin]), '']
default:
return blockText(part, n)
}
}
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')
return n.parts.flatMap((part) => partText(part, n)).join('\n').trim()
}