Skip to main content

Mkdocs Snippets Plugin

This plugin allows you to include snippets in your mkdocs documentation. It is available as a PyPI package.

Features

  • Organize your snippets in a way that makes sense for you:
    • Multiple snippets per file thanks to YAML syntax.
    • Snippets are loaded from any (nested) directory inside the configured snippets_dir.
    • Recursively include snippets in snippets!
  • Documentation links in snippets are automatically converted to relative links based on the snippet's location.
    • This allows you to link to other documentation pages from your snippets without having to worry about the snippet's location.
    • Also works with mike!
  • Snippets preserve the indentation of the snippet call.
    • This allows you to use snippets in lists and code blocks without having to worry about indentation.
  • Customizable snippet syntax.

Installation

pip install mkdocs-snippets

pip install mkdocs-exclude (recommended)

Consider adding the plugins to a requirements.txt file alongside other Python dependencies for your project.

Setup

Add the plugin to your mkdocs.yml file: It is recommended to use the mkdocs-exclude plugin to exclude the snippet directory from the documentation build. This will save you from a lot of warnings about files that are not referenced in the nav configuration.

plugins:
  - search
  - exclude:
      glob:
        - snippets/*
  - snippets

Usage

The plugin will load snippets from the default directory snippets which must be inside your documentation directory. It is allowed to have any number of files and subdirectories inside the snippet directory.

These files must be in the YAML format.

mySnippet: |- 
    This is a snippet.
    It can contain multiple lines.

# |- is the YAML Multiline Indicator
myOtherSnippet: |-
    This is another snippet. 

All top level keys in the snippet file are treated as snippets. Every key is a snippet ID.

Snippet Syntax

The default snippet syntax is @Snippet:snippetFile:snippetID@.

Internal documentation links in snippets are automatically converted to relative links based on where the snippet is used. This allows you to use documentation links in your snippets without having to worry that the snippet might be used in a directory in which the link does not work. Therefore, links in the snippet must be written as if they were in the root of the documentation directory.

Example

Consider the following documentation structure (docs/ is the documentation's root directory):

docs/API/Overview.md

docs/API/Client/Cookbook.md

A snippet would look like this:

mySnippet: |-
    This is a snippet.
    It contains a link to the [API Overview](API/Overview.md).
myOtherSnippet: |-
    This is another snippet.
    It contains a link to the [Client API Cookbook](API/Client/Cookbook.md).

No leading slashes or relative paths are allowed. The file type (.md) must be specified. The snippets can now be used in any file without the links breaking.

Configuration

If you want to customize the snippet syntax or directory, you can do so by adding the following configuration:

plugins:
  - search
  - snippets:
    directory: "mySnippetDirectory"
    delimiter: "|"
    identifier: "snip"
    divider_char: ">"

Contributing

This section is for contributors and maintainers.

Setup Dev Environment

 pip install -r requirements/dev-requirements.txt
 pip install -r requirements/requirements.txt

Releasing

To release a new version of the plugin, follow the steps in the official Python packaging guide.

Dependency Management

To manage dependencies, we use pip-tools to generate requirements.txt/dev-requirements.txt files from requirements.in/dev-requirements.in.

To update your local environment with the latest dependencies, run:

pip-sync requirements/requirements.txt requirements/dev-requirements.txt

Metadata

Release files for mkdocs-snippets 1.3.2

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

Source distribution (sdist)

Source distribution for mkdocs-snippets 1.3.2
File Size Uploaded
mkdocs_snippets-1.3.2.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mkdocs-snippets 1.3.2
File Interpreter ABI Platform
mkdocs_snippets-1.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / mkdocs_snippets-1.3.2.tar.gz

Download URL mkdocs_snippets-1.3.2.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d46d2700dec2dd8dab1d2977a656bed11ad89fdfd4dab30f2008c99d42086c53
BLAKE2b-256 checksum
How to use checksums
3e569a5264a78375443f57fe3043a77f11d5b8edb8fcd7f3385aa00edbad1a3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 May 7, 2025.

Transparency log

Release files / mkdocs_snippets-1.3.2-py3-none-any.whl

Download URL mkdocs_snippets-1.3.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a2be9ced4169acb3449d89a2d882f7e0e516a8c9cf3ffb8b9c6565af83a9a61
BLAKE2b-256 checksum
How to use checksums
0ebf8e63de00e57a05e739782dbcccfe6fad033fa886c361622053198a5b638c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 May 7, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.3.2 This release

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

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