A MkDocs plugin for server-side rendering of KaTeX math.
Project description
MkDocs KaTeX SSR Plugin
A MkDocs plugin that renders LaTeX math on the server side using KaTeX, offering faster load times, layout stability, and optional offline support.
[!NOTE] Maintenance Philosophy: This project is self-maintained. I welcome bug reports and corrections, but please note that I may not have the bandwidth to accept significant feature requests or major functional additions. Pull requests for bug fixes are appreciated.
This project's code and documentation were generated by Antigravity (Google DeepMind) and verified by the maintainer.
Why Server-Side Rendering (SSR)?
Traditional client-side rendering relies on JavaScript in the browser to convert LaTeX strings (e.g., $\sqrt{a^2 + b^2}$) into HTML. This can lead to:
- Layout Shift: Content jumps as math loads.
- Slower Performance: The browser must download and parse the KaTeX library before rendering.
- Dependency: Requires client-side JavaScript execution.
MkDocs KaTeX SSR pre-renders all math during the mkdocs build process using a local Node.js process. The resulting HTML contains ready-to-view markup.
Features
- High Performance: Uses a persistent Node.js process to render equations efficiently.
- Offline Support: Optional "Offline Mode" copies all necessary CSS, fonts, and scripts to your site directory.
- Hybrid Script Management: Separate configuration for server-side processing (e.g.,
mhchem) and client-side interaction (e.g.,copy-tex). - Flexible Rendering: New
disableoption to switch back to traditional client-side rendering while keeping asset management benefits. - Built-in Cache: Includes a SQLite-based cache to speed up recompilation.
Installation
Prerequisites
- Node.js: Must be installed and available in your system PATH.
- Python: 3.8+
Install Plugin
pip install mkdocs-katex-ssr
Configuration
Add the plugin to your mkdocs.yml:
markdown_extensions:
- pymdownx.arithmatex:
generic: true
plugins:
- katex-ssr:
# --- Basic Configuration ---
verbose: true # Enable build logs for each page
disable: false # Set to true to switch to client-side rendering only
add_katex_css: true # Required (true by default)
katex_css_filename: "katex-swap.min.css"
# --- Script Loading ---
ssr_contribs:
- mhchem # Loaded in Node.js for SSR
client_scripts:
- copy-tex # Injected as <script> tag for the browser
# --- KaTeX Options ---
katex_options:
macros:
"\\RR": "\\mathbb{R}"
Client-Side Only Mode (disable: true)
If you prefer to use KaTeX's traditional "Auto-render" extension instead of Server-Side Rendering (SSR), set disable: true.
Why use this?
- Faster builds (skips SSR logic).
- Compatibility with certain client-side plugins that expect raw LaTeX in the DOM.
- Shared configuration: Automatically passes
macros,ssr_contribs, andclient_scriptsto the client-side renderer.
[!IMPORTANT] When
disable: true,add_katex_cssmust betrue. Ifadd_katex_cssis set tofalse, the plugin will return a configuration error.
Offline Mode (Self-Contained)
Enable embed_assets to host all KaTeX files locally within your site.
plugins:
- katex-ssr:
embed_assets: true
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
disable |
bool | false |
If true, skips SSR and uses client-side "Auto-render". |
verbose |
bool | false |
Logs formula counts and processing time per page. |
katex_dist |
str | CDN URL | Base path for KaTeX (URL or local path). |
add_katex_css |
bool | true |
Whether to inject the CSS link tag. Must be true if disable is true. |
katex_css_filename |
str | katex.min.css |
The CSS filename to load. |
embed_assets |
bool | false |
Copies assets to output dir for offline support. |
ssr_contribs |
list | [] |
KaTeX contribs for SSR (or client if disable: true). |
client_scripts |
list | [] |
KaTeX contribs always injected for the browser. |
katex_options |
dict | {} |
Options passed to KaTeX (including macros). |
Troubleshooting
- Validation Error: If you see "When 'disable' is true, 'add_katex_css' must also be true", ensure you haven't set
add_katex_css: falsewhile disabling SSR. - Node.js issues: Verify
nodeis in your PATH.
License
MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mkdocs_katex_ssr-1.0.8.tar.gz.
File metadata
- Download URL: mkdocs_katex_ssr-1.0.8.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1584c2194aa818bf31508adf190e6364bfe0273bfd163a1741f2fc8a541d6e1
|
|
| MD5 |
83d2a76cc5750c8e4daa27f6da6a3338
|
|
| BLAKE2b-256 |
86f7eb9c1de82ec8cb62a83d4837fba2672d5c38bc32c287a54b9f8554409ce2
|
Provenance
The following attestation bundles were made for mkdocs_katex_ssr-1.0.8.tar.gz:
Publisher:
publish.yml on raineblog/mkdocs-katex-ssr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mkdocs_katex_ssr-1.0.8.tar.gz -
Subject digest:
d1584c2194aa818bf31508adf190e6364bfe0273bfd163a1741f2fc8a541d6e1 - Sigstore transparency entry: 926854155
- Sigstore integration time:
-
Permalink:
raineblog/mkdocs-katex-ssr@3e67e5e5c7c5c759a8bda5c2a3b3f025637caf23 -
Branch / Tag:
refs/tags/v1.0.8 - Owner: https://github.com/raineblog
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3e67e5e5c7c5c759a8bda5c2a3b3f025637caf23 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mkdocs_katex_ssr-1.0.8-py3-none-any.whl.
File metadata
- Download URL: mkdocs_katex_ssr-1.0.8-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fdcc264bd416f3772ffbf9847020590fc40f12beae29be025254a29d2ba8422
|
|
| MD5 |
f1aa9b07672930bde66e4948dce9e0b5
|
|
| BLAKE2b-256 |
38c366700b54d9cae9907242c8519fd8dd85a5e1cc1243ffef84705ddde840db
|
Provenance
The following attestation bundles were made for mkdocs_katex_ssr-1.0.8-py3-none-any.whl:
Publisher:
publish.yml on raineblog/mkdocs-katex-ssr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mkdocs_katex_ssr-1.0.8-py3-none-any.whl -
Subject digest:
6fdcc264bd416f3772ffbf9847020590fc40f12beae29be025254a29d2ba8422 - Sigstore transparency entry: 926854156
- Sigstore integration time:
-
Permalink:
raineblog/mkdocs-katex-ssr@3e67e5e5c7c5c759a8bda5c2a3b3f025637caf23 -
Branch / Tag:
refs/tags/v1.0.8 - Owner: https://github.com/raineblog
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3e67e5e5c7c5c759a8bda5c2a3b3f025637caf23 -
Trigger Event:
push
-
Statement type: