msoucy.me

Code, games, and sarcasm
Matt Soucy

Extending Pelican with Markdown

in code by Matt Soucy - Comments

Recently, I noticed a preview of a blog post. In it, there were some custom embedded macros, that looked like:

{{< mastodon "https://mastodon.social/@msoucy/99825585254720557" >}}

These links were becoming expanded to a full Mastodon embed block:

<iframe class="mastodon-embed" src="https://mastodon.social/@msoucy/99825585254720557/embed" style="max-width: 100%; border: 0" width="400"></iframe>
<script async="async" src="https://mastodon.social/embed.js"></script>

This syntax turned out to be Hugo syntax called a "shortcode".

Naturally, this seemed like a very handy thing to have on my blog. I could think of another, similar, use for embedding YouTube videos, with the same syntax.

That's why I started to play with how Pelican parses Markdown. As it turns out, there's a way to add hooks to the Markdown parser. Based on a similar project for a QR code generator, I came up with a simple extension file called mdx_snippets.

Using it, I'm able to embed a snippet using the same syntax. The actual controller for it is written in raw python, and configuration is simple.

It's not a complete implementation of Hugo's shortcodes, but it's enough that I can implement the original motivating factors.