A newsletter builder (Tools → Newsletter)
Tick upcoming events, published blog posts and the front page's featured cards, write a title and intro, and copy an email-ready newsletter (table layout, inline styles, the palette in hex, a plain-text copy alongside) into Mailchimp, Gmail or Church Center, or download it. Picked posts are read from their public pages, so only live posts make it in; links and images point at the live site. Nothing is sent or stored. The builder is src/lib/newsletter.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
177a65005e
commit
2a926971f7
4 changed files with 446 additions and 0 deletions
147
src/lib/newsletter.ts
Normal file
147
src/lib/newsletter.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
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.
|
||||
|
||||
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[] }
|
||||
|
||||
export type Newsletter = {
|
||||
title: string
|
||||
intro?: string | null
|
||||
sections: NewsletterSection[]
|
||||
/** 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
|
||||
}
|
||||
|
||||
/* 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, '"')
|
||||
|
||||
/* Paragraphs from text with blank lines between them. */
|
||||
const paragraphs = (text: string, style: string) =>
|
||||
text
|
||||
.split(/\n\s*\n/)
|
||||
.map((part) => part.trim())
|
||||
.filter(Boolean)
|
||||
.map((part) => `<p style="${style}">${escape(part).replace(/\n/g, '<br>')}</p>`)
|
||||
.join('')
|
||||
|
||||
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};`) : ''}
|
||||
</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>
|
||||
</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 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.siteName, n.origin)
|
||||
return lines.join('\n')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue