Skip to content

Markdown

Render markdown content into safe HTML with optional sanitization.

Installation

pip install "faststrap[markdown]"

Usage

from faststrap import Markdown

Markdown(
    "# Hello\n\nThis is **safe** markdown.",
    cls="prose",
)

Behavior

  • Uses Python markdown for conversion.
  • Sanitizes output with bleach by default.
  • Raises a clear ImportError if optional dependencies are missing.

Advanced Control

Markdown(
    text,
    sanitize=True,
    extensions=["extra", "tables", "fenced_code"],
)

Parameters

Parameter Type Default Description
text str required Markdown source text.
sanitize bool True Sanitize rendered HTML with bleach.
extensions list[str] \| None None Markdown extensions. Defaults to extra, sane_lists, tables, and fenced_code.
allowed_tags list[str] \| None None Override allowed HTML tags for sanitization.
allowed_attributes dict[str, list[str]] \| None None Override allowed attributes for sanitization.
allowed_protocols list[str] \| None None Override allowed link protocols.
**kwargs Any Extra wrapper attributes.

Security Notes

If you disable sanitization, only render trusted content.

API Reference

faststrap.components.display.markdown.render_markdown(text, *, sanitize=True, extensions=None, allowed_tags=None, allowed_attributes=None, allowed_protocols=None)

Render markdown text into HTML with optional sanitization.

Source code in src/faststrap/components/display/markdown.py
def render_markdown(
    text: str,
    *,
    sanitize: bool = True,
    extensions: list[str] | None = None,
    allowed_tags: list[str] | None = None,
    allowed_attributes: dict[str, list[str]] | None = None,
    allowed_protocols: list[str] | None = None,
) -> str:
    """Render markdown text into HTML with optional sanitization."""
    markdown_module = _load_markdown_module()
    html = cast(
        str,
        markdown_module.markdown(
            text,
            extensions=extensions or ["extra", "sane_lists", "tables", "fenced_code"],
        ),
    )

    if not sanitize:
        return html

    bleach_module = _load_bleach_module()
    return cast(
        str,
        bleach_module.clean(
            html,
            tags=allowed_tags or DEFAULT_ALLOWED_TAGS,
            attributes=allowed_attributes or DEFAULT_ALLOWED_ATTRIBUTES,
            protocols=allowed_protocols or DEFAULT_ALLOWED_PROTOCOLS,
            strip=True,
        ),
    )

faststrap.components.display.markdown.Markdown(text, *, sanitize=True, extensions=None, allowed_tags=None, allowed_attributes=None, allowed_protocols=None, **kwargs)

Render markdown into a styled container.

This component is optional and requires extra dependencies: pip install faststrap[markdown]

Source code in src/faststrap/components/display/markdown.py
@register(category="display")
def Markdown(
    text: str,
    *,
    sanitize: bool = True,
    extensions: list[str] | None = None,
    allowed_tags: list[str] | None = None,
    allowed_attributes: dict[str, list[str]] | None = None,
    allowed_protocols: list[str] | None = None,
    **kwargs: Any,
) -> Div:
    """Render markdown into a styled container.

    This component is optional and requires extra dependencies:
    `pip install faststrap[markdown]`
    """
    html = render_markdown(
        text,
        sanitize=sanitize,
        extensions=extensions,
        allowed_tags=allowed_tags,
        allowed_attributes=allowed_attributes,
        allowed_protocols=allowed_protocols,
    )

    user_cls = kwargs.pop("cls", "")
    attrs: dict[str, Any] = {
        "cls": merge_classes("faststrap-markdown", user_cls),
    }
    attrs.update(convert_attrs(kwargs))
    return Div(NotStr(html), **attrs)