import type { ElementNode } from 'comark' import katex from 'katex' import { escapeHtml } from 'comark/utils' /** * HTML component render function for math nodes. Renders LaTeX to HTML via KaTeX. * * Import `@comark/plugin-katex/katex.css` for the KaTeX stylesheet. * * @example * ```typescript * import math from '@comark/plugin-katex' * import { Math } from '@comark/plugin-katex/html' * import { createHtmlRenderer } from '@comark/html' * * const renderHtml = createHtmlRenderer({ * plugins: [math()], * components: { Math }, * }) * ``` */ export const Math = ([, attrs]: ElementNode): string => { const content = String(attrs.content ?? '') const isInline = String(attrs.class ?? '').includes('inline') try { const rendered = katex.renderToString(content, { displayMode: !isInline, throwOnError: false, output: 'html', }) return isInline ? `${rendered}` : `