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",
|
2022-10-25 23:07:45 +00:00
|
|
|
"pymdownx.betterem",
|
2022-10-25 04:59:04 +00:00
|
|
|
"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",
|
2022-10-25 23:10:33 +00:00
|
|
|
"nl2br",
|
2022-10-29 04:28:31 +00:00
|
|
|
"mdx_breakless_lists",
|
2022-10-25 04:59:04 +00:00
|
|
|
]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-10-25 23:07:45 +00:00
|
|
|
ALLOWED_TAGS = bleach.sanitizer.ALLOWED_TAGS + [
|
|
|
|
"img",
|
|
|
|
"center",
|
|
|
|
"iframe",
|
|
|
|
"div",
|
|
|
|
"span",
|
|
|
|
"table",
|
|
|
|
"tr",
|
|
|
|
"td",
|
|
|
|
"th",
|
|
|
|
"tr",
|
|
|
|
"pre",
|
|
|
|
"code",
|
|
|
|
"p",
|
|
|
|
"hr",
|
|
|
|
"h1",
|
|
|
|
"h2",
|
|
|
|
"h3",
|
|
|
|
"h4",
|
|
|
|
"h5",
|
|
|
|
"h6",
|
|
|
|
"thead",
|
|
|
|
"tbody",
|
|
|
|
"sup",
|
|
|
|
"dl",
|
|
|
|
"dt",
|
|
|
|
"dd",
|
2022-10-25 23:12:28 +00:00
|
|
|
"br",
|
2022-10-26 01:23:11 +00:00
|
|
|
"details",
|
2022-10-26 06:11:37 +00:00
|
|
|
"summary",
|
2022-10-25 23:07:45 +00:00
|
|
|
]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-10-26 01:26:49 +00:00
|
|
|
ALLOWED_ATTRS = ["src", "width", "height", "href", "class", "open"]
|
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 23:10:33 +00:00
|
|
|
def markdown(value):
|
2022-10-25 04:59:04 +00:00
|
|
|
extensions = EXTENSIONS
|
2022-10-25 23:07:45 +00:00
|
|
|
html = _markdown.markdown(value, extensions=extensions)
|
|
|
|
html = bleach.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS)
|
2022-10-25 04:59:04 +00:00
|
|
|
if not html:
|
|
|
|
html = escape(value)
|
|
|
|
return '<div class="md-typeset">%s</div>' % html
|