HTML & Web Rendering
Last Updated March 27, 2026
While Omni-MDX boasts unique native desktop capabilities, its primary use case remains web rendering. If you are building a Python backend (such as FastAPI, Flask, or Django) and need to serve MDX content to a frontend, the HtmlRenderer provides a secure, high-performance solution.
Unlike traditional Markdown-to-HTML converters that rely on complex regular expressions, the HtmlRenderer operates directly on the native Rust AST. This guarantees that the generated HTML is structurally sound and strictly reflects the parsed tree.
Basic HTML Generation
The HtmlRenderer converts a list of MdxNode objects into a clean, minified HTML string. By default, it automatically escapes standard text to prevent Cross-Site Scripting (XSS) vulnerabilities.
Rendering Custom JSX Components
The true power of MDX is the ability to interleave custom React-style components within Markdown. Because your Python backend does not run React, you must define how these components are translated into standard HTML.
You can achieve this by passing a components dictionary to the renderer. Each key is the JSX tag name, and the value is a Python rendering function.
Component Function Signature
A component rendering function must accept two arguments:
node: The currentMdxNodeinstance.ctx: TheRenderContext, which allows you to recursively render children.
Component Fallback
If the AST contains a JSX component that you have not registered in the components dictionary, Omni-MDX will safely fall back to a generic <div>, preserving the properties as data- attributes so they are not lost to the frontend.
For example, an unregistered <CustomCard id="123" /> will render as:<div data-component="CustomCard" data-id="123"></div>
Handling Mathematics (KaTeX)
Omni-MDX is fully equipped for scientific and academic publishing.
By default, the HtmlRenderer initializes with katex=True. This setting tells the engine not to attempt rendering LaTeX into SVG on the server side (which is slow and heavy). Instead, it delegates the rendering to the client.
Formulas are wrapped in specific HTML tags with a data-math attribute containing the raw LaTeX source:
Frontend Hydration
On your frontend (React, Vue, or Vanilla JS), you can then run KaTeX (or MathJax) over these elements to generate the visual equations:
If you prefer to output formulas simply wrapped in standard <code> tags, instantiate the renderer with HtmlRenderer(katex=False).
Functional Shortcut
If you do not need to maintain a persistent renderer instance, you can use the render_html utility function for a quick, one-line conversion: