Skip to content

MarkdownItPyParser

Base classes

Name Children Inherits
MarkdownParser
mkconvert.parsers.base
Base class for Markdown -> HTML parsers.

⋔ Inheritance diagram

graph TD
  2579250432["markdown_it_py.MarkdownItPyParser"]
  2536207506["base.MarkdownParser"]
  1836221132["_core.Backend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  2536207506 --> 2579250432
  1836221132 --> 2536207506
  925645236 --> 1836221132
  2751680898 --> 925645236
  3481597255 --> 1836221132
  2751680898 --> 3481597255

🛈 DocStrings

Bases: MarkdownParser

markdown-it-py + mdit-py-plugins: pure Python, CommonMark compliant, pluggable.

Supports every common option. Further syntax (MkDocs-style !!! admonitions, MyST directives, attributes, ...) comes from mdit-py-plugins via plugins.

Source code in src/mkconvert/parsers/markdown_it_py.py
class MarkdownItPyParser(MarkdownParser):
    """markdown-it-py + mdit-py-plugins: pure Python, CommonMark compliant, pluggable.

    Supports every common option. Further syntax (MkDocs-style `!!!` admonitions, MyST
    directives, attributes, ...) comes from mdit-py-plugins via `plugins`.
    """

    name = "markdown-it-py"
    requires = ("markdown_it", "mdit_py_plugins")
    extra = "markdown-it-py"
    supported_options: ClassVar[frozenset[str]] = frozenset({
        "tables",
        "strikethrough",
        "autolinks",
        "task_lists",
        "footnotes",
        "heading_ids",
        "alerts",
        "math",
        "definition_lists",
        "hard_breaks",
        "smart_punctuation",
        "raw_html",
        "front_matter",
    })

    def __init__(
        self,
        options: MarkdownOptions | None = None,
        *,
        on_unsupported: OnUnsupported = "warn",
        plugins: Sequence[Callable[..., None] | tuple[Callable[..., None], dict[str, Any]]] = (),
        xhtml: bool = True,
        heading_levels: tuple[int, int] = (1, 6),
    ) -> None:
        """Create a markdown-it-py parser.

        Args:
            options: Common options.
            on_unsupported: See `Backend`.
            plugins: Further plugins, each a plugin function or a `(function, kwargs)`
                pair, e.g. `[mdit_py_plugins.admon.admon_plugin]`.
            xhtml: Self-closing tags with a trailing slash (`<br />`).
            heading_levels: Lowest and highest heading level that get ids
                (with `heading_ids`).
        """
        super().__init__(options, on_unsupported=on_unsupported)
        from markdown_it import MarkdownIt

        o = self.options
        md = MarkdownIt(
            "commonmark",
            {
                "html": o.raw_html,
                "xhtmlOut": xhtml,
                "breaks": o.hard_breaks,
                "typographer": o.smart_punctuation,
                "tasklists": o.task_lists,
                "alerts": o.alerts,
            },
        )
        if not o.raw_html:
            md.disable(["html_block", "html_inline"])
        if o.tables:
            md.enable("table")
        if o.strikethrough:
            md.enable("strikethrough")
        if o.smart_punctuation:
            md.enable(["replacements", "smartquotes"])
        if o.autolinks:
            from mdit_py_plugins.gfm_autolink import gfm_autolink_plugin

            md.use(gfm_autolink_plugin)
        if o.footnotes:
            from mdit_py_plugins.footnote import footnote_plugin

            md.use(footnote_plugin, inline=False)
        if o.heading_ids:
            from mdit_py_plugins.anchors import anchors_plugin

            md.use(anchors_plugin, min_level=heading_levels[0], max_level=heading_levels[1])
        if o.math:
            from mdit_py_plugins.dollarmath import dollarmath_plugin

            md.use(dollarmath_plugin)
        if o.definition_lists:
            from mdit_py_plugins.deflist import deflist_plugin

            md.use(deflist_plugin)
        for plugin in plugins:
            func, kwargs = plugin if isinstance(plugin, tuple) else (plugin, {})
            md.use(func, **kwargs)
        self._md: MarkdownIt = md

    def _render(self, text: str) -> str:
        return self._md.render(text)

__init__

__init__(
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    plugins: Sequence[Callable[..., None] | tuple[Callable[..., None], dict[str, Any]]] = (),
    xhtml: bool = True,
    heading_levels: tuple[int, int] = (1, 6),
) -> None

Create a markdown-it-py parser.

Parameters:

Name Type Description Default
options MarkdownOptions | None

Common options.

None
on_unsupported OnUnsupported

See Backend.

'warn'
plugins Sequence[Callable[..., None] | tuple[Callable[..., None], dict[str, Any]]]

Further plugins, each a plugin function or a (function, kwargs) pair, e.g. [mdit_py_plugins.admon.admon_plugin].

()
xhtml bool

Self-closing tags with a trailing slash (<br />).

True
heading_levels tuple[int, int]

Lowest and highest heading level that get ids (with heading_ids).

(1, 6)
Source code in src/mkconvert/parsers/markdown_it_py.py
def __init__(
    self,
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    plugins: Sequence[Callable[..., None] | tuple[Callable[..., None], dict[str, Any]]] = (),
    xhtml: bool = True,
    heading_levels: tuple[int, int] = (1, 6),
) -> None:
    """Create a markdown-it-py parser.

    Args:
        options: Common options.
        on_unsupported: See `Backend`.
        plugins: Further plugins, each a plugin function or a `(function, kwargs)`
            pair, e.g. `[mdit_py_plugins.admon.admon_plugin]`.
        xhtml: Self-closing tags with a trailing slash (`<br />`).
        heading_levels: Lowest and highest heading level that get ids
            (with `heading_ids`).
    """
    super().__init__(options, on_unsupported=on_unsupported)
    from markdown_it import MarkdownIt

    o = self.options
    md = MarkdownIt(
        "commonmark",
        {
            "html": o.raw_html,
            "xhtmlOut": xhtml,
            "breaks": o.hard_breaks,
            "typographer": o.smart_punctuation,
            "tasklists": o.task_lists,
            "alerts": o.alerts,
        },
    )
    if not o.raw_html:
        md.disable(["html_block", "html_inline"])
    if o.tables:
        md.enable("table")
    if o.strikethrough:
        md.enable("strikethrough")
    if o.smart_punctuation:
        md.enable(["replacements", "smartquotes"])
    if o.autolinks:
        from mdit_py_plugins.gfm_autolink import gfm_autolink_plugin

        md.use(gfm_autolink_plugin)
    if o.footnotes:
        from mdit_py_plugins.footnote import footnote_plugin

        md.use(footnote_plugin, inline=False)
    if o.heading_ids:
        from mdit_py_plugins.anchors import anchors_plugin

        md.use(anchors_plugin, min_level=heading_levels[0], max_level=heading_levels[1])
    if o.math:
        from mdit_py_plugins.dollarmath import dollarmath_plugin

        md.use(dollarmath_plugin)
    if o.definition_lists:
        from mdit_py_plugins.deflist import deflist_plugin

        md.use(deflist_plugin)
    for plugin in plugins:
        func, kwargs = plugin if isinstance(plugin, tuple) else (plugin, {})
        md.use(func, **kwargs)
    self._md: MarkdownIt = md

:fa-brands-github: Show source on GitHub