import 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, """); } 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 { 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; fns: Map; fnOrder: string[]; fnSeen: Set; } // ── block rendering ──────────────────────────────────────────────────────── function renderBlock(node: BlockNode, ctx: RenderCtx): string { switch (node.type) { case "paragraph": { const inner = renderInlines(node.children, ctx); const sn = node.sidenote ? `` : ""; return `

${inner}

${sn}`; } case "heading": { const tag = `h${node.level}`; const idAttr = node.id ? ` id="${escHtml(node.id)}"` : ""; return `<${tag}${idAttr}>${renderInlines(node.children, ctx)}`; } case "ul": { const items = node.items.map(it => `
  • ${renderInlines(it.children, ctx)}${it.nested ? "\n" + renderBlock(it.nested, ctx) : ""}
  • ` ).join("\n"); return `
      \n${items}\n
    `; } case "ol": { const items = node.items.map(it => `
  • ${renderInlines(it.children, ctx)}${it.nested ? "\n" + renderBlock(it.nested, ctx) : ""}
  • ` ).join("\n"); return `
      \n${items}\n
    `; } case "todo-list": { const items = node.items.map(it => { const checked = it.checked ? " checked" : ""; return `
  • ${renderInlines(it.children, ctx)}
  • `; }).join("\n"); return `
      \n${items}\n
    `; } case "code-block": { const lang = node.lang ? ` class="language-${escHtml(node.lang)}"` : ""; return `
    ${escHtml(node.value)}
    `; } case "blockquote": return `
    ${renderInlines(node.children, ctx)}
    `; case "markdown-blockquote": { const inner = node.children.map(b => renderBlock(b, ctx)).join("\n"); return `
    ${inner}
    `; } case "sidenote": return ``; case "math-block": return `
    \\[${escHtml(node.value)}\\]
    `; 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 = ``; if (node.caption) { return `
    ${img}
    ${renderInlines(node.caption, ctx)}
    `; } return `
    ${img}
    `; } 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" : ""}`; } 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 `${renderInlines(node.children, ctx)}`; case "italic": return `${renderInlines(node.children, ctx)}`; case "code": return `${escHtml(node.value)}`; case "math-inline": return `\\(${escHtml(node.value)}\\)`; case "link": { let href = node.href; if (href.startsWith("^")) { const refId = href.slice(1); href = ctx.refs.get(refId) ?? "#"; } return `${renderInlines(node.children, ctx)}`; } case "wikilink": { const href = node.page; return `${escHtml(node.display)}`; } 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 `[${n}]`; } case "sidenote-inline": return `${renderInlines(node.children, ctx)}`; case "image-inline": { const alt = node.attrs.alt ? ` alt="${escHtml(node.attrs.alt)}"` : ""; return ``; } 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 `
  • ${text} ↩
  • `; }).join("\n"); return `
    \n
      \n${items}\n
    \n
    `; }