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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As I mentioned in the question, my problem is to highlight the code inside the markdown content. Thanks btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.