Skip to content

ComrakParser

Base classes

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

⋔ Inheritance diagram

graph TD
  1396973149["comrak.ComrakParser"]
  2536207506["base.MarkdownParser"]
  1836221132["_core.Backend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  2536207506 --> 1396973149
  1836221132 --> 2536207506
  925645236 --> 1836221132
  2751680898 --> 925645236
  3481597255 --> 1836221132
  2751680898 --> 3481597255

🛈 DocStrings

Bases: MarkdownParser

Comrak: fast, CommonMark / GFM compliant, supports every common option.

Source code in src/mkconvert/parsers/comrak.py
class ComrakParser(MarkdownParser):
    """Comrak: fast, CommonMark / GFM compliant, supports every common option."""

    name = "comrak"
    requires = ("comrak",)
    extra = "comrak"
    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",
        header_id_prefix: str = "",
        tagfilter: bool = False,
        superscript: bool = False,
        subscript: bool = False,
        underline: bool = False,
        spoiler: bool = False,
        greentext: bool = False,
        wikilinks: bool = False,
        multiline_block_quotes: bool = False,
        math_code: bool = False,
        relaxed_autolinks: bool = False,
        relaxed_tasklist_matching: bool = False,
        default_info_string: str | None = None,
        github_pre_lang: bool = False,
        full_info_string: bool = False,
        sourcepos: bool = False,
    ) -> None:
        """Create a comrak parser.

        Args:
            options: Common options.
            on_unsupported: See `Backend`.
            header_id_prefix: Prefix for heading ids (with `heading_ids`).
            tagfilter: GFM tag filter: escape `<script>`, `<iframe>`, ... in raw HTML.
            superscript: `^sup^`.
            subscript: `~sub~`.
            underline: `__underline__`.
            spoiler: `||spoiler||`.
            greentext: `> greentext` lines without blockquote nesting.
            wikilinks: `[[url|title]]` wikilinks.
            multiline_block_quotes: `>>>` fenced block quotes.
            math_code: `` $`math`$ `` math syntax.
            relaxed_autolinks: Looser autolink detection (with `autolinks`).
            relaxed_tasklist_matching: Any character as task list checkmark.
            default_info_string: Language for fenced code blocks without one.
            github_pre_lang: GitHub-style `<pre lang="...">` code blocks.
            full_info_string: Keep the full info string as `data-meta`.
            sourcepos: Add `data-sourcepos` attributes.
        """
        super().__init__(options, on_unsupported=on_unsupported)
        import comrak

        o = self.options
        ext = comrak.ExtensionOptions()
        ext.table = o.tables
        ext.strikethrough = o.strikethrough
        ext.autolink = o.autolinks
        ext.tasklist = o.task_lists
        ext.footnotes = o.footnotes
        ext.header_id_prefix = header_id_prefix if o.heading_ids else None
        ext.alerts = o.alerts
        ext.math_dollars = o.math
        ext.description_lists = o.definition_lists
        ext.tagfilter = tagfilter
        ext.superscript = superscript
        ext.subscript = subscript
        ext.underline = underline
        ext.spoiler = spoiler
        ext.greentext = greentext
        ext.wikilinks_title_after_pipe = wikilinks
        ext.multiline_block_quotes = multiline_block_quotes
        ext.math_code = math_code
        parse = comrak.ParseOptions()
        parse.smart = o.smart_punctuation
        parse.relaxed_autolinks = relaxed_autolinks
        parse.relaxed_tasklist_matching = relaxed_tasklist_matching
        parse.default_info_string = default_info_string
        render = comrak.RenderOptions()
        render.hardbreaks = o.hard_breaks
        render.unsafe_ = o.raw_html
        render.escape = not o.raw_html
        render.github_pre_lang = github_pre_lang
        render.full_info_string = full_info_string
        render.sourcepos = sourcepos
        self._options = (ext, parse, render)

    def _render(self, text: str) -> str:
        import comrak

        ext, parse, render = self._options
        return comrak.render_markdown(
            text, extension_options=ext, parse_options=parse, render_options=render
        )

__init__

__init__(
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    header_id_prefix: str = "",
    tagfilter: bool = False,
    superscript: bool = False,
    subscript: bool = False,
    underline: bool = False,
    spoiler: bool = False,
    greentext: bool = False,
    wikilinks: bool = False,
    multiline_block_quotes: bool = False,
    math_code: bool = False,
    relaxed_autolinks: bool = False,
    relaxed_tasklist_matching: bool = False,
    default_info_string: str | None = None,
    github_pre_lang: bool = False,
    full_info_string: bool = False,
    sourcepos: bool = False,
) -> None

Create a comrak parser.

Parameters:

Name Type Description Default
options MarkdownOptions | None

Common options.

None
on_unsupported OnUnsupported

See Backend.

'warn'
header_id_prefix str

Prefix for heading ids (with heading_ids).

''
tagfilter bool

GFM tag filter: escape <script>, <iframe>, ... in raw HTML.

False
superscript bool

^sup^.

False
subscript bool

~sub~.

False
underline bool

__underline__.

False
spoiler bool

||spoiler||.

False
greentext bool

> greentext lines without blockquote nesting.

False
wikilinks bool

[[url|title]] wikilinks.

False
multiline_block_quotes bool

>>> fenced block quotes.

False
math_code bool

$`math`$ math syntax.

False
relaxed_autolinks bool

Looser autolink detection (with autolinks).

False
relaxed_tasklist_matching bool

Any character as task list checkmark.

False
default_info_string str | None

Language for fenced code blocks without one.

None
github_pre_lang bool

GitHub-style <pre lang="..."> code blocks.

False
full_info_string bool

Keep the full info string as data-meta.

False
sourcepos bool

Add data-sourcepos attributes.

False
Source code in src/mkconvert/parsers/comrak.py
def __init__(
    self,
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    header_id_prefix: str = "",
    tagfilter: bool = False,
    superscript: bool = False,
    subscript: bool = False,
    underline: bool = False,
    spoiler: bool = False,
    greentext: bool = False,
    wikilinks: bool = False,
    multiline_block_quotes: bool = False,
    math_code: bool = False,
    relaxed_autolinks: bool = False,
    relaxed_tasklist_matching: bool = False,
    default_info_string: str | None = None,
    github_pre_lang: bool = False,
    full_info_string: bool = False,
    sourcepos: bool = False,
) -> None:
    """Create a comrak parser.

    Args:
        options: Common options.
        on_unsupported: See `Backend`.
        header_id_prefix: Prefix for heading ids (with `heading_ids`).
        tagfilter: GFM tag filter: escape `<script>`, `<iframe>`, ... in raw HTML.
        superscript: `^sup^`.
        subscript: `~sub~`.
        underline: `__underline__`.
        spoiler: `||spoiler||`.
        greentext: `> greentext` lines without blockquote nesting.
        wikilinks: `[[url|title]]` wikilinks.
        multiline_block_quotes: `>>>` fenced block quotes.
        math_code: `` $`math`$ `` math syntax.
        relaxed_autolinks: Looser autolink detection (with `autolinks`).
        relaxed_tasklist_matching: Any character as task list checkmark.
        default_info_string: Language for fenced code blocks without one.
        github_pre_lang: GitHub-style `<pre lang="...">` code blocks.
        full_info_string: Keep the full info string as `data-meta`.
        sourcepos: Add `data-sourcepos` attributes.
    """
    super().__init__(options, on_unsupported=on_unsupported)
    import comrak

    o = self.options
    ext = comrak.ExtensionOptions()
    ext.table = o.tables
    ext.strikethrough = o.strikethrough
    ext.autolink = o.autolinks
    ext.tasklist = o.task_lists
    ext.footnotes = o.footnotes
    ext.header_id_prefix = header_id_prefix if o.heading_ids else None
    ext.alerts = o.alerts
    ext.math_dollars = o.math
    ext.description_lists = o.definition_lists
    ext.tagfilter = tagfilter
    ext.superscript = superscript
    ext.subscript = subscript
    ext.underline = underline
    ext.spoiler = spoiler
    ext.greentext = greentext
    ext.wikilinks_title_after_pipe = wikilinks
    ext.multiline_block_quotes = multiline_block_quotes
    ext.math_code = math_code
    parse = comrak.ParseOptions()
    parse.smart = o.smart_punctuation
    parse.relaxed_autolinks = relaxed_autolinks
    parse.relaxed_tasklist_matching = relaxed_tasklist_matching
    parse.default_info_string = default_info_string
    render = comrak.RenderOptions()
    render.hardbreaks = o.hard_breaks
    render.unsafe_ = o.raw_html
    render.escape = not o.raw_html
    render.github_pre_lang = github_pre_lang
    render.full_info_string = full_info_string
    render.sourcepos = sourcepos
    self._options = (ext, parse, render)

:fa-brands-github: Show source on GitHub