// Import the leaf, not the richtext barrel — the barrel re-exports // RenderRichText (a React component), which would pull React into this pure, // server-usable module. import { lexicalToMarkdown } from '../richtext/lexicalToMarkdown.js'; const HEADING_KEYS = new Set([ 'heading', 'title' ]); const SUBHEADING_KEYS = new Set([ 'subheading', 'subtitle' ]); const QUESTION_KEYS = new Set([ 'question' ]); const TEXT_KEYS = new Set([ 'body', 'text', 'description', 'excerpt', 'summary', 'answer', 'caption', 'label', 'content' ]); const ARRAY_KEYS = new Set([ 'items', 'features', 'faq', 'faqs', 'cards', 'columns', 'rows', 'questions', 'services', 'steps', 'points', 'benefits', 'list' ]); /** A populated richText value is an object with a `root`. */ function isRichText(v) { return !!v && typeof v === 'object' && 'root' in v; } /** A populated upload (image) is an object with a `url`. */ function imageMarkdown(v) { if (!v || typeof v !== 'object') return ''; const o = v; const url = typeof o.url === 'string' ? o.url : ''; const alt = typeof o.alt === 'string' ? o.alt : ''; return url ? `![${alt}](${url})` : ''; } /** Extract readable Markdown from one block's known content fields. */ function extractBlock(block, depth) { const lines = []; for (const [key, value] of Object.entries(block)){ if (value == null) continue; if (HEADING_KEYS.has(key) && typeof value === 'string' && value.trim()) { lines.push(`## ${value.trim()}`); } else if (SUBHEADING_KEYS.has(key) && typeof value === 'string' && value.trim()) { lines.push(`### ${value.trim()}`); } else if (QUESTION_KEYS.has(key) && typeof value === 'string' && value.trim()) { lines.push(`**${value.trim()}**`); } else if (key === 'image') { const img = imageMarkdown(value); if (img) lines.push(img); } else if (isRichText(value)) { const md = lexicalToMarkdown(value); if (md) lines.push(md); } else if (TEXT_KEYS.has(key) && typeof value === 'string' && value.trim()) { lines.push(value.trim()); } else if (ARRAY_KEYS.has(key) && Array.isArray(value) && depth < 4) { for (const item of value){ if (item && typeof item === 'object') { const sub = extractBlock(item, depth + 1); if (sub.trim()) lines.push(sub); } else if (typeof item === 'string' && item.trim()) { lines.push(`- ${item.trim()}`); } } } } return lines.join('\n\n'); } export function blocksToMarkdown(layout, args = {}) { if (!Array.isArray(layout)) return ''; const { overrides = {} } = args; const out = []; for (const block of layout){ if (!block || typeof block !== 'object') continue; const b = block; const type = typeof b.blockType === 'string' ? b.blockType : ''; if (type && overrides[type]) { const md = overrides[type](b); if (md.trim()) out.push(md.trim()); continue; } const md = extractBlock(b, 0); if (md.trim()) out.push(md.trim()); } return out.join('\n\n').trim(); } //# sourceMappingURL=blocksToMarkdown.js.map