3

I define a variable in a pelican article using for instance Markdown syntax:

Motto: _"Paranoia is a virtue"_  Anonymous, 1984

Then I want to use it in a template (ex. article.html) as:

 {% if article.motto %}<p>{{ article.motto }}</p>{% endif %}

I, obviously, obtain in HTML something like:

 <p>_"Paranoia is a virtue"_  Anonymous, 1984</p>

Is there a way to process the variable (f.i. through a Jinja filter) to obtain the text after Markdown processing. In the case the result should be:

 <p><i>"Paranoia is a virtue"</i>  Anonymous, 1984</p>
exedre
  • 31
  • 3

3 Answers3

3

Add the following to your pelicanconf.py file to define a Jinja filter to pass the variable through markdown:

from markdown import Markdown
markdown = Markdown(extensions=['markdown.extensions.extra'])

def md(content, *args):
    return markdown.convert(content)

JINJA_FILTERS = {
    'md': md,
}

Then you can do

{% if article.motto %}<p>{{ article.motto | md }}</p>{% endif %}

and your variable will be properly rendered.

Dan
  • 2,766
  • 3
  • 27
  • 28
2

For 3.5, no. Markdown doesn't process metadata by default. But the current development version includes a new setting, FORMATTED_FIELDS, for that. So, wait for 3.6 or install the dev-version from github.

Of course, you can also use HTML instead of markdown syntax:

Motto: <i>"Paranoia is a virtue"</i>  Anonymous, 1984
Avaris
  • 35,883
  • 7
  • 81
  • 72
0

Does this do the trick?

{% if article.motto %}<p>{{ article.motto | replace("_", "<i>", 1) | replace("_", "</i>" }}</p>{% endif %}
doru
  • 9,022
  • 2
  • 33
  • 43