This release is a pre-release and may not be stable for production use.
markdown-code-snippet
Inject snippets of files or Python code into Markdown files.
Install
In your .pre-commit-hooks.yaml:
repos:
- repo: https://github.com/confarg/markdown-code-snippet
rev: v0.0.1.dev1
hooks:
- id: markdown-code-snippet
You can also install it as a project dependency:
uv add --dev markdown-code-snippet
repos:
- repo: local
hooks:
- id: markdown-code-snippet
entry: uv run markdown-code-snippet
language: system
Inject code snippets into Markdown
The tool works by reading instructions provided in HTML comments placed before a code block. The content of the code block will be overwritten during each pre-commit run.
To include a whole file:
<!-- snippet: path/to/config.yaml -->
```yaml
db:
port: 5432
```
To include a specific class:
<!-- snippet: path/to/foo.py#Foo -->
```python
@dataclass
class Foo:
id: int
```
Note that class decorators, if any, are included in the snippet.
To include a specific method:
<!-- snippet: path/to/bar.py#Bar.get_value -->
```python
def get_value(self):
return self.value
```
Note that code snippets are dedented by default. Add dedent=false to the info string to disable it.
To include a specific top-level definition:
<!-- snippet: path/to/baz.py#BAZ -->
```python
BAZ: int = 42
```
Finally, if you want to copy-paste a random snippet that cannot be expressed as a single class, function or definition, you can opt to modify your code specifically for this purpose and add anchors, like so:
# [snippet: increase_qux]
qux = some_func()
qux += 1 # always increase qux by 1
# [/snippet]
You can then specify the snippet the usual way
<!-- snippet: path/to/qux.py#increase_qux -->
```python
qux = some_func()
qux += 1 # always increase qux by 1
```
Paths
Relative paths are relative to the Markdown file that carries the annotation. A leading /
means the project root: the nearest directory above with a pyproject.toml, .git, or .jj,
or whatever directory --root names:
# docs/guide/install.md
<!-- snippet: config.yaml --> -> docs/guide/config.yaml
<!-- snippet: ../shared/a.yaml --> -> docs/shared/a.yaml
<!-- snippet: /src/app.py#main --> -> <project root>/src/app.py
Why this tool?
I was looking for a tool that copies parts of my code into my Markdown files, so that documentation always stays up-to-date. I wanted the committed Markdown to be functional, so that browsing GitHub would provide all the information and, at the same time, be usable by MkDocs or Zensical. Therefore I needed a tool where:
- the copy of code snippets happens during pre-commit;
- the copy instructions are invisible during Markdown rendering;
- the info string (the text after the opening code fence) stays in the hands of the user, e.g. to add
title:information.
Metadata
Release files for markdown-code-snippet 0.0.1.dev2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| markdown_code_snippet-0.0.1.dev2.tar.gz | 21.8 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| markdown_code_snippet-0.0.1.dev2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 53.2 kB
Release files / markdown_code_snippet-0.0.1.dev2.tar.gz
| Download URL | markdown_code_snippet-0.0.1.dev2.tar.gz |
|---|---|
| Size | 21.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7117e6ee8b71695a7d24ad63624d6eaae65e0f82b093c2bc479ed9fcc5089f92
|
|
BLAKE2b-256 checksum How to use checksums |
e1ccace1b16d724fc84fee751b79afa9e3bb25ebaf6cd89b852552188bdfba5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 4, 2026.
Transparency logRelease files / markdown_code_snippet-0.0.1.dev2-py3-none-any.whl
| Download URL | markdown_code_snippet-0.0.1.dev2-py3-none-any.whl |
|---|---|
| Size | 31.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b749597e32f99d81528a09857e54ccf8c373549bcfb0fa3f5b8f602a042c4f9
|
|
BLAKE2b-256 checksum How to use checksums |
f4666d2fb685aac786e5a3a5eedb90985656f7f0999f89697f20d329dae50bdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 4, 2026.
Transparency logSecurity for markdown-code-snippet 0.0.1.dev2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for markdown_code_snippet-0.0.1.dev2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 4, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.