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 or "button" not in element.attrib["class"]: element.set("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 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"] ) section_class = "" section_class_only = "" if text_mode: section_class = ' class="text-section"' section_class_only = ' text-section' html_text = html_text.replace("
+-+-+-
", f'+-+-+-", f' -+-+-+", "
+=+=+=
", f'+=+=+=", f' {{{ {{{", ' }}} }}}", '