aboutsummaryrefslogtreecommitdiff
path: root/muppet/markdown.py
blob: df54feebc89f4a18c504dd2ee13f76b457681704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Muppet central markdown module.

This module exports one procedure, ``markdown``. This to easily
configure all markdown rendering to be the same in one central place,
and to allow "easy" switching of the markdown engine.
"""

from markdown_it import MarkdownIt
from mdit_py_plugins.anchors import anchors_plugin


def markdown(text: str) -> str:
    """
    Render the given markdown string to HTML.

    The current implementations sets these options and plugins:

    html
        Enable HTML in the source, which will be passed verbatim

    linkify
        Things which looks like links will be hyperlinked

    anchors_plugin
        Each header will get an appropriate id-attribute set, allowing
        hyperlinks to it.

    table
        Allow markdown tables.

    strikethrough
        Allow GFM-like strikethrough (``~~striken out~~``).

    :param text:
        A Markdown string.

    :returns:
        A HTML string.
    """
    md = MarkdownIt('commonmark', {'html': True, 'linkify': True}) \
        .use(anchors_plugin) \
        .enable('table') \
        .enable('strikethrough')

    return md.render(text)


# header_text.downcase().replace(' ', '-')