2020-01-21 06:35:58 +00:00
|
|
|
from .. import registry
|
2022-10-25 04:59:04 +00:00
|
|
|
import markdown as _markdown
|
|
|
|
import bleach
|
|
|
|
from django.utils.html import escape
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
2022-10-25 04:59:04 +00:00
|
|
|
EXTENSIONS = [
|
|
|
|
"pymdownx.magiclink",
|
|
|
|
"pymdownx.details",
|
|
|
|
"pymdownx.emoji",
|
|
|
|
"pymdownx.inlinehilite",
|
|
|
|
"pymdownx.superfences",
|
|
|
|
"pymdownx.tasklist",
|
|
|
|
"markdown.extensions.footnotes",
|
|
|
|
"markdown.extensions.attr_list",
|
|
|
|
"markdown.extensions.def_list",
|
|
|
|
"markdown.extensions.tables",
|
|
|
|
"markdown.extensions.admonition",
|
|
|
|
"pymdownx.arithmatex",
|
|
|
|
]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-10-25 04:59:04 +00:00
|
|
|
ALLOWED_TAGS = bleach.sanitizer.ALLOWED_TAGS + ["img", "center", "iframe"]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-10-25 04:59:04 +00:00
|
|
|
ALLOWED_ATTRS = ["src", "width", "height", "href"]
|
2022-07-30 10:21:08 +00:00
|
|
|
|
2022-07-30 14:01:37 +00:00
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
@registry.filter
|
2022-10-25 04:59:04 +00:00
|
|
|
def markdown(value, hard_wrap=False):
|
|
|
|
extensions = EXTENSIONS
|
|
|
|
if hard_wrap:
|
|
|
|
extensions = EXTENSIONS + ["nl2br"]
|
|
|
|
html = bleach.clean(value, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS)
|
|
|
|
html = _markdown.markdown(html, extensions=extensions)
|
|
|
|
if not html:
|
|
|
|
html = escape(value)
|
|
|
|
return '<div class="md-typeset">%s</div>' % html
|