Is there a way of getting the rendered markdown from a subtree of SyntaxTreeNodes? #297
Unanswered
anapaulagomes
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Heya: from markdown_it import MarkdownIt
from markdown_it.tree import SyntaxTreeNode
text = """
## Checklist
1. Pull the code from the `main` branch
from the main project repo
2. Configure the environment
1. Configure the environment variables in the `.env` file
2. Install the dependencies with:
bash
pip install -r requirements.txt
3. Run the migrations: `python manage.py migrate`
4. Done!
"""
env = {}
md = MarkdownIt()
node = SyntaxTreeNode(md.parse(text, env))
sublist = node.children[1].children[1].children[1]
html = md.renderer.render(sublist.to_tokens(), md.options, env)
print(html) For rendering back to Markdown, there is: https://github.com/executablebooks/mdformat/blob/0cbd2054dedf98ec8366001c8a16eacfa85cebc1/src/mdformat/renderer/__init__.py#L27 from mdformat.renderer import MDRenderer
renderer = MDRenderer()
markdown = renderer.render(sublist.to_tokens(), md.options, env)
print(markdown) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi folks! First of all: really nice library. Thanks for putting it together!
I'd like to get part of a subtree of SyntaxTreeNodes and render it back to the markdown format (HTML would do the job too, I think). Is there a way of doing it? I've been exploring with tokens and such but I couldn't find a way of doing it yet.
Example:
I have this tree of SyntaxTreeNodes from the markdown below:
When I get to point 2, I'd like to have its inner enumerated list as markdown, like:
How could I do it?
Any help is appreciated! Thanks! 🌷
Beta Was this translation helpful? Give feedback.
All reactions