import { query, tx } from './db'; import { ensureAnalysisSchema } from './analysis-schema'; import { HttpError } from './api'; // Block shapes (data column) — see lib/article-types.ts for the typed version: // markdown {text} list {items[], ordered?} callout {text, title?, tone} table {columns[], rows[][], caption?, numeric?, highlightCol?} // chart {kind:'bar', title, labels[], series:[{name, values[]}], unit?, note?, highlight?[]} mermaid {code, caption?} // figure {src, alt, caption?} code {text, lang?} const TYPES = ['markdown', 'list', 'callout', 'table', 'chart', 'mermaid', 'figure', 'code']; const KINDS = ['paper', 'code', 'project', 'demo', 'dataset', 'related', 'other']; export interface SectionInput { key: string; title: string; level?: number; blocks?: { type: string; data: any }[]; children?: SectionInput[] } export interface ArticleInput { slug: string; title: string; summary?: string | null; subject: { conf: string; ext_id: string }; sections: SectionInput[]; links?: { kind: string; title: string; url: string; note?: string | null; section?: string | null }[]; } const bad = (msg: string): never => { throw new HttpError(400, msg); }; export function validateArticle(a: any): ArticleInput { if (!a || typeof a !== 'object') bad('article must be a JSON object'); if (typeof a.slug !== 'string' || !/^[a-z0-9][a-z0-9-]*$/.test(a.slug)) bad('slug must be lowercase letters, digits and dashes'); if (typeof a.title !== 'string' || !a.title.trim()) bad('missing title'); if (!a.subject || typeof a.subject.conf !== 'string' || typeof a.subject.ext_id !== 'string') bad('missing subject.conf / subject.ext_id (the paper, e.g. {"conf":"acl2026","ext_id":"2026.acl-long.1"})'); if (!Array.isArray(a.sections) || !a.sections.length) bad('no sections'); const keys = new Set(); const walk = (secs: any[]) => { for (const s of secs) { if (!s.key || !s.title) bad('every section needs key and title'); if (keys.has(s.key)) bad(`duplicate section key ${s.key}`); keys.add(s.key); (s.blocks || []).forEach((b: any, i: number) => { const at = `${s.key}[${i}]`; if (!TYPES.includes(b.type)) bad(`${at}: unknown block type ${b.type}`); const d = b.data || {}; if (b.type === 'markdown' && typeof d.text !== 'string') bad(`${at}: markdown needs text`); if (b.type === 'list' && !Array.isArray(d.items)) bad(`${at}: list needs items`); if (b.type === 'mermaid' && typeof d.code !== 'string') bad(`${at}: mermaid needs code`); if (b.type === 'table' && !(Array.isArray(d.columns) && Array.isArray(d.rows) && d.rows.every((r: any[]) => r.length === d.columns.length))) bad(`${at}: table rows must match columns`); if (b.type === 'chart' && !(Array.isArray(d.labels) && Array.isArray(d.series) && d.series.every((x: any) => Array.isArray(x.values) && x.values.length === d.labels.length))) bad(`${at}: chart series must match labels`); }); walk(s.children || []); } }; walk(a.sections); for (const l of a.links || []) { if (!KINDS.includes(l.kind) || !l.url || !l.title) bad(`bad link ${JSON.stringify(l)} (kind must be one of ${KINDS.join(', ')})`); if (l.section && !keys.has(l.section)) bad(`link "${l.title}" points to unknown section ${l.section}`); } return a as ArticleInput; } /** Insert or replace a whole article (sections, blocks, links) in one transaction. */ export async function saveArticle(input: unknown) { const a = validateArticle(input); await ensureAnalysisSchema(); const saved = await tx(async (q) => { const ex = await q<{ id: number }>('SELECT id FROM analysis.article WHERE slug = ?', [a.slug]); let id: number; let created = false; if (ex.length) { id = ex[0].id; await q('DELETE FROM analysis.link WHERE article_id = ?', [id]); await q('DELETE FROM analysis.section WHERE article_id = ?', [id]); // cascades to blocks await q('UPDATE analysis.article SET subject_conf=?, subject_ext_id=?, title=?, summary=?, revision=revision+1, updated_at=now() WHERE id=?', [a.subject.conf, a.subject.ext_id, a.title, a.summary ?? null, id]); } else { created = true; id = (await q<{ id: number }>('INSERT INTO analysis.article(slug, subject_conf, subject_ext_id, title, summary) VALUES (?,?,?,?,?) RETURNING id', [a.slug, a.subject.conf, a.subject.ext_id, a.title, a.summary ?? null]))[0].id; } const secIds: Record = {}; let blocks = 0; const insert = async (secs: SectionInput[], parent: number | null) => { for (let i = 0; i < secs.length; i++) { const s = secs[i]; const sid = (await q<{ id: number }>('INSERT INTO analysis.section(article_id, parent_id, key, title, level, position) VALUES (?,?,?,?,?,?) RETURNING id', [id, parent, s.key, s.title, s.level ?? (parent ? 3 : 2), i]))[0].id; secIds[s.key] = sid; const bl = s.blocks || []; for (let j = 0; j < bl.length; j++) { await q('INSERT INTO analysis.block(section_id, position, type, data) VALUES (?,?,?,?::jsonb)', [sid, j, bl[j].type, JSON.stringify(bl[j].data)]); blocks++; } await insert(s.children || [], sid); } }; await insert(a.sections, null); const links = a.links || []; for (let i = 0; i < links.length; i++) { const l = links[i]; await q('INSERT INTO analysis.link(article_id, section_id, position, kind, title, url, note) VALUES (?,?,?,?,?,?,?)', [id, l.section ? secIds[l.section] : null, i, l.kind, l.title, l.url, l.note ?? null]); } const rev = (await q<{ revision: number }>('SELECT revision FROM analysis.article WHERE id = ?', [id]))[0].revision; return { slug: a.slug, created, revision: rev, sections: Object.keys(secIds).length, blocks, links: links.length }; }); // does the paper exist in the current data build? (the article is stored either way, keyed by conf + ext_id) const [paper] = await query<{ id: number; title: string }>('SELECT id, title FROM papers WHERE conf = ? AND ext_id = ?', [a.subject.conf, a.subject.ext_id]); return { ...saved, paper: paper ? { id: paper.id, title: paper.title, url: `/papers/${paper.id}` } : null }; } export async function listArticles() { await ensureAnalysisSchema(); return query(`SELECT slug, title, subject_conf AS conf, subject_ext_id AS ext_id, revision, updated_at FROM analysis.article ORDER BY updated_at DESC`); } /** The article in the same JSON shape that saveArticle accepts, so GET then PUT round-trips. */ export async function exportArticle(slug: string): Promise { await ensureAnalysisSchema(); const [a] = await query('SELECT id, slug, subject_conf, subject_ext_id, title, summary, revision FROM analysis.article WHERE slug = ?', [slug]); if (!a) throw new HttpError(404, `no article with slug ${slug}`); const [secs, blocks, links] = await Promise.all([ query('SELECT id, parent_id, key, title, level FROM analysis.section WHERE article_id = ? ORDER BY position, id', [a.id]), query('SELECT b.section_id, b.type, b.data FROM analysis.block b JOIN analysis.section s ON s.id = b.section_id WHERE s.article_id = ? ORDER BY b.position', [a.id]), query('SELECT l.kind, l.title, l.url, l.note, s.key AS section FROM analysis.link l LEFT JOIN analysis.section s ON s.id = l.section_id WHERE l.article_id = ? ORDER BY l.position, l.id', [a.id]), ]); const node = new Map(); for (const s of secs) node.set(s.id, { key: s.key, title: s.title, level: s.level, blocks: blocks.filter((b) => b.section_id === s.id).map((b) => ({ type: b.type, data: b.data })), children: [] }); const roots: SectionInput[] = []; for (const s of secs) (s.parent_id && node.has(s.parent_id) ? node.get(s.parent_id)!.children! : roots).push(node.get(s.id)!); return { slug: a.slug, title: a.title, summary: a.summary, revision: a.revision, subject: { conf: a.subject_conf, ext_id: a.subject_ext_id }, sections: roots, links: links.map((l) => ({ kind: l.kind, title: l.title, url: l.url, note: l.note, section: l.section })), }; } export async function deleteArticle(slug: string) { await ensureAnalysisSchema(); const r = await query<{ id: number }>('DELETE FROM analysis.article WHERE slug = ? RETURNING id', [slug]); if (!r.length) throw new HttpError(404, `no article with slug ${slug}`); return { deleted: slug }; }