/* ═══════════════════════════════════════════════════════════════ 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, '"') /* 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 ? `${label}` : label }) .replace(/\*\*([^*]+)\*\*/g, '$1') .replace(/\*([^*]+)\*/g, '$1') } /* 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) => `
${(n ? inline(part, n) : escape(part)).replace(/\n/g, '
')}
${paragraphs(text, `${TEXT}font-style:italic;`, n)}${ block.cite ? `` : '' case 'list': return items.length ? `${escape(block.cite)}
` : '' }
${buttons}
` : '' } case 'button': return href && text ? `${buttonHtml(href, text)}
` : '' case 'image': case 'video': { if (!block.media) return '' const img = `${escape(text)}
` : '' return `
${escape(item.text)}${item.detail ? paragraphs(item.detail, TEXT) : ''} |
${escape(item.meta)}
` : '' 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 `
${escape(item.title)}${meta}${text} |
|