Skip to content

base

Class info

Classes

Name Children Inherits
MarkdownOptions
mkconvert.parsers.base
Common Markdown -> HTML options, mapped onto every parser.
    MarkdownParser
    mkconvert.parsers.base
    Base class for Markdown -> HTML parsers.

    🛈 DocStrings

    Common options and base class for Markdown -> HTML parsers.

    MarkdownOptions

    Bases: Options

    Common Markdown -> HTML options, mapped onto every parser.

    The defaults are plain CommonMark (every extension off, raw HTML passed through), except that a leading front matter block is dropped. Check MarkdownParser.supported_options for what a parser can enable.

    Source code in src/mkconvert/parsers/base.py
    class MarkdownOptions(Options):
        """Common Markdown -> HTML options, mapped onto every parser.
    
        The defaults are plain CommonMark (every extension off, raw HTML passed through),
        except that a leading front matter block is dropped.
        Check `MarkdownParser.supported_options` for what a parser can enable.
        """
    
        tables: bool = False
        """GFM pipe tables."""
        strikethrough: bool = False
        """`~~text~~` -> `<del>` (or `<s>`)."""
        autolinks: bool = False
        """Link bare URLs (`https://...`, `www.`) like GFM's autolink extension."""
        task_lists: bool = False
        """`- [x] item` -> list items with checkboxes."""
        footnotes: bool = False
        """`text[^1]` references and `[^1]: ...` definitions."""
        heading_ids: bool = False
        """Add slug `id` attributes to headings."""
        alerts: bool = False
        """GitHub alerts: `> [!NOTE]`, `> [!WARNING]`, ..."""
        math: bool = False
        """`$inline$` and `$$display$$` math, rendered as markup for MathJax / KaTeX."""
        definition_lists: bool = False
        """`Term` followed by `: definition` lines -> `<dl>`."""
        hard_breaks: bool = False
        """Render every newline inside a paragraph as `<br>`."""
        smart_punctuation: bool = False
        """Typographic quotes, dashes and ellipses."""
        raw_html: bool = True
        """Pass raw HTML through. `False` escapes or omits it (parser-dependent).
    
        This is not a sanitizer: use `mkconvert.sanitize` for untrusted input.
        """
        front_matter: bool = True
        """Drop a leading `---`-delimited front matter block instead of rendering it.
    
        On by default: almost every Markdown file with a leading `---` block means front
        matter, not a horizontal rule followed by a heading.
        """
    
        @classmethod
        def gfm(cls, **changes: Any) -> Self:
            """GitHub Flavored Markdown: tables, strikethrough, autolinks and task lists.
    
            Args:
                changes: Further options to set, e.g. `footnotes=True`.
            """
            values: dict[str, Any] = dict(
                tables=True, strikethrough=True, autolinks=True, task_lists=True
            )
            return cls(**(values | changes))
    

    alerts class-attribute instance-attribute

    alerts: bool = False
    

    GitHub alerts: > [!NOTE], > [!WARNING], ...

    autolinks: bool = False
    

    Link bare URLs (https://..., www.) like GFM's autolink extension.

    definition_lists class-attribute instance-attribute

    definition_lists: bool = False
    

    Term followed by : definition lines -> <dl>.

    footnotes class-attribute instance-attribute

    footnotes: bool = False
    

    text[^1] references and [^1]: ... definitions.

    front_matter class-attribute instance-attribute

    front_matter: bool = True
    

    Drop a leading ----delimited front matter block instead of rendering it.

    On by default: almost every Markdown file with a leading --- block means front matter, not a horizontal rule followed by a heading.

    hard_breaks class-attribute instance-attribute

    hard_breaks: bool = False
    

    Render every newline inside a paragraph as <br>.

    heading_ids class-attribute instance-attribute

    heading_ids: bool = False
    

    Add slug id attributes to headings.

    math class-attribute instance-attribute

    math: bool = False
    

    $inline$ and $$display$$ math, rendered as markup for MathJax / KaTeX.

    raw_html class-attribute instance-attribute

    raw_html: bool = True
    

    Pass raw HTML through. False escapes or omits it (parser-dependent).

    This is not a sanitizer: use mkconvert.sanitize for untrusted input.

    smart_punctuation class-attribute instance-attribute

    smart_punctuation: bool = False
    

    Typographic quotes, dashes and ellipses.

    strikethrough class-attribute instance-attribute

    strikethrough: bool = False
    

    ~~text~~ -> <del> (or <s>).

    tables class-attribute instance-attribute

    tables: bool = False
    

    GFM pipe tables.

    task_lists class-attribute instance-attribute

    task_lists: bool = False
    

    - [x] item -> list items with checkboxes.

    gfm classmethod

    gfm(**changes: Any) -> Self
    

    GitHub Flavored Markdown: tables, strikethrough, autolinks and task lists.

    Parameters:

    Name Type Description Default
    changes Any

    Further options to set, e.g. footnotes=True.

    {}
    Source code in src/mkconvert/parsers/base.py
    @classmethod
    def gfm(cls, **changes: Any) -> Self:
        """GitHub Flavored Markdown: tables, strikethrough, autolinks and task lists.
    
        Args:
            changes: Further options to set, e.g. `footnotes=True`.
        """
        values: dict[str, Any] = dict(
            tables=True, strikethrough=True, autolinks=True, task_lists=True
        )
        return cls(**(values | changes))
    

    MarkdownParser

    Bases: Backend[MarkdownOptions]

    Base class for Markdown -> HTML parsers.

    Subclasses take MarkdownOptions plus their own keyword-only options, map them onto the wrapped library once in __init__ and implement _render.

    Source code in src/mkconvert/parsers/base.py
    class MarkdownParser(Backend[MarkdownOptions]):
        """Base class for Markdown -> HTML parsers.
    
        Subclasses take `MarkdownOptions` plus their own keyword-only options, map them onto
        the wrapped library once in `__init__` and implement `_render`.
        """
    
        options_class: ClassVar[type[MarkdownOptions]] = MarkdownOptions
    
        def convert(self, text: str) -> str:
            """Convert Markdown `text` to HTML."""
            if self.options.front_matter:
                _, text = split_front_matter(text)
            return self._render(text)
    
        @abstractmethod
        def _render(self, text: str) -> str:
            """Render Markdown to HTML with the wrapped library."""
    

    convert

    convert(text: str) -> str
    

    Convert Markdown text to HTML.

    Source code in src/mkconvert/parsers/base.py
    def convert(self, text: str) -> str:
        """Convert Markdown `text` to HTML."""
        if self.options.front_matter:
            _, text = split_front_matter(text)
        return self._render(text)