Skip to main content

markdown_to_plaintext()

Parse CommonMark/GFM and render as plain text.

Usage

Source

markdown_to_plaintext(
    text,
    *,
    hardbreaks=False,
    smart=False,
    normalize=False,
    unsafe=False,
    footnotes=False,
    extensions=(),
    width=0,
    options=0,
)

Removes heading, emphasis, link, and code delimiters while preserving their text. Link destinations and image URLs are omitted; image descriptions are retained. Lists keep their markers and indentation, and paragraphs remain separated by blank lines.

GFM tables retain pipe-table syntax, task lists retain checkbox markers, and strikethrough text is surrounded by single tildes. Math keeps its TeX source between $ or $$ delimiters. Enabled footnotes retain numbered references and definitions. Raw HTML blocks, including their text, are omitted; inline HTML tags are omitted without removing surrounding text.

Parameters

text: str

The Markdown string to parse and render. Must be a Python str.

hardbreaks: bool = False

Render soft line breaks as hard breaks (newlines). By default, this is False. When enabled, width-based wrapping is disabled.

smart: bool = False

Convert straight quotes to curly quotes, -- to en-dashes, --- to em-dashes, and ... to ellipses. By default, this is False.

normalize: bool = False

Consolidate adjacent text nodes in the parsed AST. By default, this is False.

unsafe: bool = False

Raw HTML is always omitted, regardless of this flag. By default, this is False.

footnotes: bool = False

Enable footnote syntax parsing. By default, this is False.

extensions: Sequence[str] = ()

A sequence of extension names to enable. Valid names are "table", "strikethrough", "autolink", "tagfilter", "tasklist", and "math".

width: int = 0

The column at which to wrap output lines. Set to 0 (the default) to disable line wrapping entirely and preserve soft line breaks. With a positive width, soft line breaks become spaces before wrapping. Code blocks are not wrapped. Ignored when hardbreaks=True.

options: int = 0
An integer bitmask of Options flags (e.g., Options.SMART | Options.UNSAFE). Merged via OR with any boolean keyword arguments set to True. Defaults to 0. Options.NOBREAKS converts soft line breaks to spaces and disables automatic wrapping.

Keyword Arguments Vs. Options Flags

The boolean keyword arguments (smart, unsafe, hardbreaks, etc.) are convenience shortcuts for the most common Options flags. When both styles are provided, they are merged via OR. See markdown_to_html() for a detailed explanation.

Returns

str
The rendered text, including a trailing newline. Empty input returns "\n". Output whitespace is not stripped.

Examples

Remove inline formatting:

from multimark import markdown_to_plaintext

markdown_to_plaintext("**Hello**, *world*!")
'Hello, world!\n'

Wrap readable text at 72 columns:

markdown_to_plaintext("A paragraph with **bold** text. " * 5, width=72)
'A paragraph with bold text. A paragraph with bold text. A paragraph with\nbold text. A paragraph with bold text. A paragraph with bold text.\n'