Skip to main content

shortcode for mkdocs

Templates and Shortcodes

This plugin adds shortcodes and templates to mkdocs. In context, shortcode and templates are:

  • theme: mkdocs already supports themes. Here, the style of the complete website with all its pages is defined.
  • template: A markdown-file in in the docs/-folder can define its template. In this case a special HTML is loaded ad template for that markdown. If you observe magazines for example, there are a limited number of structures of pages, like image on the left side, info-box on the right and so on. Templates are that for mkdocs. Templates require, that you define template-specific blocks, like image-block or infobox-block
  • shortcode: These are the lowest of the reusable components. One shortcode generates multiple HTML-elements. This is useful, if you want to extend the markdown language in an easy way. Examples are blocks where the image is left of the text.

The new directives (think markdown-extensions) are designed in such a way that they are backwards-compatible with markdown. The idea is that they are interpreded as markdown-comments of normal compilers.

Shortcode: Example

Usage

Usage of an shortcode looks like:

... some previous markdown

[start]: with_image (pos="right", src="https://dummyimage.com/400x600/eee/aaa")

## How it works 
1. This is a good point
2. everything is markdown
3. And yet another point

[end]: with_image

... some other markdown continues

Note, that we wrap some content with the shortcode and pass some parameters. The pos argument says, that the image defined in src should be renderd right of the wrapped content. To see, how this is done, see the next section.

Definition

You can create shortcode very easly, by creating a jinja2-html-file under ./shortcodes/ with the name of the shortcode. In this example we defined ./shortcoded/with_image.html:

<div class="row {{class}} mt-5 with-image">
    {% if pos == "left" %}
    <div class="col-md-5">
        <img src="{{src}}" alt="alt" />
    </div>
    <div class="col-md-7">
        {{content}}
    </div>
    {% else %}
    <div class="col-md-7">
        {{content}}
    </div>
    <div class="col-md-2">
        <img src="{{src}}" alt="alt" />
    </div>
    {% endif %}
</div>

Note, that we can use the kwargs-paramters with {{parameter-name}} as usual in jinja2 and there is also the special variable {{content}} which are the lines beteen start and end.

Template: Example

Usage

We can define one template per markdown-file. The template defines blocks, that we can fill if we want. This can look for example like this.

[template]: promo1 (color="red")


[block]: mainimage

![Some image](https://dummyimage.com/800x400/eee/aaa)

[endblock]: mainimage



[block]: content

# Markdown

here comes the normal markdown code. This is wrapped within the content. The whole block gets placed at the proper position in the output by the template

[endblock]: content

This snipped uses the template promo1. Note, that you can use parameters as well. It is important to understand that you only fill out the content of the blocks. The blocks themself are placed by the template at the correct position. There are normally a few templates in a project and many sites use them. If you want to make a consistent change, you have to change only one template (for example move all info-boxes from left to right).

Definition

Defining templates works by creating a jinja2-html-file under the folder ./shortcodes/templates. In this example there is a file ./shortcodes/templates/promo1.html with this content:

<div class="row" style="border: 1px solid {{color or 'white'}}">
    <div class="col-md-12 main-image-container">
        {% block mainimage %}
        {% endblock %}
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        {% block content %}
        {% endblock content %}
    </div>
</div>

You can see that this template just adds the image-block before the content and wraps the content a little bit. Also you can see the usage of the specified parameter color which you can access as a normal variable using jinja2.

Metadata

Release files for mkdocs-shortcode 0.0.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-shortcode 0.0.2
File Size Uploaded
mkdocs-shortcode-0.0.2.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mkdocs-shortcode 0.0.2
File Interpreter ABI Platform
mkdocs_shortcode-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / mkdocs-shortcode-0.0.2.tar.gz

Download URL mkdocs-shortcode-0.0.2.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
320455715f6eca456c5d6ec417db83ebc05475b2b0577610a447898ae6e9d94c
BLAKE2b-256 checksum
How to use checksums
b736499c173036463196bbcfca2fc99924c39df79244a1f550b8bb25dbd355f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

Release files / mkdocs_shortcode-0.0.2-py3-none-any.whl

Download URL mkdocs_shortcode-0.0.2-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2df921cba10ecc66551933d59d6a0a2868c2e7788b98400d2a3012688f09f643
BLAKE2b-256 checksum
How to use checksums
488d1fb674f120f862bfad143b897dc25cd27e5f0dc3abd650bb5063e82c2cdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

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