Skip to content

How to convert AST back to markdown ? thanks #336

Closed Answered by adrgs
palla-lin asked this question in Q&A
Discussion options

You must be logged in to vote

Here's a short snippet of Python code that will do that, in case anyone else is interested:

import mistune # pip install mistune==3.0.0rc4
from mistune.renderers.markdown import MarkdownRenderer

markdown_content = """# Heading 1
## Heading 2
### Heading 3"""

markdown_ast = mistune.create_markdown(renderer=None)
ast_markdown = MarkdownRenderer()

results, state = markdown_ast.parse(markdown_content)

# modify results AST

result = ast_markdown(results, state)
print(result)

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@palla-lin
Comment options

Comment options

You must be logged in to vote
1 reply
@palla-lin
Comment options

Answer selected by lepture
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #335 on March 07, 2023 07:24.