MkDocs¶
MkDocs can render ANSI-colored terminal output using Click Extra’s Pygments lexers. Without this, raw escape codes show up as garbage text in documentation.
Important
For these helpers to work, you need to install click_extra’s additional dependencies from the mkdocs extra group:
$ uv pip install 'click-extra[mkdocs]'
Setup¶
Once Click Extra is installed, enable the plugin in your mkdocs.yml:
mkdocs.yml¶markdown_extensions:
- pymdownx.highlight:
use_pygments: true
- pymdownx.superfences
plugins:
- click-extra
The plugin patches pymdownx.highlight’s formatter classes to use AnsiHtmlFormatter, the same way the Sphinx integration patches PygmentsBridge. Compound tokens like Token.Ansi.Bold.Cyan are decomposed into individual CSS classes. Because MkDocs (unlike Sphinx) never regenerates a Pygments stylesheet from the active formatter, the plugin also generates one covering the 256-color indexed palette and all SGR text attributes, and registers it through extra_css so every page picks up the colors automatically.
ANSI shell sessions¶
Use Click Extra’s ansi- prefixed lexers as the language identifier in fenced code blocks. The lexer names map directly to Pygments IDs registered via entry points, so MkDocs picks them up automatically.
For terminal sessions with colored output, ansi-shell-session is the most common:
```ansi-shell-session
$ my-cli --help
[1mUsage:[0m [97mmy-cli[0m [36m[2m[OPTIONS][0m [36m[2mCOMMAND[0m [36m[2m[ARGS][0m...
Manage recipes and shopping lists.
[1mOptions:[0m
[36m--name[0m [36m[2mTEXT[0m Your name.
[36m--help[0m Show this message and exit.
```
With the plugin enabled, that source renders in full color:
$ my-cli --help
Usage: my-cli [OPTIONS] COMMAND [ARGS]...
Manage recipes and shopping lists.
Options:
--name TEXT Your name.
--help Show this message and exit.
Every other language works the same way: see the full list of ANSI lexer variants, and lexers usage for a Python console session.
mkdocs-click integration¶
mkdocs-click auto-generates CLI reference pages from Click command objects. If your CLI emits ANSI-styled help text (through Rich, Click Extra, or custom HelpFormatter subclasses), mkdocs-click captures the escape codes verbatim and injects them into the page. Without an ANSI-aware formatter, those codes render as garbled text:
[1mbump-my-version[0m [[1;36mOPTIONS[0m] [1;36mCOMMAND[0m [[1;36mARGS[0m]...
When the click-extra plugin is enabled (as shown in Setup) and mkdocs-click is installed, the plugin automatically patches mkdocs-click’s code-block generators to use the ansi-output lexer instead of plain text. The usage and options blocks then render with proper ANSI colors.
The patch needs mkdocs-click 0.8.1 or newer. An older release writes these blocks behind a bare fence, which the plugin leaves as is.