'use client';
import Link from 'next/link';
import { Fragment, type ReactNode } from 'react';
/**
* Tiny dependency-free markdown renderer for assistant answers: paragraphs, headings, bullet and
* numbered lists, pipe tables, bold/italic/code and links (internal links use next/link).
* Not a full CommonMark implementation — enough for terminal-style research answers.
*/
export function Markdown({ text }: { text: string }) {
const blocks = splitBlocks(text);
return
{blocks.map((b, i) => {renderBlock(b)})}
;
}
function splitBlocks(text: string): string[] {
return text.replace(/\r\n/g, '\n').split(/\n{2,}/).map((b) => b.trim()).filter(Boolean);
}
function renderBlock(block: string): ReactNode {
const lines = block.split('\n');
if (lines.length >= 2 && lines.every((l) => l.trim().startsWith('|'))) return renderTable(lines);
if (lines.every((l) => /^\s*[-*•]\s+/.test(l))) return
{lines.map((l, i) =>
{inline(l.replace(/^\s*[-*•]\s+/, ''))}
)}
;
if (lines.every((l) => /^\s*\d+[.)]\s+/.test(l))) return {lines.map((l, i) =>