import type { BlockKind, InlineNode, ListItemNode, MarkdownBlock } from "./types" export interface HtmlRenderOptions { gfm?: boolean math?: boolean } const DISALLOWED_RAW_HTML_RE = /<(\/?)(iframe|noembed|noframes|plaintext|script|style|title|textarea|xmp)(?=[\t\n\f\r />])/gi export function filterDisallowedRawHtml(raw: string): string { return raw.replace(DISALLOWED_RAW_HTML_RE, "<$1$2") } export function escapeHtml(text: string): string { if (!/[&<>"]/.test(text)) return text return text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """) } /** Image alt text the way commonmark.js renders it: tags suppressed, text escaped, raw HTML kept */ function renderAlt(nodes: ReadonlyArray): string { let out = "" for (const node of nodes) { switch (node.type) { case "text": case "code": out += escapeHtml(node.text) break case "strong": case "em": case "del": case "link": out += renderAlt(node.children) break case "image": out += node.children ? renderAlt(node.children) : escapeHtml(node.alt) break case "math": out += escapeHtml(node.source) break case "br": out += "\n" break case "html_inline": out += node.raw break } } return out } export function renderInlinesToHtml(nodes: ReadonlyArray, options?: HtmlRenderOptions): string { let out = "" const gfm = options?.gfm ?? false for (const node of nodes) { switch (node.type) { case "text": out += escapeHtml(node.text) break case "strong": out += `${renderInlinesToHtml(node.children, options)}` break case "em": out += `${renderInlinesToHtml(node.children, options)}` break case "del": out += `${renderInlinesToHtml(node.children, options)}` break case "code": out += `${escapeHtml(node.text)}` break case "math": out += `${escapeHtml(node.source)}` break case "link": { const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "" out += `${renderInlinesToHtml(node.children, options)}` break } case "image": { const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "" const alt = node.children ? renderAlt(node.children) : escapeHtml(node.alt) out += `${alt}` break } case "html_inline": out += gfm ? filterDisallowedRawHtml(node.raw) : node.raw break case "br": out += "
\n" break } } return out } const blockHtmlCache = new WeakMap() const paragraphHtmlCache = new WeakMap() const rowHtmlCache = new WeakMap() const CHECKED = ` ` const UNCHECKED = ` ` class HtmlWriter { private parts: string[] = [] private lastOut = "\n" constructor(readonly options: HtmlRenderOptions | undefined) {} get buffer(): string { return this.parts.join("") } out(s: string) { this.parts.push(s) this.lastOut = s } cr() { if (this.lastOut !== "\n") this.out("\n") } block(kind: BlockKind, tight: boolean, prefix = "") { const gfm = this.options?.gfm ?? false if (kind.type === "paragraph") { if (prefix) return this.blockUncached(kind, tight, prefix) const hit = paragraphHtmlCache.get(kind) const key = tight ? "tight" : "loose" let html = hit?.gfm === gfm ? hit[key] : undefined if (html === undefined) { html = renderInlinesToHtml(kind.inlines, this.options) const entry = hit?.gfm === gfm ? hit : { gfm } entry[key] = html paragraphHtmlCache.set(kind, entry) } if (tight) { this.out(html) } else { this.cr() this.out(`

${html}

`) this.cr() } return } if (!tight && !prefix) { // kinds of closed blocks are shared across stream frames, so their HTML is too const hit = blockHtmlCache.get(kind) if (hit && hit.gfm === gfm) { this.cr() this.out(hit.html) this.lastOut = "\n" return } const sub = new HtmlWriter(this.options) sub.blockUncached(kind, false, "") const html = sub.buffer blockHtmlCache.set(kind, { gfm, html }) this.cr() this.out(html) this.lastOut = "\n" return } this.blockUncached(kind, tight, prefix) } blockUncached(kind: BlockKind, tight: boolean, prefix: string) { const gfm = this.options?.gfm ?? false switch (kind.type) { case "paragraph": if (tight) { this.out(prefix + renderInlinesToHtml(kind.inlines, this.options)) } else { this.cr() this.out(`

${prefix}${renderInlinesToHtml(kind.inlines, this.options)}

`) this.cr() } return case "heading": this.cr() this.out(`${renderInlinesToHtml(kind.inlines, this.options)}`) this.cr() return case "code": { let cls = "" if (kind.language) { const escaped = escapeHtml(kind.language) cls = ` class="${escaped.startsWith("language-") ? escaped : `language-${escaped}`}"` } const defaultBody = kind.source ? (kind.source.endsWith("\n") && !kind.complete ? kind.source : `${kind.source}\n`) : "" const body = kind.htmlLiteral !== undefined ? kind.htmlLiteral : defaultBody this.cr() this.out(`
${escapeHtml(body)}
`) this.cr() return } case "mermaid": { const defaultBody = kind.source ? (kind.source.endsWith("\n") && !kind.complete ? kind.source : `${kind.source}\n`) : "" const body = kind.htmlLiteral !== undefined ? kind.htmlLiteral : defaultBody this.cr() this.out(`
${escapeHtml(body)}
`) this.cr() return } case "math": this.cr() this.out(`
${escapeHtml(kind.source)}
`) this.cr() return case "blockquote": this.cr() this.out("
") this.cr() for (const child of kind.blocks) this.block(child, false) this.cr() this.out("
") this.cr() return case "list": { const tag = kind.ordered ? "ol" : "ul" const startAttr = kind.ordered && kind.start !== undefined && kind.start !== 1 ? ` start="${kind.start}"` : "" this.cr() this.out(`<${tag}${startAttr}>`) this.cr() for (const item of kind.items) this.item(item, kind.tight) this.cr() this.out(``) this.cr() return } case "table": { this.cr() let out = "\n\n\n" for (let c = 0; c < kind.headers.length; c++) { const align = kind.align[c] out += `${renderInlinesToHtml(kind.headers[c]!, this.options)}\n` } out += "\n\n" if (kind.rows.length > 0) { out += "\n" const alignKey = kind.align.join(",") + ":" + kind.headers.length for (const row of kind.rows) { const hit = rowHtmlCache.get(row) if (hit && hit.gfm === gfm && hit.align === alignKey) { out += hit.html continue } let rowHtml = "\n" for (let c = 0; c < kind.headers.length; c++) { const align = kind.align[c] rowHtml += `${renderInlinesToHtml(row[c] ?? [], this.options)}\n` } rowHtml += "\n" rowHtmlCache.set(row, { gfm, align: alignKey, html: rowHtml }) out += rowHtml } out += "\n" } out += "
" this.out(out) this.cr() return } case "html_block": this.cr() this.out(gfm ? filterDisallowedRawHtml(kind.raw) : kind.raw) this.cr() return case "hr": this.cr() this.out("
") this.cr() return case "group": for (const child of kind.blocks) this.block(child, false) return } } item(item: ListItemNode, tight: boolean) { const checkbox = item.checked === true ? CHECKED : item.checked === false ? UNCHECKED : "" this.out("
  • ") for (let i = 0; i < item.blocks.length; i++) { this.block(item.blocks[i]!, tight, i === 0 ? checkbox : "") } if (item.blocks.length === 0 && checkbox) this.out(checkbox.trimEnd()) this.out("
  • ") this.cr() } } export function renderBlockKindToHtml(kind: BlockKind, options?: HtmlRenderOptions): string { const writer = new HtmlWriter(options) writer.block(kind, false) return writer.buffer } export function renderBlocksToHtml(blocks: ReadonlyArray, options?: HtmlRenderOptions): string { let out = "" for (const b of blocks) { out += options ? renderBlockKindToHtml(b.kind, options) : (b.html ?? renderBlockKindToHtml(b.kind)) } return out }