aboutsummaryrefslogtreecommitdiff
path: root/markdown_renderer.py
blob: bd05baea32394696c894b790cca297ca1d4fa779 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import markdown
import frontmatter
import markdown.treeprocessors
import markdown.extensions


class DefaultClassTreeprocessor(markdown.treeprocessors.Treeprocessor):
    def run(self, root):
        for element in root.iter("a"):
            print(element.attrib)
            if "class" not in element.attrib:
                element.set("class", "link")
            elif "button" not in element.attrib["class"]:
                element.set("class", element.attrib["class"] + " link")
            if "href" in element.attrib and element.attrib["href"] == "":
                del element.attrib["href"]
        for element in root.iter("img"):
            element.set("class", "img")


class DefaultClassExtension(markdown.extensions.Extension):
    def extendMarkdown(self, md):
        md.treeprocessors.register(DefaultClassTreeprocessor(md), "link_class", 2)


def strict_identity_slugify(value, separator):
    return value


def md_to_html(raw_content: str, text_mode: bool = False) -> str:
    metadata, content = frontmatter.parse(raw_content)
    html_text = markdown.markdown(
        content,
        output_format="html",
        extensions=["attr_list", "fenced_code", "tables", DefaultClassExtension(), "md_in_html", "pymdownx.tilde", "footnotes", "codehilite", "mdx_wikilink_plus", "toc", "pymdownx.mark", "pymdownx.arithmatex"],
        extension_configs={
            "toc": {
                "slugify": strict_identity_slugify
            },
            "pymdownx.arithmatex": {
                "generic": True
            },
            "mdx_wikilink_plus": {
                "url_whitespace": " "
            },
        }
    )

    section_class = ""
    section_class_only = ""

    if text_mode:
        section_class = ' class="text-section"'
        section_class_only = ' text-section'

    html_text = html_text.replace("<p>+-+-+-</p>", f'<section{section_class}><div class="div-sizing">')
    html_text = html_text.replace("<p>+-+-+-", f'<section{section_class}><div class="div-sizing">')
    html_text = html_text.replace("+-+-+-</p>", f'<section{section_class}><div class="div-sizing">')
    html_text = html_text.replace("-+-+-+</p>", "</div></section>")
    html_text = html_text.replace("<p>-+-+-+", "</div></section>")
    html_text = html_text.replace("<p>+=+=+=</p>", f'<section class="normal-section{section_class_only}"><div class="div-sizing">')
    html_text = html_text.replace("<p>+=+=+=", f'<section class="normal-section{section_class_only}"><div class="div-sizing">')
    html_text = html_text.replace("+=+=+=</p>", f'<section class="normal-section{section_class_only}"><div class="div-sizing">')
    html_text = html_text.replace("<p>{{{</p>", '<div class="flex-container flex-container-dynamic gap-row">')
    html_text = html_text.replace("<p>{{{", '<div class="flex-container flex-container-dynamic gap-row">')
    html_text = html_text.replace("{{{</p>", '<div class="flex-container flex-container-dynamic gap-row">')
    html_text = html_text.replace("<p>}}}</p>", '</div>')
    html_text = html_text.replace("}}}</p>", '</div>')
    html_text = html_text.replace("<p>}}}", '</div>')
    html_text = html_text.replace("<p>{&amp;{</p>", '<div class="flex-container flex-container-dynamic gap">')
    html_text = html_text.replace("<p>{&amp;{", '<div class="flex-container flex-container-dynamic gap">')
    html_text = html_text.replace("{&amp;{</p>", '<div class="flex-container flex-container-dynamic gap">')

    return (metadata, html_text)