Replace WYSIWYG editor with Markdown editor across all relevant models and admin fields. Add utilities for rendering and stripping markdown. Adjust serializers, views, and templates to support markdown content. Introduce `PastedImage` model and upload endpoint for handling inline image uploads in markdown. This change simplifies content formatting while enhancing flexibility with markdown support.
21 lines
601 B
Python
21 lines
601 B
Python
import re
|
|
from contextlib import suppress
|
|
|
|
import markdown
|
|
from django.utils.html import strip_tags
|
|
|
|
|
|
def render_markdown(text: str) -> str:
|
|
"""Render a markdown string to HTML."""
|
|
with suppress(Exception):
|
|
return markdown.markdown(text, extensions=["tables", "fenced_code", "nl2br"])
|
|
return text
|
|
|
|
|
|
def strip_markdown(text: str) -> str:
|
|
"""Render markdown to HTML then strip all tags, collapsing whitespace."""
|
|
with suppress(Exception):
|
|
html = render_markdown(text)
|
|
plain = strip_tags(html)
|
|
return re.sub(r"\s+", " ", plain).strip()
|
|
return text
|