miyuchina / mistletoe

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

Question how to modify ast and write back out as markdown #178

Closed tvmaly closed 1 year ago

tvmaly commented 1 year ago

Could you add an example on how to take an AST of the markdown, modify it then write out the result to markdown?

SomeoneSerge commented 1 year ago

Related/duplicate of https://github.com/miyuchina/mistletoe/issues/4

pbodnar commented 1 year ago

@SomeoneSerge is correct. I.e. right now, there is no MarkdownRenderer, but it is being worked on (namely in #162).

@tvmaly, I hope that this answers your question. :)

codekiln commented 5 months ago

Note to others who wind up here; now that we have MarkdownRenderer class see its sub-section in https://github.com/miyuchina/mistletoe?tab=readme-ov-file#usage-from-python ... Maybe something like this...

with MarkdownRenderer(max_line_length=20) as renderer:
        print(renderer.render(mistletoe.Document(fin)))
pbodnar commented 5 months ago

@codekiln, thanks for the update. Some more info then:

1) The thing is one can use MarkdownRenderer as any other renderer in mistletoe. While of course, it has got special options like max_line_length which can be passed to the constructor (see its pydoc).

2) Also note that there is this dedicated Markdown to Markdown parsing-and-rendering chapter available in the Developer's Guide, which shows how to modify the AST before rendering it back to markdown.