diff --git a/src/templates/newsletter/index.ts b/src/templates/newsletter/index.ts index 6dafabf..7df6b4b 100644 --- a/src/templates/newsletter/index.ts +++ b/src/templates/newsletter/index.ts @@ -11,3 +11,4 @@ export { default as newsletterSponsor } from './sponsor'; export { default as newsletterPoll } from './poll'; export { default as newsletterWelcome } from './welcome'; export { default as newsletterMembersOnly } from './members-only'; +export { default as newsletterMailbag } from './mailbag'; diff --git a/src/templates/newsletter/mailbag.ts b/src/templates/newsletter/mailbag.ts new file mode 100644 index 0000000..9816cb6 --- /dev/null +++ b/src/templates/newsletter/mailbag.ts @@ -0,0 +1,364 @@ +import type { SatoriElement, TemplateDefinition } from '../types'; +import { autoFontSize, commonFields, isLightColor, mixColors, truncate } from '../utils'; + +const cleanText = (value: unknown, maxLength: number) => + truncate(String(value ?? '').replace(/\s+/g, ' ').trim(), maxLength); + +const template: TemplateDefinition = { + id: 'newsletter-mailbag', + name: 'Mailbag', + category: 'newsletter', + description: 'Newsletter mailbag issue with reader questions and who asked them', + tags: ['newsletter', 'mailbag', 'questions', 'readers', 'qa', 'email'], + fields: [ + { ...commonFields.siteName, defaultValue: 'The Friday Diff' }, + { + key: 'issue', + label: 'Issue', + type: 'text', + defaultValue: 'Issue 58', + placeholder: 'Issue 58', + group: 'Content', + }, + { + ...commonFields.title, + defaultValue: 'Your questions about going freelance', + placeholder: 'The issue title...', + }, + { + key: 'question1', + label: 'Question 1', + type: 'text', + defaultValue: 'How did you find your first client?', + placeholder: 'Question 1', + group: 'Content', + }, + { + key: 'asker1', + label: 'Asker 1', + type: 'text', + defaultValue: 'Sam, Lisbon', + placeholder: 'Asker 1', + group: 'Content', + }, + { + key: 'question2', + label: 'Question 2', + type: 'text', + defaultValue: 'What do you charge for a small project?', + placeholder: 'Question 2', + group: 'Content', + }, + { + key: 'asker2', + label: 'Asker 2', + type: 'text', + defaultValue: 'Priya', + placeholder: 'Asker 2', + group: 'Content', + }, + { + key: 'question3', + label: 'Question 3', + type: 'text', + defaultValue: 'Do you still take full-time offers?', + placeholder: 'Question 3', + group: 'Content', + }, + { + key: 'asker3', + label: 'Asker 3', + type: 'text', + defaultValue: 'A reader in Toronto', + placeholder: 'Asker 3', + group: 'Content', + }, + commonFields.bgColor, + commonFields.accentColor, + commonFields.textColor, + ], + defaults: { + siteName: 'The Friday Diff', + issue: 'Issue 58', + title: 'Your questions about going freelance', + question1: 'How did you find your first client?', + asker1: 'Sam, Lisbon', + question2: 'What do you charge for a small project?', + asker2: 'Priya', + question3: 'Do you still take full-time offers?', + asker3: 'A reader in Toronto', + bgColor: '#0a0a0a', + accentColor: '#E07A5F', + textColor: '#FFFFFF', + }, + render: (params) => { + const rawTitle = cleanText(params.title, 140); + const titleSize = rawTitle + ? autoFontSize(rawTitle, [ + { maxLen: 40, size: 46 }, + { maxLen: 65, size: 38 }, + { maxLen: 95, size: 32 }, + { maxLen: 140, size: 28 }, + ]) + : 40; + const title = rawTitle; + + const siteName = cleanText(params.siteName, 42); + const issue = cleanText(params.issue, 24); + + const rawQuestions = [ + { q: params.question1, a: params.asker1 }, + { q: params.question2, a: params.asker2 }, + { q: params.question3, a: params.asker3 }, + ]; + + const questions = rawQuestions + .map(({ q, a }) => ({ + question: cleanText(q, 120), + asker: cleanText(a, 40), + })) + .filter((item) => Boolean(item.question)); + + const bgColor = String(params.bgColor || '#0a0a0a'); + const accentColor = String(params.accentColor || '#E07A5F'); + const textColor = String(params.textColor || '#FFFFFF'); + const lightBg = isLightColor(bgColor); + const accentInk = lightBg ? mixColors(accentColor, '#000000', 0.45) : accentColor; + const mainColor = lightBg && isLightColor(textColor) ? '#111111' : textColor; + const mutedColor = mixColors(bgColor, mainColor, 0.65); + const rowBackground = mixColors(bgColor, mainColor, lightBg ? 0.04 : 0.07); + const rowBorder = mixColors(bgColor, mainColor, lightBg ? 0.15 : 0.18); + + const headerItems: SatoriElement[] = [ + ...(siteName + ? [ + { + type: 'span', + props: { + style: { + fontSize: '21px', + fontWeight: 700, + color: mainColor, + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', + }, + children: siteName, + }, + }, + ] + : []), + ...(issue + ? [ + { + type: 'span', + props: { + style: { + fontSize: '17px', + fontWeight: 600, + color: mutedColor, + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', + }, + children: issue, + }, + }, + ] + : []), + ]; + + const questionRows: SatoriElement[] = questions.map((item) => ({ + type: 'div', + props: { + style: { + display: 'flex', + alignItems: 'center', + boxSizing: 'border-box', + width: '100%', + padding: '16px 20px', + marginBottom: '14px', + borderRadius: '14px', + border: `1px solid ${rowBorder}`, + backgroundColor: rowBackground, + }, + children: [ + // Q circle + { + type: 'div', + props: { + style: { + display: 'flex', + alignItems: 'center', + justifyContent: 'center', + flexShrink: 0, + width: '32px', + height: '32px', + borderRadius: '50%', + border: `2px solid ${accentInk}`, + color: accentInk, + fontSize: '15px', + fontWeight: 700, + marginRight: '16px', + }, + children: 'Q', + }, + }, + // Question text + { + type: 'span', + props: { + style: { + display: 'flex', + flex: 1, + minWidth: 0, + fontSize: '20px', + fontWeight: 600, + lineHeight: 1.25, + color: mainColor, + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', + }, + children: item.question, + }, + }, + // Asker (if present) + ...(item.asker + ? [ + { + type: 'span', + props: { + style: { + display: 'flex', + flexShrink: 0, + maxWidth: '320px', + marginLeft: '24px', + fontSize: '16px', + fontWeight: 500, + color: mutedColor, + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', + }, + children: item.asker, + }, + }, + ] + : []), + ], + }, + })); + + return { + type: 'div', + props: { + style: { + display: 'flex', + flexDirection: 'column', + boxSizing: 'border-box', + width: '1200px', + height: '630px', + padding: '54px 64px', + backgroundColor: bgColor, + fontFamily: 'Inter, sans-serif', + }, + children: [ + // Header row (siteName and issue) + ...(headerItems.length + ? [ + { + type: 'div', + props: { + style: { + display: 'flex', + alignItems: 'center', + justifyContent: + siteName && issue + ? 'space-between' + : siteName + ? 'flex-start' + : 'flex-end', + width: '100%', + marginBottom: '24px', + }, + children: headerItems, + }, + }, + ] + : []), + // Mailbag pill + { + type: 'div', + props: { + style: { + display: 'flex', + alignItems: 'center', + alignSelf: 'flex-start', + padding: '7px 16px', + marginBottom: '18px', + borderRadius: '999px', + border: `1px solid ${accentInk}55`, + backgroundColor: mixColors(bgColor, accentInk, lightBg ? 0.08 : 0.16), + }, + children: { + type: 'span', + props: { + style: { + fontSize: '13px', + fontWeight: 700, + color: accentInk, + letterSpacing: '0.08em', + textTransform: 'uppercase', + }, + children: 'Mailbag', + }, + }, + }, + }, + // Title + ...(title + ? [ + { + type: 'h1', + props: { + style: { + display: 'flex', + alignItems: 'center', + margin: '0 0 24px 0', + fontSize: `${titleSize}px`, + fontWeight: 700, + lineHeight: 1.2, + color: mainColor, + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', + }, + children: title, + }, + }, + ] + : []), + // Question rows + ...(questionRows.length + ? [ + { + type: 'div', + props: { + style: { + display: 'flex', + flexDirection: 'column', + width: '100%', + }, + children: questionRows, + }, + }, + ] + : []), + ], + }, + }; + }, +}; + +export default template; diff --git a/src/templates/registry.ts b/src/templates/registry.ts index 775ecca..c7cfe06 100644 --- a/src/templates/registry.ts +++ b/src/templates/registry.ts @@ -258,12 +258,13 @@ import newsletterSponsor from './newsletter/sponsor'; import newsletterPoll from './newsletter/poll'; import newsletterWelcome from './newsletter/welcome'; import newsletterMembersOnly from './newsletter/members-only'; +import newsletterMailbag from './newsletter/mailbag'; [ newsletterIssueCard, newsletterDigestBanner, newsletterPreviewText, newsletterEditorial, newsletterCuratedLinks, newsletterMinimalNumber, newsletterNewspaperFold, newsletterSubscriberCount, newsletterSubscribeCard, newsletterSponsor, newsletterPoll, - newsletterWelcome, newsletterMembersOnly, + newsletterWelcome, newsletterMembersOnly, newsletterMailbag, ].forEach(registerTemplate); // Quote diff --git a/tests/templates/mailbag.test.ts b/tests/templates/mailbag.test.ts new file mode 100644 index 0000000..d365cd2 --- /dev/null +++ b/tests/templates/mailbag.test.ts @@ -0,0 +1,234 @@ +import { describe, expect, it } from 'vitest'; +import newsletterMailbag from '@/templates/newsletter/mailbag'; +import { newsletterMailbag as newsletterMailbagExport } from '@/templates/newsletter'; +import { getTemplate, getTemplatesByCategory } from '@/templates/registry'; +import type { SatoriElement } from '@/templates/types'; + +function walk(node: unknown, visit: (element: SatoriElement) => void): void { + if (Array.isArray(node)) { + node.forEach((child) => walk(child, visit)); + return; + } + if (!node || typeof node !== 'object' || !('type' in node)) return; + + const element = node as SatoriElement; + visit(element); + walk(element.props.children, visit); +} + +function textContent(node: unknown): string[] { + if (typeof node === 'string') return [node]; + if (Array.isArray(node)) return node.flatMap(textContent); + if (node && typeof node === 'object' && 'props' in node) { + return textContent((node as SatoriElement).props.children); + } + return []; +} + +function allElements(node: unknown): SatoriElement[] { + const found: SatoriElement[] = []; + walk(node, (element) => found.push(element)); + return found; +} + +const tree = (overrides: Record = {}) => + newsletterMailbag.render({ ...newsletterMailbag.defaults, ...overrides }); + +describe('newsletter-mailbag', () => { + it('is re-exported and registered exactly once in the newsletter category', () => { + expect(newsletterMailbagExport).toBe(newsletterMailbag); + expect(getTemplate('newsletter-mailbag')).toBe(newsletterMailbag); + expect( + getTemplatesByCategory('newsletter').filter((t) => t.id === 'newsletter-mailbag'), + ).toHaveLength(1); + }); + + it('renders the defaults with site name, issue, Mailbag pill, title, and three question rows with askers', () => { + const rendered = tree(); + const texts = textContent(rendered); + + expect(texts).toContain('The Friday Diff'); + expect(texts).toContain('Issue 58'); + expect(texts).toContain('Mailbag'); + expect(texts).toContain('Your questions about going freelance'); + expect(texts).toContain('How did you find your first client?'); + expect(texts).toContain('Sam, Lisbon'); + expect(texts).toContain('What do you charge for a small project?'); + expect(texts).toContain('Priya'); + expect(texts).toContain('Do you still take full-time offers?'); + expect(texts).toContain('A reader in Toronto'); + + // Check for 3 'Q' circles + const qCircles = allElements(rendered).filter( + (el) => el.type === 'div' && el.props.children === 'Q', + ); + expect(qCircles).toHaveLength(3); + }); + + it('drops a question row completely when its question is blank, closing up remaining rows', () => { + const rendered = tree({ + question2: ' ', + asker2: 'Should Be Dropped', + }); + const texts = textContent(rendered); + + expect(texts).toContain('How did you find your first client?'); + expect(texts).toContain('Sam, Lisbon'); + expect(texts).not.toContain('What do you charge for a small project?'); + expect(texts).not.toContain('Should Be Dropped'); + expect(texts).toContain('Do you still take full-time offers?'); + expect(texts).toContain('A reader in Toronto'); + + // Only 2 Q circles remain + const qCircles = allElements(rendered).filter( + (el) => el.type === 'div' && el.props.children === 'Q', + ); + expect(qCircles).toHaveLength(2); + }); + + it('drops the question list completely when all three questions are blank', () => { + const rendered = tree({ + question1: '', + question2: '', + question3: '', + asker1: 'Asker 1', + asker2: 'Asker 2', + asker3: 'Asker 3', + }); + const texts = textContent(rendered); + + expect(texts).toContain('The Friday Diff'); + expect(texts).toContain('Issue 58'); + expect(texts).toContain('Mailbag'); + expect(texts).toContain('Your questions about going freelance'); + expect(texts).not.toContain('Asker 1'); + expect(texts).not.toContain('Asker 2'); + expect(texts).not.toContain('Asker 3'); + + // 0 Q circles remain + const qCircles = allElements(rendered).filter( + (el) => el.type === 'div' && el.props.children === 'Q', + ); + expect(qCircles).toHaveLength(0); + }); + + it('lets the question use the full row width when its asker is blank', () => { + const rendered = tree({ + asker1: '', + }); + const texts = textContent(rendered); + expect(texts).not.toContain('Sam, Lisbon'); + + // Find the row for question 1 + const rows = allElements(rendered).filter( + (el) => el.type === 'div' && el.props.style?.borderRadius === '14px', + ); + expect(rows).toHaveLength(3); + + // First row should have 2 children: [Q circle, questionSpan], no askerSpan + const row1Children = rows[0].props.children as SatoriElement[]; + expect(row1Children).toHaveLength(2); + expect(row1Children[1].props.style?.flex).toBe(1); + }); + + it('shrinks a long title with autoFontSize() and truncates on one line', () => { + const longTitle = 'An Exceptionally Long Newsletter Mailbag Edition Title About Launching Your Solo Consulting Practice'; + const shortTitle = 'Mailbag'; + + const longRendered = tree({ title: longTitle }); + const shortRendered = tree({ title: shortTitle }); + + const longH1 = allElements(longRendered).find((el) => el.type === 'h1'); + const shortH1 = allElements(shortRendered).find((el) => el.type === 'h1'); + + const longSize = parseInt(String(longH1?.props.style?.fontSize ?? '0'), 10); + const shortSize = parseInt(String(shortH1?.props.style?.fontSize ?? '0'), 10); + + expect(shortSize).toBeGreaterThan(longSize); + expect(longH1?.props.style?.overflow).toBe('hidden'); + expect(longH1?.props.style?.textOverflow).toBe('ellipsis'); + expect(longH1?.props.style?.whiteSpace).toBe('nowrap'); + }); + + it('truncates a long question on one line with an ellipsis before reaching the asker', () => { + const longQuestion = 'How exactly did you manage to get your first paying client when you had zero previous portfolio items or testimonials? '.repeat(2); + const rendered = tree({ question1: longQuestion }); + + const qSpan = allElements(rendered).find( + (el) => el.type === 'span' && textContent(el).some((t) => t.startsWith('How exactly did you manage')), + ); + expect(qSpan).toBeDefined(); + expect(qSpan?.props.style?.whiteSpace).toBe('nowrap'); + expect(qSpan?.props.style?.overflow).toBe('hidden'); + expect(qSpan?.props.style?.textOverflow).toBe('ellipsis'); + expect(qSpan?.props.style?.flex).toBe(1); + expect(textContent(qSpan)[0].length).toBeLessThanOrEqual(120); + expect(textContent(qSpan)[0]).toMatch(/…$/); + }); + + it('truncates a long asker on one line while keeping most of the row for the question', () => { + const longAsker = 'Alexander Montgomery-Fitzgerald the Third, VP of Engineering'; + const rendered = tree({ asker1: longAsker }); + + const askerSpan = allElements(rendered).find( + (el) => el.type === 'span' && textContent(el).some((t) => t.startsWith('Alexander')), + ); + expect(askerSpan).toBeDefined(); + expect(askerSpan?.props.style?.maxWidth).toBe('320px'); + expect(askerSpan?.props.style?.overflow).toBe('hidden'); + expect(askerSpan?.props.style?.textOverflow).toBe('ellipsis'); + expect(askerSpan?.props.style?.whiteSpace).toBe('nowrap'); + expect(textContent(askerSpan)[0]).toHaveLength(40); + expect(textContent(askerSpan)[0]).toMatch(/…$/); + }); + + it('omits siteName or issue cleanly without leaving an awkward gap', () => { + const noSite = tree({ siteName: '' }); + const noIssue = tree({ issue: '' }); + const noHeader = tree({ siteName: '', issue: '' }); + + expect(textContent(noSite)).not.toContain('The Friday Diff'); + expect(textContent(noSite)).toContain('Issue 58'); + + expect(textContent(noIssue)).toContain('The Friday Diff'); + expect(textContent(noIssue)).not.toContain('Issue 58'); + + expect(textContent(noHeader)).not.toContain('The Friday Diff'); + expect(textContent(noHeader)).not.toContain('Issue 58'); + }); + + it('renders cleanly on a light background with readable pill, Q circles, and askers', () => { + const rendered = tree({ + bgColor: '#ffffff', + textColor: '#111827', + accentColor: '#e07a5f', + }); + + // Pill color + const pillSpan = allElements(rendered).find( + (el) => el.type === 'span' && el.props.children === 'Mailbag', + ); + expect(pillSpan).toBeDefined(); + const pillColor = String(pillSpan?.props.style?.color ?? '').toLowerCase(); + expect(pillColor).not.toBe('#ffffff'); + expect(pillColor).not.toBe('#fff'); + + // Q circle border & text color + const qCircle = allElements(rendered).find( + (el) => el.type === 'div' && el.props.children === 'Q', + ); + expect(qCircle).toBeDefined(); + const qColor = String(qCircle?.props.style?.color ?? '').toLowerCase(); + expect(qColor).not.toBe('#ffffff'); + expect(qColor).not.toBe('#fff'); + + // Asker color + const askerSpan = allElements(rendered).find( + (el) => el.type === 'span' && textContent(el).includes('Sam, Lisbon'), + ); + expect(askerSpan).toBeDefined(); + const askerColor = String(askerSpan?.props.style?.color ?? '').toLowerCase(); + expect(askerColor).not.toBe('#ffffff'); + expect(askerColor).not.toBe('#fff'); + }); +});