// src/components/lexical/Render.tsx import { Fragment, type ReactNode } from 'react' import { Callout } from './Callout' import { LinkPreviewCard } from './LinkPreviewCard' import { PostImage } from './PostImage' import { anchorFor } from '@/lib/lexical-headings' import { mediaUrl } from '@/lib/media-url' type Node = { type: string; [key: string]: any } function renderChildren(children: Node[] | undefined): ReactNode { return (children ?? []).map((c, i) => {renderNode(c)}) } function renderText(node: Node): ReactNode { let el: ReactNode = node.text const fmt = (node.format as number | undefined) ?? 0 if (fmt & 1) el = {el} if (fmt & 2) el = {el} if (fmt & 16) el = {el} return el } function renderNode(node: Node): ReactNode { switch (node.type) { case 'text': return renderText(node) case 'paragraph': return

{renderChildren(node.children)}

case 'heading': { const Tag = (node.tag as 'h2' | 'h3' | 'h4' | 'h5' | 'h6') ?? 'h2' const id = anchorFor(node as { children?: Array<{ type: string; text?: string }> }) return {renderChildren(node.children)} } case 'list': { const Tag = node.tag === 'ol' ? 'ol' : 'ul' return {renderChildren(node.children)} } case 'listitem': return
  • {renderChildren(node.children)}
  • case 'quote': return
    {renderChildren(node.children)}
    case 'horizontalrule': return
    case 'link': { const fields = node.fields ?? {} const href = fields.url ?? node.url const newTab = fields.newTab ?? false return ( {renderChildren(node.children)} ) } case 'block': { const f = node.fields ?? {} if (f.blockType === 'callout') { return ( ) } if (f.blockType === 'linkPreview') { return ( ) } if (f.blockType === 'mediaBlock') { const url = mediaUrl(f.media) if (!url) return null const alt = (typeof f.media === 'object' && f.media?.alt) || f.caption || '' return } if (f.blockType === 'Code') { return (
                {f.code}
              
    ) } return null } default: return null } } export function Render({ value }: { value?: { root: { children: Node[] } } | null }) { if (!value?.root?.children) return null return <>{renderChildren(value.root.children as Node[])} }