Custom Renderer¶
The renderer converts markdown text to HTML on the server side. It's used by the template tag/filter and the preview API endpoint.
Default Renderer¶
DefaultRenderer uses python-markdown with the extra, codehilite, and toc extensions. If python-markdown is not installed, it falls back to HTML-escaped plaintext.
Creating a Custom Renderer¶
Subclass BaseRenderer and implement the render method:
from django_markdown_widget import BaseRenderer
class MarkdownItRenderer(BaseRenderer):
def render(self, markdown_text: str) -> str:
if not markdown_text:
return ""
import markdown_it
md = markdown_it.MarkdownIt()
return md.render(markdown_text)
With Sanitization¶
For user-generated content, sanitize the output:
import nh3
from django_markdown_widget import BaseRenderer
class SafeRenderer(BaseRenderer):
def render(self, markdown_text: str) -> str:
if not markdown_text:
return ""
import markdown
html = markdown.markdown(markdown_text, extensions=["extra"])
return nh3.clean(html)
Register Your Renderer¶
The class is loaded via django.utils.module_loading.import_string, so any importable Python path works.