Skip to main content

render_math

Function render_math 

Source
pub fn render_math(src: &str, display: bool) -> String
Expand description

Render one math expression to KaTeX HTML at build time.

display selects block ($$) vs inline ($) layout. On a KaTeX parse error we fall back to an escaped <code> so a bad expression degrades gracefully instead of failing the whole build.

throw_on_error is true here: with it false, KaTeX swallows invalid input and emits its own red error markup (returning Ok), which would never reach our graceful fallback. Letting KaTeX return Err on a genuine parse failure lets us emit a clean escaped <code class="docgen-math-error">.

The error fallback honors display: a failed display ($$) equation is wrapped in a block <div class="katex-display docgen-math-error"> so it still renders as a centered block (matching .katex-display spacing), while inline math degrades to inline <code>. The KaTeX error message is logged to stderr so a malformed expression leaves a build-time diagnostic.