2025-02-13 14:27:56 -08:00
2025-02-07 19:45:04 +13:00
2024-12-03 13:56:51 +01:00
2025-02-03 02:49:59 +01:00
2025-02-03 02:43:46 +01:00
2024-03-20 18:03:35 +01:00
2024-12-03 13:56:51 +01:00
2024-12-06 16:25:25 +01:00
2024-03-20 16:29:27 +01:00
2024-12-03 13:56:51 +01:00
2025-02-03 15:36:39 +01:00
2024-06-12 20:40:22 +02:00
2025-02-08 02:31:25 +13:00

Markdown Exec

ci documentation pypi version gitter

Utilities to execute code blocks in Markdown files.

For example, you write a Python code block that computes some HTML, and this HTML is injected in place of the code block.

Installation

pip install markdown-exec[ansi]

The ansi extra provides the necessary bits (pygments-ansi-color and a CSS file) to render ANSI colors in HTML code blocks. The CSS file is automatically added to MkDocs' extra_css when Markdown Exec is activated via plugins (see below).

Configuration

This extension relies on the SuperFences extension of PyMdown Extensions.

To allow execution of code blocks, configure a custom fence from Python:

from markdown import Markdown
from markdown_exec import formatter, validator

Markdown(
    extensions=["pymdownx.superfences"],
    extension_configs={
        "pymdownx.superfences": {
            "custom_fences": [
                {
                    "name": "python",
                    "class": "python",
                    "validator": validator,
                    "format": formatter,
                }
                # ...one fence for each language we support:
                # bash, console, md, markdown, py, python, pycon, sh, tree,
                # typescript, ts, tscon
            ]
        }
    }
)

...or in MkDocs configuration file, as a Markdown extension:

# mkdocs.yml
markdown_extensions:
- pymdownx.superfences:
    custom_fences:
    - name: python
      class: python
      validator: !!python/name:markdown_exec.validator
      format: !!python/name:markdown_exec.formatter
    # ...one fence for each language we support:
    # bash, console, md, markdown, py, python, pycon, sh, tree, typescript, ts,
    # tscon

...or in MkDocs configuration file, as a plugin:

# mkdocs.yml
plugins:
- search
- markdown-exec

# SuperFences must still be enabled!
markdown_extensions:
- pymdownx.superfences

We do recommend enabling Markdown Exec with the MkDocs plugin if you are using MkDocs: it will take care of adding relevant assets (CSS/JS) to the final site when needed.

Usage

You are now able to execute code blocks instead of displaying them:

```python exec="on"
print("Hello Markdown!")
```

The exec option will be true for every possible value except 0, no, off and false (case insensitive).

Below you can see an example of running a bash script that is expected to return a non-zero exit code:

```bash exec="1" source="tabbed-left" returncode="2"
grep extra_css README.md && exit 2
```

See usage for more details, and the gallery for more examples!

S
Description
Utilities to execute code blocks in Markdown files.
Readme ISC 458 KiB
Languages
Python 90.7%
CSS 6.3%
JavaScript 2.7%
Makefile 0.3%