Search code examples
htmldjangomarkdownmarkupdjango-markdown-deux

django markdownx isn't allowing normal html tags


I added django-markdownx to my project. Everything is working fine with this custom filter:

import markdown
@register.filter
def markdownify(text):
    return markdown.markdown(text, safe_mode='escape')

This is converting markdown to HTML but if the file contains normal HTML it will convert the markdown successfully but not the normal HTML.

This is how I use the filter:

{{ Post.body|markdownify|safe|linebreaks }}

Solution

  • You are passing safe_mode='escape' to markdown.markdown, which tells the Markdown parser to "escape" raw HTML in the Markdown text. Remove the safe_mode parameter and raw HTML will be preserved in your output:

    import markdown
    @register.filter
    def markdownify(text):
        return markdown.markdown(text)