Server-Side Math Rendering
Relevant source files
The following files were used as context for generating this wiki page:
The Server-Side Math Rendering (SSR) system in the WHK project is designed to pre-render LaTeX mathematical formulas and chemical equations into static HTML during the build process. This approach replaces the traditional client-side rendering methods, ensuring that complex scientific notations are displayed immediately upon page load without requiring the user's browser to execute heavy JavaScript for rendering.
The system is primarily implemented as a Pandoc JSON AST filter and an MkDocs plugin, leveraging the KaTeX library. It specifically supports standard mathematical notation, chemical formulas via the mhchem extension, and physical units, which are critical for the project's focus on high school science subjects.
Sources: README.md:65-75, filters/katex_ssr.py:1-15
Architecture and Data Flow
The SSR process integrates into the document transformation pipeline, typically acting between the Markdown parsing and the final HTML generation phases. It utilizes a Python-based filter to intercept Pandoc's Abstract Syntax Tree (AST), identifies math elements, and delegates the actual rendering to a high-performance JavaScript runtime.
Rendering Pipeline
The rendering flow involves several stages:
- AST Parsing: The system reads the Pandoc JSON AST from standard input.
- Extraction: It traverses the AST to find all
Mathelements (bothInlineMathandDisplayMath). - Batch Processing: To optimize performance, all identified formulas are collected into a batch and passed to a Node.js/Bun-based renderer.
- KaTeX Execution: The
bunruntime executes a specialized JavaScript renderer that uses the KaTeX library to convert LaTeX strings into HTML spans. - AST Replacement: The Python filter replaces the original
Mathnodes in the AST withRawInlinenodes containing the rendered HTML.
The diagram shows the transition from raw LaTeX in the Pandoc AST to pre-rendered HTML fragments.
Sources: filters/katex_ssr.py:136-215
Key Components
Pandoc Filter (katex_ssr.py)
This Python script serves as the bridge between the document build system and the KaTeX renderer. It includes an embedded JavaScript renderer and logic to handle the bun runtime execution.
| Function | Description |
|---|---|
walk_ast | Recursively traverses the JSON structure to find elements with type Math. |
_render_formulas_with_bun | Spawns a subprocess using bun or bunx to render a list of formulas. |
transform_ast | Replaces the identified math elements with RawInline HTML content. |
_load_katex_config | Loads macros and configuration from an external YAML file. |
Sources: filters/katex_ssr.py:80-134
Embedded JavaScript Renderer
The filter contains a hardcoded JavaScript block (KATEX_RENDERER_JS) that is written to a temporary .mjs file during execution. This script imports katex, reads a JSON input of formulas, and outputs the rendered HTML results.
Sources: filters/katex_ssr.py:27-56
Configuration and Customization
The system supports extensive customization through macros and configuration files, allowing the project to define shorthand for frequently used mathematical and chemical symbols.
Global Macros
Macros are defined in the project configuration (e.g., scripts/template.serve.yml) or a dedicated katex_config.yml. These include support for:
- Standard Sets: $\RR$, $\mathbb{C}$, $\mathbb{Q}$, etc.
- Physics/Chemistry Units: $\pu{1kg}$, $\ce{H2O}$.
- Custom Operators:
\rank,\poly,\pH. - Unicode Mapping: Handling Chinese punctuation marks (like
(,),【) within math environments by mapping them to standard LaTeX equivalents.
Sources: scripts/template.serve.yml:32-75, filters/katex_ssr.py:59-70
Integration with MkDocs
In the development environment, the katex-ssr plugin can be toggled. While full SSR is used for production builds to generate static HTML, the dev server might use a "swap" mode where CSS is injected but rendering is handled differently to speed up the preview.
The sequence shows the inter-process communication between Python and the Bun runtime to achieve server-side rendering.
Sources: filters/katex_ssr.py:91-134, scripts/template.serve.yml:25-30
Dependencies and Requirements
The system relies on specific environment tools to function correctly:
- Bun/Bunx: Used as the high-speed JavaScript runtime for executing the KaTeX renderer.
- KaTeX NPM Package: Must be available in the environment for the Bun runtime to import.
- Pandoc: The underlying engine providing the AST that the filter processes.
- PyYAML: Required by the Python filter to parse configuration files.
Sources: pyproject.toml:15-25, filters/katex_ssr.py:73-78, README.md:115-125
Layout and Styling
Rendered math is wrapped in specific CSS classes to ensure consistent layout. Inline math is wrapped in <span class="math math-inline">, and display math maintains its block-level characteristics. The project also utilizes arithmatex extensions and specific CSS overrides to prevent overflow and manage spacing.
.md-typeset div.arithmatex {
overflow: hidden !important;
}
.md-typeset .math.inline {
margin-right: auto;
float: none;
}
Sources: overrides/partials/integrations/head.html:43-52, filters/katex_ssr.py:165-170
The Server-Side Math Rendering system is a cornerstone of the WHK project's performance, ensuring that math-heavy scientific content is accessible and rendered with high quality without client-side overhead.