cmsmanage/tasks/clean_markdown.py
Adam Goldsmith 02986bdabc Replace {django-,}bleach with {django-,}nh3, as bleach is now deprecated
https://bluesock.org/~willkg/blog/dev/bleach_6_0_0_deprecation.html
nh3/ammonia is faster anyway. django-nh3 is not yet feature complete,
but cmsmanage was only using the template tag anyway
2023-12-30 12:54:08 -05:00

28 lines
610 B
Python

import nh3
from markdownx.utils import markdownify
# fmt: off
MARKDOWN_TAGS = [
"h1", "h2", "h3", "h4", "h5", "h6",
"b", "i", "strong", "em", "tt", "sub", "sup",
"p", "br", "span", "div",
"blockquote", "code", "pre",
"hr",
"ul", "ol", "li",
"dl", "dd", "dt",
"img",
"a",
"table", "thead", "tbody", "tr", "th", "td",
]
MARKDOWN_ATTRS = {
"*": ["id"],
"img": ["src", "alt", "title"],
"a": ["href", "alt", "title"],
}
def markdown_to_clean_html(md: str) -> str:
x = nh3.clean(markdownify(md), tags=MARKDOWN_TAGS, attributes=MARKDOWN_ATTRS)
return x