An small extension for Python's Markdown library that
hides code blocks marked with hide
from the markdown output. This is useful
when developers want to include unit tests in their markdown documentation (e.g., using pytest-markdown-docs) but don't want those tests to be visible to documentation readers. I created this extension while developing Pyoframe.
```python {hide}
# This code block will be hidden in the rendered markdown.
```
pip install markdown-hide-code
Usage with mkdocs
Add the extension to your mkdocs.yml
configuration:
markdown_extensions:
...
- pymdownx.superfences
- attr_list
- markdown_hide_code # must appear after superfences and attr_list
...
Warning
pymdownx.superfences
and attr_list
are required dependencies and must be listed BEFORE markdown-hide-code
.
Then, to hide a code block simply add {hide}
:
```python {hide}
# This code block will be hidden in the output
```
Usage directly with the Markdown library
Just add the extension to the list. Order matters (see above warning).
from markdown import Markdown
md = Markdown(extensions=["pymdownx.superfences", "attr_list", "markdown_hide_code"])
...
Clone the repo and run uv sync
and pre-commit install
to get setup. The core code is all found in src/markdown_hide_code/main.py
.