"""Markdown → безопасный HTML для тел писем."""
import bleach
import markdown as md
ALLOWED_TAGS = [
"p", "br", "strong", "em", "u", "s", "code", "pre", "blockquote",
"h1", "h2", "h3", "h4", "ul", "ol", "li", "a", "img", "hr",
"table", "thead", "tbody", "tr", "th", "td",
]
ALLOWED_ATTRS = {
"a": ["href", "title", "rel"],
"img": ["src", "alt", "title"],
}
def render(body_md: str) -> str:
if not body_md:
return ""
html = md.markdown(body_md, extensions=["fenced_code", "tables", "nl2br", "sane_lists"])
return bleach.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS, strip=True)