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) -> 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"] ) html_text = html_text.replace("
+-+-+-
", '+-+-+-", ' -+-+-+", "
+=+=+=
", '+=+=+=", ' {{{ {{{", ' }}} }}}", '