import React from "react"; import { EditorJsDoc, EditorJsBlock, EditorJsParagraphBlock, EditorJsHeaderBlock, EditorJsListBlock, EditorJsQuoteBlock, EditorJsLinkBlock, EditorJsImageBlock, EditorJsTableBlock, EditorJsCodeBlock, } from "@/app/utils/editorJsUtils"; function EditorJsRenderer({ content }: { content?: string | null }) { if (!content) return null; try { const parsed = JSON.parse(content) as EditorJsDoc; const blocks = parsed.blocks || []; if (!blocks.length) return null; return (
{blocks.map((b: EditorJsBlock, idx: number) => { const key = b.id || String(idx); switch (b.type) { case "paragraph": { const html = String((b.data as EditorJsParagraphBlock["data"])?.text || ""); return (

); } case "header": { const level = Math.min(6, Math.max(1, Number((b as EditorJsHeaderBlock).data?.level) || 2)); const html = String((b as EditorJsHeaderBlock).data?.text || ""); if (level === 1) return

; if (level === 2) return

; if (level === 3) return

; if (level === 4) return

; if (level === 5) return

; return
; } case "list": { const data = (b as EditorJsListBlock).data || {}; const style = data.style === "ordered" ? "ol" : "ul"; const items: string[] = Array.isArray(data.items) ? data.items : []; if (style === "ol") { return (
    {items.map((it, i) => (
  1. ))}
); } return ( ); } case "quote": { const data = (b as EditorJsQuoteBlock).data || {}; const text = String(data.text || ""); const caption = String(data.caption || ""); const alignment = data.alignment || "left"; return (
{caption ? ( ) : null}
); } case "linkTool": { const data = (b as EditorJsLinkBlock).data || {}; const link = data.link || ""; const title = data.meta?.title || link; const description = data.meta?.description || ""; const image = data.meta?.image?.url || ""; return (
{image && ( {title} )}

{title}

{description && (

{description}

)}

{new URL(link).hostname}

); } case "image": { const data = (b as EditorJsImageBlock).data || {}; const url = data.file?.url || ""; const caption = data.caption || ""; const withBorder = data.withBorder; const withBackground = data.withBackground; const stretched = data.stretched; if (!url) return null; return (
{caption {caption && (
{caption}
)}
); } case "delimiter": { return (
); } case "table": { const data = (b as EditorJsTableBlock).data || {}; const withHeadings = data.withHeadings || false; const content = data.content || []; if (content.length === 0) return null; return (
{withHeadings && content[0] && ( {content[0].map((cell, cellIdx) => ( ))} )} {content.slice(withHeadings ? 1 : 0).map((row, rowIdx) => ( {row.map((cell, cellIdx) => ( ))} ))}
{cell}
{cell}
); } case "code": { const data = (b as EditorJsCodeBlock).data || {}; const code = data.code || ""; return (
                  {code}
                
); } default: { return null; } } })}
); } catch { return (

); } } export default EditorJsRenderer;