Skip to main content
Pre-release

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 or assignment:

<!-- snippet: path/to/baz.py#BAZ -->

```python
BAZ: int = 42
```

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

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markdown-code-snippet 0.0.1.dev1
File Size Uploaded Provenance
markdown_code_snippet-0.0.1.dev1.tar.gz 19.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for markdown-code-snippet 0.0.1.dev1
File Interpreter ABI Platform
markdown_code_snippet-0.0.1.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 48.2 kB

Release files / markdown_code_snippet-0.0.1.dev1.tar.gz

Download URL markdown_code_snippet-0.0.1.dev1.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d6add361982d1dd48652d84dcdc2a3ddd6aedc641c5598f01940e717e46fd37c
BLAKE2b-256 checksum
How to use checksums
480fceafb03ea1c9f52433ee466f13d7486718143d5893df19ef323736c1174c
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 Sep 27, 2026.

Transparency log

Release files / markdown_code_snippet-0.0.1.dev1-py3-none-any.whl

Download URL markdown_code_snippet-0.0.1.dev1-py3-none-any.whl
Size 28.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd4ec9df69a83eb06bf50284f777f97da0ad5fd626ff0f7395b44c189be1641f
BLAKE2b-256 checksum
How to use checksums
14fb2a5005cccb4c189d16364766a14859508b5430d6da8be1bb92030466a52b
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 Sep 27, 2026.

Transparency log

Security for markdown-code-snippet 0.0.1.dev1

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.

Select from 2 files

Attestation for markdown_code_snippet-0.0.1.dev1.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 Sep 27, 2026.

Transparency log

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

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page