import { Fragment } from "react"; function inline(text, keyPrefix) { const pattern = /(\*\*[^*]+\*\*|\*[^*]+\*|`[^`]+`|\[[^\]]+\]\(https?:\/\/[^)\s]+\))/g; const nodes = []; let last = 0; let index = 0; for (const match of text.matchAll(pattern)) { if (match.index > last) nodes.push(text.slice(last, match.index)); const token = match[0]; const key = `${keyPrefix}-${index}`; if (token.startsWith("**")) nodes.push({token.slice(2, -2)}); else if (token.startsWith("*")) nodes.push({token.slice(1, -1)}); else if (token.startsWith("`")) nodes.push({token.slice(1, -1)}); else { const linked = token.match(/^\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)$/); if (linked) { nodes.push({linked[1]}); } } last = match.index + token.length; index += 1; } if (last < text.length) nodes.push(text.slice(last)); return nodes; } export function MarkdownView({ text }) { const lines = String(text || "").replace(/\r\n/g, "\n").split("\n"); const blocks = []; let list = null; function closeList() { if (!list) return; const items = list.items.map((item, index) =>
  • {inline(item, `${list.key}-${index}`)}
  • ); blocks.push(list.ordered ?
      {items}
    : ); list = null; } lines.forEach((line, index) => { const heading = line.match(/^(#{1,3})\s+(.*)$/); const bullet = line.match(/^\s*[-*]\s+(.*)$/); const numbered = line.match(/^\s*\d+\.\s+(.*)$/); if (heading) { closeList(); const Tag = heading[1].length === 1 ? "h3" : heading[1].length === 2 ? "h4" : "h5"; blocks.push({inline(heading[2], `h-${index}`)}); return; } if (bullet || numbered) { if (!list || list.ordered !== Boolean(numbered)) { closeList(); list = { key: `l-${index}`, ordered: Boolean(numbered), items: [] }; } list.items.push((bullet || numbered)[1]); return; } closeList(); if (!line.trim()) { blocks.push(); return; } blocks.push(

    {inline(line, `p-${index}`)}

    ); }); closeList(); return
    {blocks}
    ; }