2024-01-19 01:46:41 +00:00
|
|
|
import markdown as _markdown
|
|
|
|
import bleach
|
|
|
|
from django.utils.html import escape
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from pymdownx import superfences
|
|
|
|
|
|
|
|
|
|
|
|
EXTENSIONS = [
|
2024-02-06 00:32:08 +00:00
|
|
|
"pymdownx.arithmatex",
|
2024-01-19 01:46:41 +00:00
|
|
|
"pymdownx.magiclink",
|
|
|
|
"pymdownx.betterem",
|
|
|
|
"pymdownx.details",
|
|
|
|
"pymdownx.emoji",
|
|
|
|
"pymdownx.inlinehilite",
|
|
|
|
"pymdownx.superfences",
|
|
|
|
"pymdownx.highlight",
|
|
|
|
"pymdownx.tasklist",
|
|
|
|
"markdown.extensions.footnotes",
|
|
|
|
"markdown.extensions.attr_list",
|
|
|
|
"markdown.extensions.def_list",
|
|
|
|
"markdown.extensions.tables",
|
|
|
|
"markdown.extensions.admonition",
|
|
|
|
"nl2br",
|
|
|
|
"mdx_breakless_lists",
|
|
|
|
]
|
|
|
|
|
|
|
|
EXTENSION_CONFIGS = {
|
2024-02-06 00:32:08 +00:00
|
|
|
"pymdownx.arithmatex": {
|
|
|
|
"generic": True,
|
|
|
|
},
|
2024-01-19 01:46:41 +00:00
|
|
|
"pymdownx.superfences": {
|
|
|
|
"custom_fences": [
|
|
|
|
{
|
|
|
|
"name": "sample",
|
|
|
|
"class": "no-border",
|
|
|
|
"format": superfences.fence_code_format,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"pymdownx.highlight": {
|
|
|
|
"auto_title": True,
|
|
|
|
"auto_title_map": {
|
|
|
|
"Text Only": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ALLOWED_TAGS = list(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",
|
|
|
|
"br",
|
|
|
|
"details",
|
|
|
|
"summary",
|
|
|
|
]
|
|
|
|
|
2024-02-19 23:00:44 +00:00
|
|
|
ALLOWED_ATTRS = [
|
|
|
|
"src",
|
|
|
|
"width",
|
|
|
|
"height",
|
|
|
|
"href",
|
|
|
|
"class",
|
|
|
|
"open",
|
|
|
|
"title",
|
|
|
|
"frameborder",
|
|
|
|
"allow",
|
|
|
|
"allowfullscreen",
|
2024-02-26 20:31:33 +00:00
|
|
|
"loading",
|
2024-02-19 23:00:44 +00:00
|
|
|
]
|
2024-01-19 01:46:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def markdown(value, lazy_load=False):
|
|
|
|
extensions = EXTENSIONS
|
|
|
|
html = _markdown.markdown(
|
|
|
|
value, extensions=extensions, extension_configs=EXTENSION_CONFIGS
|
|
|
|
)
|
|
|
|
|
|
|
|
html = bleach.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS)
|
|
|
|
|
|
|
|
if not html:
|
|
|
|
html = escape(value)
|
|
|
|
if lazy_load:
|
|
|
|
soup = BeautifulSoup(html, features="html.parser")
|
|
|
|
for img in soup.findAll("img"):
|
|
|
|
if img.get("src"):
|
2024-02-26 20:31:33 +00:00
|
|
|
img["loading"] = "lazy"
|
2024-01-19 01:46:41 +00:00
|
|
|
for img in soup.findAll("iframe"):
|
|
|
|
if img.get("src"):
|
2024-02-26 20:31:33 +00:00
|
|
|
img["loading"] = "lazy"
|
2024-01-19 01:46:41 +00:00
|
|
|
html = str(soup)
|
|
|
|
return '<div class="md-typeset content-description">%s</div>' % html
|