lepture/mistune

create AST with plugins, then render back to markdown?

nschloe opened this issue · 2 comments

I'm trying to parse a markdown file using some plugins and render that back to markdown. I tried

import mistune
from mistune.core import BlockState
from mistune.renderers.markdown import MarkdownRenderer

# format_markdown = mistune.create_markdown(renderer=MarkdownRenderer())
markdown = mistune.create_markdown(
    renderer="ast",
    plugins=[
        "footnotes",
        "math",
        "strikethrough",
    ],
)


md = """
a *b*

## Lorem.

$a+b=c$

~~a~~ [^fn1]

Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.

[^fn1]: abc
"""

tokens = markdown(md)

from rich import print

print(tokens)

renderer = MarkdownRenderer()
print(renderer(tokens, state=BlockState()))

but this gives

AttributeError: No renderer "'inline_math'"

in the render step. Are plugins not supported by the markdown renderer?

You need to customize the MarkdownRenderer, subclass it and add inline_math render method.

Aha, I had thought this was built-in. Thanks!