Skip to main content

Snippets for MkDocs

Project description

Mkdocs Snippets Plugin

This plugin allows you to include snippets into your mkdocs documentation.

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!
  • Links in snippets are automatically converted to relative links based on the snippet's location.
    • This allows you to use links in 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 amount 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.

Links in Snippets

Links in snippets are automatically converted to relative links based on where the snippet is used. This allows you to use 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: ">"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mkdocs-snippets-1.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mkdocs_snippets-1.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file mkdocs-snippets-1.2.0.tar.gz.

File metadata

  • Download URL: mkdocs-snippets-1.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for mkdocs-snippets-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c275d1c86755bc845133fd231080931d27586787f125a138000e068a45fca68b
MD5 998b3e85809d4b7d111cfafa8d395455
BLAKE2b-256 8efbdd45df2180b567ce2a4c6d70067e7ad0b69fc1c09d88be27b8c1c8d18b9e

See more details on using hashes here.

File details

Details for the file mkdocs_snippets-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_snippets-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4637b2baac7586397b3eece0365e08f7a005816f4a7e4efd0e2c2cc9be06a1b4
MD5 0c9b5d14645b25a89001406f997fa732
BLAKE2b-256 f985da4cacfd31c2a25b9bbfe56ab177e11a2c3a4997fe0ed4f5c7887fca7325

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page