Skip to content

staadecker/markdown-hide-code

Repository files navigation

markdown-hide-code

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.
```

Install

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"])
...

Notes for contributors (and myself)

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.

About

Hide code blocks from markdown output. Useful for including tests directly in your documentation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages