import type { Section } from "../types"; /** Minimal Block type — matches the structure returned by the Spire API. */ interface Block { type?: string; position?: number; content?: Record; system_block_id?: string; custom_block_id?: string; } const BASE = "blog/sections/blocks"; function toSection(resolveType: string, props: Record): Section { return { __resolveType: resolveType, ...props } as Section; } /** * Converts a Spire Block array into a Section[] using blog/sections/blocks * components. Sections are sorted by block position before conversion. * Supports an optional override map to remap block types to custom section * renderers. */ export function blocksToSections( blocks: Block[], overrides: Record = {}, ): Section[] { return [...blocks] .sort((a, b) => (a.position ?? 0) - (b.position ?? 0)) .map((block) => blockToSection(block, overrides)) .filter((s): s is Section => s !== null); } function blockToSection( block: Block, overrides: Record, ): Section | null { const content = (block.content ?? {}) as Record; if ("type" in block && block.type) { if (overrides[block.type]) { return toSection(overrides[block.type]?.__resolveType ?? block.type, content); } switch (block.type) { case "paragraph": return toSection(`${BASE}/Paragraph.tsx`, { html: content.html, text: content.text, }); case "heading": return toSection(`${BASE}/Heading.tsx`, { text: content.text, level: content.level, }); case "list": return toSection(`${BASE}/List.tsx`, { items: content.items, style: content.style, }); case "divider": return toSection(`${BASE}/Divider.tsx`, {}); case "quote": return toSection(`${BASE}/Quote.tsx`, { quote: content.quote, text: content.text, attribution: content.attribution, source: content.source, }); case "callout": return toSection(`${BASE}/Callout.tsx`, { title: content.title, body: content.body, variant: content.variant, }); case "checklist": return toSection(`${BASE}/Checklist.tsx`, { title: content.title, items: content.items, }); case "steps": return toSection(`${BASE}/Steps.tsx`, { title: content.title, steps: content.steps, }); case "stat": return toSection(`${BASE}/Stat.tsx`, { value: content.value, label: content.label, description: content.description, }); case "stat-group": return toSection(`${BASE}/StatGroup.tsx`, { stats: content.stats, }); case "card-group": return toSection(`${BASE}/CardGroup.tsx`, { cards: content.cards, }); case "comparison": return toSection(`${BASE}/Comparison.tsx`, { left: content.left, right: content.right, }); case "table": return toSection(`${BASE}/Table.tsx`, { headers: content.headers, rows: content.rows, }); case "image": return toSection(`${BASE}/BlockImage.tsx`, { url: content.url, mobileUrl: content.mobileUrl, alt: content.alt, caption: content.caption, size: content.size, // Upstream does not forward this, which leaves every block-pipeline // image lazy-loaded and the prop reachable only on a hand-placed // BlockImage section. highPriority: content.highPriority, }); case "video": return toSection(`${BASE}/Video.tsx`, { url: content.url, caption: content.caption, }); case "code": return toSection(`${BASE}/Code.tsx`, { code: content.code, language: content.language, filename: content.filename, }); case "cta": return toSection(`${BASE}/Cta.tsx`, { text: content.text, href: content.href, }); default: return null; } } // System blocks (no explicit type) — render as paragraph if they have html/text if ("system_block_id" in block) { if (content.html) { return toSection(`${BASE}/Paragraph.tsx`, { html: content.html as string, }); } if (content.text) { return toSection(`${BASE}/Paragraph.tsx`, { text: content.text as string, }); } } return null; }