Newsletter drafts with content blocks, a footer and Post as blog post; Home has no header dropdown
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>
This commit is contained in:
parent
9daf7b1a4f
commit
e8ac242e6d
8 changed files with 575 additions and 34 deletions
|
|
@ -2,9 +2,17 @@
|
|||
NEWSLETTER: email-ready HTML and plain text from picked items
|
||||
|
||||
The admin's Newsletter tool (AdminNewsletter.tsx) gathers events,
|
||||
blog posts and featured cards; this turns them into one email to
|
||||
paste into Mailchimp, Gmail or Church Center. Nothing is sent or
|
||||
stored here.
|
||||
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
|
||||
|
|
@ -27,15 +35,32 @@ export type NewsletterItem = {
|
|||
|
||||
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. */
|
||||
|
|
@ -55,15 +80,148 @@ const BODY_FONT = "'Montserrat', Helvetica, Arial, sans-serif"
|
|||
const escape = (value: string) =>
|
||||
value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
|
||||
|
||||
/* Paragraphs from text with blank lines between them. */
|
||||
const paragraphs = (text: string, style: string) =>
|
||||
/* 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}">${escape(part).replace(/\n/g, '<br>')}</p>`)
|
||||
.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>`
|
||||
|
|
@ -116,11 +274,12 @@ export function newsletterHtml(n: Newsletter): string {
|
|||
<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.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};">
|
||||
${escape(n.siteName)}<br><a href="${escape(n.origin)}" style="color:${COLOR.teal};">${escape(n.origin.replace(/^https?:\/\//, ''))}</a>
|
||||
${footerHtml(n)}
|
||||
</td></tr>
|
||||
</table>
|
||||
</td></tr>
|
||||
|
|
@ -132,6 +291,7 @@ export function newsletterHtml(n: Newsletter): string {
|
|||
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(), '')
|
||||
|
|
@ -142,6 +302,6 @@ export function newsletterText(n: Newsletter): string {
|
|||
lines.push(`${item.cta}: ${item.href}`, '')
|
||||
}
|
||||
}
|
||||
lines.push('--', n.siteName, n.origin)
|
||||
lines.push('--', ...(n.footer?.trim() ? [n.footer.trim()] : [n.siteName, n.origin]))
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue