miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
811 stars 113 forks source link

question: how to render AST back to a markdown string #148

Closed sixhobbits closed 2 years ago

sixhobbits commented 2 years ago

Maybe a dumb question, but once I have the AST from a markdown file, e.g. with

from mistletoe import Document, ast_renderer

with open("test.md") as f:
    lines = f.readlines()
document = Document(lines)
output = ast_renderer.get_ast(document)

print(output)

Is there a way I can easily render the AST back to a markdown string again? Or would I need to write a custom markdown renderer to do the process in reverse.

My use case is that I'd like to make modifications to the AST and then write it back as a markdown file again.

pbodnar commented 2 years ago

Hi @sixhobbits, this is surely not a dumb question. :) Actually, this feature was questioned a while back in #4 which is still open and awaiting contributions. I have updated it with some fresh info, for the case you're interested...