/** * @module research/agents/markdown-to-html * @description Converts Markdown to HTML with Prism.js syntax highlighting. */ import { marked } from "marked"; import { highlightCode } from "./prism"; import { encode, decode } from "html-entities"; // Inline onclick: self-contained per button, works in dangerouslySetInnerHTML contexts const COPY_ONCLICK = [ "(function(b){", "var c=b.closest('figure.code-block')?.querySelector('pre code');", "if(!c)return;", "navigator.clipboard.writeText(c.innerText).then(function(){", "var t=b.textContent;b.textContent='Copied!';", "setTimeout(function(){b.textContent=t},2000)", "})", "})(this)", ].join(""); // Configure marked once at module load — stacking use() calls would duplicate extensions marked.use({ breaks: true, gfm: true, async: true }); marked.use({ renderer: { code({ text, lang }) { const language = lang || "plaintext"; let highlighted: string; try { const result = highlightCode(text, language); highlighted = result ?? encode(text); } catch (e) { highlighted = encode(text); } return `
${highlighted}
`; }, }, }); /** * Convert markdown text to HTML with Prism.js syntax highlighting. * Unescapes HTML entities like `&` → `&`. */ export async function convertMarkdownToHTMLEscaped( markdown: string, ): Promise { return (await marked.parse(decode(markdown))).trim(); }