Using Markdown in shortcodes body

Is there a way to parse shortcode body as Markdown? For example with the quote shortcode from the doc :

<blockquote>
    {{ body }} <br>
    -- {{ author}}
</blockquote>
{% quote(author="Vincent") %}
A **markdown** quote
{% end %}

Could that be achieved with a markdown filter in Tera?

{{ body | markdown }}
1 Like

There is a markdown filter in Zola: https://www.getzola.org/documentation/templates/overview/#markdown

1 Like

Thank you! I thought I already read through the whole Zola and Tera documentation, but apparently not. Sorry for the “go to RTFD.io” question :smiley:

I still didn’t get it after following that link (or reading through the shortcodes doc several times), so I’m leaving this for anyone else who is having a hard time with it. (caveat: I’m not a programmer, but I’m managing to muddle through, so I’m sure this explanation is overkill for most on this forum, but this is the post that came up when I was searching for an answer.)

In order to get markdown to work in a shortcode, I had to add safe to the end of the {{ body }} in the shortcode file.

For example, I have a very simple shortcode to allow for different styles of messages/callouts, styled by css class. In an .md file (like a blog post), I reference it by inserting :

{% callout(class="warning") %}
    Message text goes here. It can use markdown features like **bold text**.
{% end %}

Then in templates/shortcodes I have a file named callout.html that contains:

<div class="message" {% if class %}class="{{class}}"{% endif %}>
    {{ body | markdown | safe }}
</div> 

It works just like normal markdown, so you can have multiple paragraphs, text formatting, links, etc.

3 Likes

Thanks for the tips, {{ body | markdown | safe }} got markdown working, but shortcode still doesn’t work, that is if I have another shortcode and what to use it in the body, it will be rendered as raw {{ short_code_name ... }}.

Same problem here. Shortcode in yellowBox.html is:

<p class="yellowBox">
   {{ body | markdown | safe }}
</p>

…with the CSS class putting it in a yellow box, as you can guess. When I try it in Markdown…

{% yellowBox %}
  **Hello**, this should be in a yellow box.
{% end %}

I get the following HTML:

<p>
   {% yellowBox %}
<strong>Hello</strong>, this should be in a yellow box. {% end %}
<p>

…while it should be:

<p class="yellowBox"><strong>Hello</strong>, this should be in a yellow box.</p>

Is this a known and continuing bug, given the age of this thread?

Try

{% yellowBox() %}
  **Hello**, this should be in a yellow box.
{% end %}

with the (), otherwise it’s not recognized as a shortcode

1 Like

@keats Thanks very much! Only remaining glitch was that it didn’t care for my using <p> rather than <div>, but that obviously was an easy fix.