Skip to main content

Module markdown

Module markdown 

Source
Expand description

Server-side Markdown → HTML rendering for page bodies.

Authors write markdown in the admin (via the extension-field editor). At render time, pulldown-cmark converts to HTML; the result is then run through ammonia so any embedded <script>, event handlers, javascript: hrefs, or other XSS vectors are stripped before reaching the browser.

Exposed as the Tera filter markdown. Use it like:

{{ extension.body_markdown | markdown | safe }}

The | safe is required — the filter returns trusted HTML, and without safe Tera would re-escape the angle brackets.

Functions§

register_tera_filter
Register the markdown filter on a Tera instance. Host apps already calling crate::translation::register_tera_filter should call this too so {{ … | markdown }} resolves.
render
Convert a markdown source string to sanitized HTML.
sanitize_html
Sanitize an HTML string for richtext storage / preview. Uses the same ammonia allow-list as the markdown renderer, PLUS the move-safe internal-link attributes <a linktype="page|media" id="N"> that the | richtext filter resolves to real URLs at render time. Ammonia’s default policy strips unknown <a> attributes, which would drop the linktype/id pair and turn move-safe links into broken ones the next time a page is moved — so they’re explicitly allow-listed here. Both are inert (no script/URL vector), so allowing them is XSS-safe. Used by the richtext on-save sanitizer (sanitized_extension_form) and the preview pane.