micromark-extension-llm-math
A micromark extension that parses LaTeX math from LLM output, including backslash-delimited \( \) and \[ \] notation, and renders it to HTML via KaTeX.
Repository Health
Technical Analysis
micromark-extension-llm-math extends the micromark markdown parser with math syntax support, forked specifically to handle the way large language models format LaTeX equations. Where the upstream micromark-extension-math package only recognizes dollar-delimited math ($C_L$, $$...$$), this fork adds parsing for backslash-delimited notation (\(C_L\), \[...\]) that models like GPT and Claude commonly output, without requiring any changes to how the extension is wired into a micromark pipeline.
It exposes the same two-function API as the original — math() for tokenizing and mathHtml() for HTML serialization via KaTeX — so projects can adopt it as a bundler alias (mapping micromark-extension-math to this package) and immediately support both syntaxes side by side.
What You Get
- Two micromark extension factories,
math()for parsing andmathHtml()for HTML serialization, exported from a single ESM entry point. - Support for both
$...$/$$...$$dollar-fenced math and\( \)/\[ \]backslash-delimited math in the same parser. - Full TypeScript types, including augmented
micromark-util-typestoken/compile-data declarations for editor autocompletion. - A
developmentconditional export that loads instrumented dev code vianode --conditions development, separate from the production build.
Common Use Cases
- Enabling math notation in a micromark-based markdown-to-HTML pipeline.
- Aliasing into remark/unified toolchains that already depend on micromark-extension-math.
- Rendering LaTeX equations embedded in LLM-generated markdown responses.
- Building custom markdown renderers for scientific or technical content.
Under The Hood
Architecture
This is a small, flat module: dev/index.js re-exports math() and mathHtml() from sibling files dev/lib/syntax.js and dev/lib/html.js. syntax.js builds a micromark Extension object mapping character codes ($ and \) to tokenizer constructs spread across four files: math-flow.js/math-text.js (the original dollar-based dispatchers, ported from upstream micromark-extension-math) and the fork’s own additions math-tex-flow.js/math-tex-text.js (backslash-delimited \( \)/\[ \] dispatchers). html.js implements a separate HtmlExtension with enter/exit handlers keyed by token name, buffering content and calling into KaTeX’s renderToString at mathFlow/mathText exit. The architecture is dictated almost entirely by micromark’s own tokenizer/compiler contract rather than any internal class hierarchy, and because the tex handlers were added as parallel files alongside the original dollar handlers instead of a unified dispatcher, a change to micromark’s core Construct interface would need to be applied identically across all four math-*.js files.
Tech Stack
Runtime dependencies are katex for rendering plus micromark-factory-space, micromark-util-character, micromark-util-symbol, and micromark-util-types from the micromark ecosystem, alongside devlop for dev-only assertions. The package ships plain ESM (type: module) with a conditional export map (development/default) so consumers can load instrumented dev code separately from the production build. Development tooling includes TypeScript with project-reference builds, type-coverage enforcing full type coverage, the xo/Prettier combination for linting and formatting, micromark-build to compile published output, c8 for coverage enforcement, and remark-cli to lint the README itself. CI runs the test suite across two Node.js versions and uploads coverage to Codecov.
Code Quality
Tests live in a single node:test file exercised twice — once under the development export condition and once under production — asserting exact HTML output including KaTeX-rendered markup, and the suite is wrapped by a coverage tool configured to fail below full coverage. Error handling relies on dev-only assertion helpers that are stripped from the production build, appropriate for a tokenizer construct where malformed input should fall through rather than throw. Naming and types reuse micromark’s own conventions verbatim, and type coverage is enforced at a required threshold on top of hand-written type augmentations for the upstream token map. Linting and formatting are enforced via a strict preset with an embedded style config, all gated in CI.
API Design
The public surface is intentionally tiny — two functions, math() and mathHtml(), mirroring micromark’s own parse/serialize extension-pair convention, so anyone already using micromark-extension-math can adopt this with a single import swap or a zero-code bundler alias. The specific, narrow contribution is recognizing backslash-delimited LaTeX notation that large language models emit by default, without disturbing the original dollar-sign syntax or output format — a real but limited-scope addition rather than a broad architectural departure from its upstream.
Used by 2 apps in this directory
LibreChat
Developer Tools · AI Assistants
Unite every major AI model in one self-hosted chat platform with agents, code execution, MCP tools, and enterprise authentication.
Ollama
AI Development · Developer Tools
Run Llama, Gemma, DeepSeek, and other open LLMs on your own machine with one command and an OpenAI-compatible API.