bornhack-website/src/utils/templatetags/commonmark.py

31 lines
1,012 B
Python
Raw Normal View History

import commonmark
import bleach
from django import template
2016-08-15 07:16:07 +00:00
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
register = template.Library()
def parse_commonmark(value):
parser = commonmark.Parser()
renderer = commonmark.HtmlRenderer()
ast = parser.parse(value)
return mark_safe(renderer.render(ast))
@register.filter(is_safe=True)
@stringfilter
def trustedcommonmark(value):
2019-01-22 07:57:24 +00:00
"""Returns HTML given some commonmark Markdown. Also allows real HTML, so do not use this with untrusted input."""
linkified_value = bleach.linkify(value, parse_email=True)
return parse_commonmark(linkified_value)
@register.filter(is_safe=True)
@stringfilter
def untrustedcommonmark(value):
2019-01-22 07:57:24 +00:00
"""Returns HTML given some commonmark Markdown. Cleans actual HTML from input using bleach, suitable for use with untrusted input."""
linkified_value = bleach.linkify(bleach.clean(value), parse_email=True)
return parse_commonmark(linkified_value)