Skip to main content

A MkDocs plugin that injects the mkdocs.yml extra variables into the markdown template

Project description

mkdocs-markdownextradata-plugin

Build Status

A MkDocs plugin that injects the mkdocs.yml extra variables into the markdown template

usecase

As a user with variables that need to be inserted at the markdown level, not the template level.
I need a mkdocs plugin that will inject my `extras` variables into the markdown template before it gets rendered to html.
So that I can build my markdown pages with different values for images, urls, client_names, etc. 

Installation

Note: This package requires MkDocs version 0.17 or higher.

Install the package with pip:

pip install mkdocs-markdownextradata-plugin

Enable the plugin in your mkdocs.yml:

plugins:
    - search
    - markdownextradata: {}

You are then able to use the mkdocs extra: {} hash to pass context data into your files

Note: If you have no plugins entry in your config file yet, you'll likely also want to add the search plugin. MkDocs enables it by default if there is no plugins entry set, but now you have to enable it explicitly.

Features

Use Extra Variables in your markdown files

The variables you define in the mkdown.yml extra: slot will become available in your templates

site_name: My fantastic site

plugins:
    - search
    - markdownextradata

extra:
  customer:
    name: Your name here
    web: www.example.com
    salt: salt.example.com

and then in your *.md files

{{ customer.name }}
<a href="{{ customer.web }}">{{ customer.web }}</a>

Using external data files

If the extra: {} hash is not enough for your data then you are able to make use of external yaml files to provide that context data

plugins:
    - search
    - markdownextradata:
        data: path/to/datafiles

or if you have multiple locations provide a comma (,) separated list of locations

plugins:
    - search
    - markdownextradata:
        data: path/to/datafiles, another/path/to/datafiles

if you leave markdownextradata.data empty

plugins:
    - search
    - markdownextradata

by default it will search in the folder where your mkdocs.yml is kept and in the docs folder for another folder called _data

i.e. ./docs/_data/site.yaml - available as '{{ site.whatever_variable_in_the_yaml}}'

If this path is found, the plugin will read all .yml|.yaml and .json files inside it and add the data in them, to the template context.

If inside your data folder you have a directory and a file file called [path/to/datafiles/]sections/captions.yaml - where [path/to/datafiles/] is the path in your configuration - the data inside that file will be available in your templates as sections.captions.whatever_variable_in_the_yaml.

Jinja2 Template Engine Configuration

You may provide Jinja2 configuration as plugin options:

plugins:
    - markdownextradata:
        jinja_options:
          comment_start_string: __CUSTOMCOMMENTSTART__

The above example will make it so that instead of {#, the template engine will interpret __CUSTOMCOMMENTSTART__ as comment start delimiter. This is useful in cases where you write Markdown that contains Jinja-like syntax that's colliding with the template engine. Alternatively, it lets you control what the variable delimiter is (instead of the default {{ }}).

Testing

virtualenv venv -p python3.7
source venv/bin/activate
python setup.py test
pytest test

Contributing

From reporting a bug to submitting a pull request: every contribution is appreciated and welcome. Report bugs, ask questions and request features using Github issues. If you want to contribute to the code of this project, please read the Contribution Guidelines.

Contributors

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-markdownextradata-plugin-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file mkdocs-markdownextradata-plugin-0.2.0.tar.gz.

File metadata

  • Download URL: mkdocs-markdownextradata-plugin-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for mkdocs-markdownextradata-plugin-0.2.0.tar.gz
Algorithm Hash digest
SHA256 33815cc7f14774dbceb9de51b41890f28e8d46d47ccb36d2419554b560f55740
MD5 e66ffae24856b69634897979764ddee5
BLAKE2b-256 b3e52a92cd5f2560e80248ba47416103494fe3c3f2827600b7c22d5ff83c57b4

See more details on using hashes here.

Provenance

File details

Details for the file mkdocs_markdownextradata_plugin-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mkdocs_markdownextradata_plugin-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for mkdocs_markdownextradata_plugin-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfac29396b0dcc502ec47e270fb9fd6ef9831666d62771a93a6ce6dcac953a2e
MD5 0724324e1e177fee2eb9fe5f299df5d7
BLAKE2b-256 75c8ec6947e6e73345cface0bc7f6c7dd4c891d2e4710b14e26b957b26e3df49

See more details on using hashes here.

Provenance

Supported by

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