/** * `@tanstack/cms-core/read` — the tree-shakeable read + render surface for * public pages on any runtime, including the edge (Kody/workerd, Cloudflare * Workers, etc.). * * This module is deliberately **self-contained**: it imports nothing from * `config`, `schema`, `server`, the storage engine, AI/MCP/code-mode, or * `node:*`. That is what lets a public site bundle to a few KB with no WASM, * no `eval`, and no server engine. The write/admin/migrate engine * (`@tanstack/cms-core/server`) is a separate, heavier world; this is the half * a blog, docs site, or marketing site needs to render. */ // --- Document shape ------------------------------------------------------- /** A stored document's content (the `doc` JSONB in the CMS schema). */ export type Doc = Record /** A content block: `{ _type, ...fields }`. */ export type Block = { _type?: string; [key: string]: unknown } /** A document as returned from a store: its id + content. */ export interface ReadRecord { id: string collection?: string doc: Doc } const asString = (v: unknown): string => (typeof v === 'string' ? v : '') export function isPublished(doc: Doc | null | undefined): boolean { return !!doc && doc.published !== false } // --- HTML + markdown primitives ------------------------------------------- export function escapeHtml(value: unknown): string { return asString(value) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') } /** * Intentionally tiny markdown: blank-line paragraphs, `**bold**`, `` `code` ``. * Enough for block/body text without pulling a markdown dependency into an edge * bundle. Swap in a full renderer app-side if you need one. */ export function renderMarkdown(text: string): string { return asString(text) .split(/\n{2,}/) .filter((p) => p.trim()) .map((p) => { const inner = escapeHtml(p) .replace(/\*\*([^*]+)\*\*/g, '$1') .replace(/`([^`]+)`/g, '$1') return `

${inner}

` }) .join('') } // --- Block rendering ------------------------------------------------------ export interface RenderContext { escapeHtml: (v: unknown) => string renderMarkdown: (text: string) => string } export type BlockRenderer = (block: Block, ctx: RenderContext) => string /** Renderers for the built-in block palette → semantic, unstyled HTML. */ export const defaultBlockRenderers: Record = { hero: (b, c) => `

${c.escapeHtml(b.heading)}

${ asString(b.subheading) ? `

${c.escapeHtml(b.subheading)}

` : '' }
`, richText: (b, c) => c.renderMarkdown(asString(b.markdown)), quote: (b, c) => `

${c.escapeHtml(b.text)}

${ asString(b.attribution) ? `${c.escapeHtml(b.attribution)}` : '' }
`, image: (b, c) => asString(b.image) ? `
${c.escapeHtml(b.caption)}${ asString(b.caption) ? `
${c.escapeHtml(b.caption)}
` : '' }
` : '', cta: (b, c) => `

${ c.escapeHtml(b.label) || c.escapeHtml(b.heading) }

`, callout: (b, c) => ``, code: (b, c) => `
${c.escapeHtml(b.code)}
`, } const DEFAULT_CTX: RenderContext = { escapeHtml, renderMarkdown } /** * Render an array of blocks to an HTML string. Pass `renderers` to override or * extend the built-ins (merged over the defaults); unknown block types render * to empty string. */ export function renderBlocks( blocks: ReadonlyArray | unknown, renderers: Record = {}, ctx: RenderContext = DEFAULT_CTX, ): string { if (!Array.isArray(blocks)) return '' const map = { ...defaultBlockRenderers, ...renderers } return (blocks as Array) .map((b) => (b && map[String(b._type)] ? map[String(b._type)](b, ctx) : '')) .join('') } // --- Reading published documents ------------------------------------------ /** * The minimal read surface the helpers below need. Implement it over whatever * store you like — the full `@tanstack/cms-core/server` storage adapter * satisfies it, and so does a ~10-line edge adapter over a serverless Postgres * HTTP driver. Keeping it this small is what keeps the engine out of the bundle. */ export interface ReadStore { getDocument: (collection: string, id: string) => Promise queryDocuments: (query: { collections: Array limit?: number }) => Promise<{ items: Array; total: number }> } /** Get a single document by id, or null. Pass `publishedOnly` to gate drafts. */ export async function getDocument( store: ReadStore, collection: string, id: string, options: { publishedOnly?: boolean } = {}, ): Promise { const rec = await store.getDocument(collection, id) if (!rec) return null if (options.publishedOnly && !isPublished(rec.doc)) return null return rec.doc } /** List documents in a collection. Published-only by default. */ export async function listDocuments( store: ReadStore, collection: string, options: { limit?: number; publishedOnly?: boolean } = {}, ): Promise> { const { limit = 100, publishedOnly = true } = options const res = await store.queryDocuments({ collections: [collection], limit }) return publishedOnly ? res.items.filter((r) => isPublished(r.doc)) : res.items }