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 without spawning a new process for every item.
- Offline Support: Optional "Offline Mode" copies all necessary CSS, fonts, and scripts to your site directory, removing external CDN dependencies.
- Smart Asset Management: Separate configuration for server-side processing scripts (like
mhchem) and client-side interactive scripts (likecopy-tex). - Clean Output: Aggressive warning suppression for a quieter build log.
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 ---
katex_dist: "https://cdn.jsdelivr.net/npm/katex@latest/dist/"
add_katex_css: true
katex_css_filename: "katex-swap.min.css" # Use swap version for better font-display behavior
# --- Script Loading ---
# Scripts needed for rendering (Server-Side). e.g., chemical formulas.
# These are NOT sent to the browser.
ssr_contribs:
- mhchem
# Scripts needed for interaction (Client-Side). e.g., clipboard copy.
# These ARE injected into the HTML.
client_scripts:
- copy-tex
# --- KaTeX Options ---
katex_options:
macros:
"\\RR": "\\mathbb{R}"
Offline Mode (Self-Contained)
If you need your documentation to work without an internet connection (or just want to host everything yourself), enable embed_assets.
plugins:
- katex-ssr:
embed_assets: true
# You can specify a local path if auto-detection fails
# katex_dist: "./node_modules/katex/dist/"
What this does:
- Copies Files: It copies
katex.min.css(or your chosen filename), thefonts/directory, and any specifiedclient_scriptsfrom your localnode_modulestosite/assets/katex. - Relative Linking: It updates all HTML citations to point to these local files using correct relative paths (e.g.,
../assets/katex/katex.min.css).
Advanced Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
katex_dist |
str | jsDelivr | Base URL for CDN, or local file path to KaTeX distribution. |
add_katex_css |
bool | true |
Whether to inject the CSS link tag. |
katex_css_filename |
str | katex.min.css |
The specific CSS file to load. katex-swap.min.css is recommended. |
embed_assets |
bool | false |
If true, copies assets to output dir and links locally. |
ssr_contribs |
list | [] |
List of KaTeX contrib libraries to load in the Node.js renderer (e.g., mhchem). |
client_scripts |
list | [] |
List of KaTeX contrib libraries to load in the browser (e.g., copy-tex). |
copy_assets_to |
str | assets/katex |
Destination directory for copied assets (relative to site_dir). |
katex_options |
dict | {} |
Standard options passed to katex.renderToString (macros, etc.). |
Troubleshooting
katexmodule not found: Ensurenpm install katex(orpnpm/yarn) has been run in your project root, or specific the path viakatex_dist.- Node.js error: The plugin requires
nodeto be in your PATH. On Windows, ensure you can runnode --versionin your terminal.
License
This project is licensed under the 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.1.tar.gz.
File metadata
- Download URL: mkdocs_katex_ssr-1.0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5c74cc2d82de2ec5fc2b1dedfc1853d77e0bf34d98b03f816f571012d34c69f
|
|
| MD5 |
144e2536a7463625eac238e8dde785d3
|
|
| BLAKE2b-256 |
941d38b14334e110a740bf4e18ff33be3df3cddbd48c1c0d8c6b86420982b80f
|
Provenance
The following attestation bundles were made for mkdocs_katex_ssr-1.0.1.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.1.tar.gz -
Subject digest:
a5c74cc2d82de2ec5fc2b1dedfc1853d77e0bf34d98b03f816f571012d34c69f - Sigstore transparency entry: 869301024
- Sigstore integration time:
-
Permalink:
raineblog/mkdocs-katex-ssr@afefd5dafb5915ced2d14141ec85fdd98fb048d6 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/raineblog
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@afefd5dafb5915ced2d14141ec85fdd98fb048d6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mkdocs_katex_ssr-1.0.1-py3-none-any.whl.
File metadata
- Download URL: mkdocs_katex_ssr-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.5 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 |
a7b214cbaed5d32ca82c5c3ac9809102f95e2cb56a6d48da480b800ae41261e5
|
|
| MD5 |
5d1afa378c28169ec178415a0d279f38
|
|
| BLAKE2b-256 |
e375010fe957690ca57f4964f11ecb536a6dd5a88a2e2803ba5d1df984aaf718
|
Provenance
The following attestation bundles were made for mkdocs_katex_ssr-1.0.1-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.1-py3-none-any.whl -
Subject digest:
a7b214cbaed5d32ca82c5c3ac9809102f95e2cb56a6d48da480b800ae41261e5 - Sigstore transparency entry: 869301026
- Sigstore integration time:
-
Permalink:
raineblog/mkdocs-katex-ssr@afefd5dafb5915ced2d14141ec85fdd98fb048d6 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/raineblog
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@afefd5dafb5915ced2d14141ec85fdd98fb048d6 -
Trigger Event:
push
-
Statement type: