0

I'm using django-pygmentify package in order to highlight my code blocks in my Django templates. The thing is that this package only supports code blocks as input. I have a model field that keeps markdown data. This markdown content might contain code blocks. (using ``` symbol)

Now, how can I highlight its inner code blocks??

Imagine I have a field that only contains source code. Like:

print('Hey..!')

In that case, this one works properly.

{% load pygmentify_tags %}
...
{% pygmentify %}
{{post.code}}
{% endpygmentify %}

Imagine my field contains the following content.

## Hello
This is my first step working with Python.
```python
print('Hey..!')
‍```

In this case, how can I implement it?? I can render that whole markdown content with {{post.body|markdown|safe}}, but how can I highlight those code blocks?? I also want to give all those code blocks a class name .code-block for better styling. Should I create a custom template tag?

Sadra
  • 167
  • 1
  • 9

1 Answers1

0

You may use html standard <code> tag like this:

{% load pygmentify_tags %}
...
{% pygmentify %}
<code>
{{post.code}}
</code>
{% endpygmentify %}

This will separate the code section and at the same time will apply pygmentify to it.

  • As I mentioned in the question, my problem is to highlight the code inside the markdown content. Thanks btw. – Sadra Dec 03 '21 at 19:24