UNPKG

myst-to-html

Version:
23 lines (22 loc) 960 B
import { toHTML } from './utils.js'; export const renderMath = (math, block, target) => { const { id, number } = target ?? {}; const [html] = toHTML([ block ? 'div' : 'span', { class: target ? ['math', 'numbered'] : 'math', id, number, children: block ? `\\[\n${math}\n\\]` : `\\(${math}\\)`, }, ], { inline: true }); return block ? `${html}\n` : html; }; export function addMathRenderers(md) { const { renderer } = md; renderer.rules.math_inline = (tokens, idx) => renderMath(tokens[idx].content, false); // Note: this will actually create invalid HTML renderer.rules.math_inline_double = (tokens, idx) => renderMath(tokens[idx].content, true); renderer.rules.math_block = (tokens, idx) => renderMath(tokens[idx].content, true); renderer.rules.math_block_label = (tokens, idx) => renderMath(tokens[idx].content, true, tokens[idx].meta?.target); }