Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189import type { BlockNode, InlineNode, ListItem, TodoItem, HtmlAttrs, ParseResult } from "./types.js";import { applyTypography } from "./inline.js";
function escHtml(s: string): string { return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);}
function attrsStr(attrs: HtmlAttrs): string { let s = ""; if (attrs.id) s += ` id="${escHtml(attrs.id)}"`; if (attrs.class?.length) s += ` class="${escHtml(attrs.class.join(" "))}"`; return s;}
function extraAttrs(attrs: Record<string, string>): string { return Object.entries(attrs) .map(([k, v]) => v ? ` ${escHtml(k)}="${escHtml(v)}"` : ` ${escHtml(k)}`) .join("");}
export function render(result: ParseResult): string { const ctx: RenderCtx = { refs: result.references, fns: result.footnotes, fnOrder: [], fnSeen: new Set() }; const body = result.blocks.map(b => renderBlock(b, ctx)).join("\n"); const fnHtml = renderFootnotes(ctx); return body + (fnHtml ? "\n" + fnHtml : "");}
interface RenderCtx { refs: Map<string, string>; fns: Map<string, InlineNode[]>; fnOrder: string[]; fnSeen: Set<string>;}
// ── block rendering ────────────────────────────────────────────────────────
function renderBlock(node: BlockNode, ctx: RenderCtx): string { switch (node.type) { case "paragraph": { const inner = renderInlines(node.children, ctx); const sn = node.sidenote ? `<aside class="sidenote">${renderInlines(node.sidenote.children, ctx)}</aside>` : ""; return `<p>${inner}</p>${sn}`; }
case "heading": { const tag = `h${node.level}`; const idAttr = node.id ? ` id="${escHtml(node.id)}"` : ""; return `<${tag}${idAttr}>${renderInlines(node.children, ctx)}</${tag}>`; }
case "ul": { const items = node.items.map(it => ` <li>${renderInlines(it.children, ctx)}${it.nested ? "\n" + renderBlock(it.nested, ctx) : ""}</li>` ).join("\n"); return `<ul>\n${items}\n</ul>`; }
case "ol": { const items = node.items.map(it => ` <li>${renderInlines(it.children, ctx)}${it.nested ? "\n" + renderBlock(it.nested, ctx) : ""}</li>` ).join("\n"); return `<ol>\n${items}\n</ol>`; }
case "todo-list": { const items = node.items.map(it => { const checked = it.checked ? " checked" : ""; return ` <li><input type="checkbox"${checked} disabled> ${renderInlines(it.children, ctx)}</li>`; }).join("\n"); return `<ul class="todo-list">\n${items}\n</ul>`; }
case "code-block": { const lang = node.lang ? ` class="language-${escHtml(node.lang)}"` : ""; return `<pre><code${lang}>${escHtml(node.value)}</code></pre>`; }
case "blockquote": return `<blockquote>${renderInlines(node.children, ctx)}</blockquote>`;
case "markdown-blockquote": { const inner = node.children.map(b => renderBlock(b, ctx)).join("\n"); return `<blockquote>${inner}</blockquote>`; }
case "sidenote": return `<aside class="sidenote">${renderInlines(node.children, ctx)}</aside>`;
case "math-block": return `<div class="math-block">\\[${escHtml(node.value)}\\]</div>`;
case "image": { const alt = node.attrs.alt ? ` alt="${escHtml(node.attrs.alt)}"` : ""; const rest = extraAttrs(Object.fromEntries(Object.entries(node.attrs).filter(([k]) => k !== "alt"))); const img = `<img src="${escHtml(node.src)}"${alt}${rest}>`; if (node.caption) { return `<figure>${img}<figcaption>${renderInlines(node.caption, ctx)}</figcaption></figure>`; } return `<figure>${img}</figure>`; }
case "html-block": { const a = attrsStr(node.attrs) + extraAttrs(node.attrs.extra ?? {}); if (node.selfClose) return `<${escHtml(node.tag)}${a}>`; const inner = node.children.map(b => renderBlock(b, ctx)).join("\n"); return `<${escHtml(node.tag)}${a}>${inner ? "\n" + inner + "\n" : ""}</${escHtml(node.tag)}>`; }
case "footnote-def": // Definitions are collected in pre-pass; nothing to render in-place return "";
case "raw-html": return node.value;
case "comment": return ""; }}
// ── inline rendering ───────────────────────────────────────────────────────
function renderInlines(nodes: InlineNode[], ctx: RenderCtx): string { return nodes.map(n => renderInline(n, ctx)).join("");}
function renderInline(node: InlineNode, ctx: RenderCtx): string { switch (node.type) { case "text": return escHtml(applyTypography(node.value));
case "bold": return `<strong>${renderInlines(node.children, ctx)}</strong>`;
case "italic": return `<em>${renderInlines(node.children, ctx)}</em>`;
case "code": return `<code>${escHtml(node.value)}</code>`;
case "math-inline": return `<span class="math-inline">\\(${escHtml(node.value)}\\)</span>`;
case "link": { let href = node.href; if (href.startsWith("^")) { const refId = href.slice(1); href = ctx.refs.get(refId) ?? "#"; } return `<a href="${escHtml(href)}">${renderInlines(node.children, ctx)}</a>`; }
case "wikilink": { const href = node.page; return `<a href="${escHtml(href)}" class="wikilink">${escHtml(node.display)}</a>`; }
case "footnote-ref": { const id = node.id; if (!ctx.fnSeen.has(id)) { ctx.fnSeen.add(id); ctx.fnOrder.push(id); } const n = ctx.fnOrder.indexOf(id) + 1; return `<sup><a href="#fn-${escHtml(id)}" id="fnref-${escHtml(id)}">[${n}]</a></sup>`; }
case "sidenote-inline": return `<span class="sidenote-inline">${renderInlines(node.children, ctx)}</span>`;
case "image-inline": { const alt = node.attrs.alt ? ` alt="${escHtml(node.attrs.alt)}"` : ""; return `<img src="${escHtml(node.src)}"${alt}>`; }
case "html": return node.value; }}
// ── footnotes section ──────────────────────────────────────────────────────
function renderFootnotes(ctx: RenderCtx): string { if (ctx.fnOrder.length === 0) return ""; const items = ctx.fnOrder.map((id, i) => { const content = ctx.fns.get(id); const text = content ? renderInlines(content, ctx) : escHtml(ctx.refs.get(id) ?? ""); return ` <li id="fn-${escHtml(id)}">${text} <a href="#fnref-${escHtml(id)}">↩</a></li>`; }).join("\n"); return `<section class="footnotes">\n<ol>\n${items}\n</ol>\n</section>`;}