import Link from 'next/link'; import { Fragment, ReactNode } from 'react'; /** Minimal, safe markdown: paragraphs, ###/#### headings, -/1. lists, **bold**, *italic*, `code`, citations [#id] / [#a, #b]. */ export default function Markdown({ text, cite }: { text: string; cite: Record }) { const blocks = (text ?? '').replace(/\r/g, '').split(/\n{2,}/); return <>{blocks.map((b, i) => )}; } function Block({ src, cite }: { src: string; cite: Record }) { if (!src) return null; const h = src.match(/^(#{2,4})\s+(.*)$/); if (h && !src.includes('\n')) { const lvl = h[1].length; return lvl <= 3 ?

{inline(h[2], cite)}

:

{inline(h[2], cite)}

; } const lines = src.split('\n'); if (lines.every((l) => /^\s*[-*]\s+/.test(l))) return
    {lines.map((l, i) =>
  • {inline(l.replace(/^\s*[-*]\s+/, ''), cite)}
  • )}
; if (lines.every((l) => /^\s*\d+[.)]\s+/.test(l))) return
    {lines.map((l, i) =>
  1. {inline(l.replace(/^\s*\d+[.)]\s+/, ''), cite)}
  2. )}
; return

{inline(lines.join(' '), cite)}

; } export function inline(s: string, cite: Record): ReactNode[] { const out: ReactNode[] = []; const re = /(\[#\d+(?:\s*[,;]\s*#?\d+)*\])|(\*\*[^*]+\*\*)|(\*[^*\s][^*]*\*)|(`[^`]+`)/g; let last = 0; let m: RegExpExecArray | null; let k = 0; while ((m = re.exec(s))) { if (m.index > last) out.push(s.slice(last, m.index)); const t = m[0]; if (m[1]) { const ids = [...t.matchAll(/(\d+)/g)].map((x) => Number(x[1])); out.push([{ids.map((id, j) => ( {j > 0 && ','}{cite[id] ?? '?'} ))}]); } else if (m[2]) out.push({t.slice(2, -2)}); else if (m[3]) out.push({t.slice(1, -1)}); else out.push({t.slice(1, -1)}); last = m.index + t.length; } if (last < s.length) out.push(s.slice(last)); return out; } /** Inline list of citation chips for id arrays (tables). */ export function Cites({ ids, cite }: { ids: number[]; cite: Record }) { return <>{ids.map((id) => [{cite[id] ?? '?'}])}; } export { Link };