import Link from 'next/link'; import dynamic from 'next/dynamic'; import { Fragment, ReactNode } from 'react'; import type { ArticleData, Block, Section } from '../lib/article-types'; const Mermaid = dynamic(() => import('./Mermaid'), { ssr: false, loading: () =>
Loading diagram…
}); const PALETTE = ['#2f5bea', '#e0702b', '#1b9e77', '#a14b9c', '#c9a227', '#d6336c', '#37a0d6']; /* ---------- inline markdown: **bold**, *italic*, `code`, [text](url) ---------- */ const safeHref = (u: string) => /^(https?:\/\/|\/|#|mailto:)/.test(u); export function inline(s: string): ReactNode[] { const out: ReactNode[] = []; const re = /(\[[^\]]+\]\([^)\s]+\))|(\*\*[^*]+\*\*)|(\*[^*\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 [, text, url] = t.match(/^\[([^\]]+)\]\(([^)\s]+)\)$/)!; if (!safeHref(url)) out.push(text); else if (url.startsWith('/') || url.startsWith('#')) out.push({text}); else out.push({text} ↗); } else if (m[2]) out.push({inline(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; } function MarkdownBlock({ text }: { text: string }) { return ( <> {text.replace(/\r/g, '').split(/\n{2,}/).map((raw, i) => { const src = raw.trim(); if (!src) return null; const h = src.match(/^(#{3,4})\s+(.*)$/); if (h && !src.includes('\n')) return h[1].length === 3 ?

{inline(h[2])}

:

{inline(h[2])}

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

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

; })} ); } /* ---------- horizontal bar chart (SVG) ---------- */ function BarChart({ d }: { d: Extract['data'] }) { const rowH = 22 + (d.series.length - 1) * 12, W = 640, L = 170, R = 56, top = 8; const H = top + d.labels.length * (rowH + 8) + 4; const max = Math.max(...d.series.flatMap((s) => s.values), 0) || 1; const X = (v: number) => (v / max) * (W - L - R); const allInt = d.series.every((s) => s.values.every(Number.isInteger)); const fmt = (v: number) => (allInt ? String(v) : v.toFixed(1)) + (d.unit ?? ''); return (
{d.title}
{d.labels.map((lab, i) => { const y = top + i * (rowH + 8); return ( {lab.length > 26 ? lab.slice(0, 25) + '…' : lab}{lab} {d.series.map((s, j) => { const h = rowH / d.series.length, yy = y + j * h; const hi = d.highlight?.includes(i); return ( {`${lab}${d.series.length > 1 ? ' · ' + s.name : ''}: ${fmt(s.values[i])}`} {fmt(s.values[i])} ); })} ); })} {d.series.length > 1 &&
{d.series.map((s, j) => {s.name})}
} {d.note &&
{d.note}
}
); } function BlockView({ b }: { b: Block }) { switch (b.type) { case 'markdown': return ; case 'list': { const Tag = b.data.ordered ? 'ol' : 'ul'; return {b.data.items.map((it, i) =>
  • {inline(it)}
  • )}
    ; } case 'callout': return
    {b.data.title && {b.data.title}. }{inline(b.data.text)}
    ; case 'table': { const d = b.data; return (
    {d.caption &&
    {d.caption}
    }
    {d.columns.map((c, i) => )}{d.rows.map((r, i) => {r.map((c, j) => )})}
    {c}
    {inline(c)}
    ); } case 'chart': return ; case 'mermaid': return ; case 'figure': return /^(https:\/\/|\/)/.test(b.data.src) ?
    {b.data.alt}{b.data.caption &&
    {b.data.caption}
    }
    : null; case 'code': return
    {b.data.text}
    ; } } function SectionView({ s, id }: { s: Section; id: string }) { const H = (s.level <= 2 ? 'h2' : 'h3') as 'h2' | 'h3'; return (
    {s.title} {s.blocks.map((b, i) => )} {s.children.map((c) => )}
    ); } const toc = (secs: Section[], id: string, depth = 0): ReactNode => secs.map((s) => ( {s.title} {toc(s.children, id, depth + 1)} )); export default function ArticleView({ a }: { a: ArticleData }) { return (
    Analysis

    {a.title}

    {a.summary &&

    {a.summary}

    }

    revision {a.revision} · updated {a.updatedAt}

    {a.sections.map((s) => )} {a.links.length > 0 && (

    Links

      {a.links.map((l) => (
    • {l.kind}{' '} {l.url.startsWith('/') ? {l.title} : {l.title} ↗} {l.note && — {l.note}}
    • ))}
    )}
    ); }