Skip to main content

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 (like copy-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:

  1. Copies Files: It copies katex.min.css (or your chosen filename), the fonts/ directory, and any specified client_scripts from your local node_modules to site/assets/katex.
  2. 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

  • katex module not found: Ensure npm install katex (or pnpm/yarn) has been run in your project root, or specific the path via katex_dist.
  • Node.js error: The plugin requires node to be in your PATH. On Windows, ensure you can run node --version in your terminal.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mkdocs_katex_ssr-1.0.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mkdocs_katex_ssr-1.0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

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

Hashes for mkdocs_katex_ssr-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a5c74cc2d82de2ec5fc2b1dedfc1853d77e0bf34d98b03f816f571012d34c69f
MD5 144e2536a7463625eac238e8dde785d3
BLAKE2b-256 941d38b14334e110a740bf4e18ff33be3df3cddbd48c1c0d8c6b86420982b80f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mkdocs_katex_ssr-1.0.1.tar.gz:

Publisher: publish.yml on raineblog/mkdocs-katex-ssr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mkdocs_katex_ssr-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_katex_ssr-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a7b214cbaed5d32ca82c5c3ac9809102f95e2cb56a6d48da480b800ae41261e5
MD5 5d1afa378c28169ec178415a0d279f38
BLAKE2b-256 e375010fe957690ca57f4964f11ecb536a6dd5a88a2e2803ba5d1df984aaf718

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page