import React, { lazy, Suspense } from 'react'; import { renderInlineMarkdown } from '../utils/markdownUtils'; const KatexRenderer = lazy(() => import('./KatexRenderer')); interface MarkdownRendererProps { content: string; className?: string; } /** * Full Markdown + LaTeX component with retro aesthetic */ export const MarkdownRenderer: React.FC = ({ content, className = '' }) => { if (!content) return null; // Split content into distinct structural blocks (code blocks, display math blocks, and normal text segments) const blockRegex = /(```[\s\S]*?```|\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\])/g; const blocks = content.split(blockRegex); return (
{blocks.map((block, blockIndex) => { if (!block) return null; const trimmedBlock = block.trim(); // Code Block: ```lang ... ``` if (trimmedBlock.startsWith('```') && trimmedBlock.endsWith('```') && trimmedBlock.length >= 6) { const firstLineEnd = trimmedBlock.indexOf('\n'); const lang = firstLineEnd !== -1 ? trimmedBlock.slice(3, firstLineEnd).trim() : ''; const codeContent = firstLineEnd !== -1 ? trimmedBlock.slice(firstLineEnd + 1, -3).trimEnd() : trimmedBlock.slice(3, -3).trim(); return (
{lang && (
{lang} SNIPPET
)}
                {codeContent}
              
); } // Display Math Block: $$ ... $$ or \[ ... \] if ( (trimmedBlock.startsWith('$$') && trimmedBlock.endsWith('$$') && trimmedBlock.length >= 4) || (trimmedBlock.startsWith('\\[') && trimmedBlock.endsWith('\\]') && trimmedBlock.length >= 4) ) { const math = trimmedBlock.slice(2, -2); return (
{math}}>
); } // Normal text lines, headings, tables, lists, blockquotes const lines = block.split('\n'); const renderedElements: React.ReactNode[] = []; let i = 0; while (i < lines.length) { const line = lines[i]; const trimmed = line.trim(); const lineKey = `${blockIndex}-${i}`; // Empty line if (trimmed === '') { renderedElements.push(
); i++; continue; } // Table detection if (trimmed.startsWith('|') && trimmed.endsWith('|') && i + 1 < lines.length && lines[i + 1].trim().startsWith('|')) { const tableLines: string[] = []; while (i < lines.length && lines[i].trim().startsWith('|') && lines[i].trim().endsWith('|')) { tableLines.push(lines[i].trim()); i++; } if (tableLines.length >= 2) { const headerCells = tableLines[0] .slice(1, -1) .split('|') .map(c => c.trim()); const bodyRows = tableLines.slice(2).map(row => row .slice(1, -1) .split('|') .map(c => c.trim()) ); renderedElements.push(
{headerCells.map((cell, idx) => ( ))} {bodyRows.map((row, rowIdx) => ( {row.map((cell, cellIdx) => ( ))} ))}
{renderInlineMarkdown(cell)}
{renderInlineMarkdown(cell)}
); continue; } } // Headings if (trimmed.startsWith('#### ')) { renderedElements.push(

{renderInlineMarkdown(trimmed.substring(5))}

); i++; continue; } if (trimmed.startsWith('### ')) { renderedElements.push(

{renderInlineMarkdown(trimmed.substring(4))}

); i++; continue; } if (trimmed.startsWith('## ')) { renderedElements.push(

{renderInlineMarkdown(trimmed.substring(3))}

); i++; continue; } if (trimmed.startsWith('# ')) { renderedElements.push(

{renderInlineMarkdown(trimmed.substring(2))}

); i++; continue; } // Horizontal rule if (trimmed === '---' || trimmed === '***' || trimmed === '___') { renderedElements.push(
); i++; continue; } // Blockquote if (line.trim().startsWith('>')) { renderedElements.push(
{renderInlineMarkdown(line.trim().substring(1).trim())}
); i++; continue; } // Unordered list item (- or *) if (trimmed.startsWith('- ') || trimmed.startsWith('* ') || trimmed.startsWith('+ ')) { const indentLevel = line.search(/\S/) >= 2 ? 'pl-6' : 'pl-2'; renderedElements.push(
•
{renderInlineMarkdown(trimmed.substring(2))}
); i++; continue; } // Ordered list item (1. 2. etc.) const numberedMatch = trimmed.match(/^(\d+)\.\s+(.*)$/); if (numberedMatch) { const indentLevel = line.search(/\S/) >= 2 ? 'pl-6' : 'pl-2'; renderedElements.push(
{numberedMatch[1]}
{renderInlineMarkdown(numberedMatch[2])}
); i++; continue; } // Default paragraph renderedElements.push(

{renderInlineMarkdown(line)}

); i++; } return {renderedElements}; })}
); }; export default MarkdownRenderer;