26 lines
977 B
Python
26 lines
977 B
Python
from markdown_it import MarkdownIt
|
|
|
|
def parse_markdown_to_ast(md_content: str):
|
|
md = MarkdownIt()
|
|
tokens = md.parse(md_content)
|
|
# Convert to a JSON-serializable list of dicts (tokens are objects, so we dict-ify them recursively)
|
|
def token_to_dict(token):
|
|
d = {
|
|
'type': token.type,
|
|
'tag': token.tag,
|
|
'attrs': token.attrs,
|
|
'map': token.map,
|
|
'nesting': token.nesting,
|
|
'level': token.level,
|
|
'children': [token_to_dict(child) if child else None for child in token.children] if token.children else None,
|
|
'content': token.content,
|
|
'markup': token.markup,
|
|
'info': token.info,
|
|
'meta': token.meta,
|
|
'block': token.block,
|
|
'hidden': token.hidden
|
|
}
|
|
return {k: v for k, v in d.items() if v is not None} # Remove None values for cleanliness
|
|
|
|
return [token_to_dict(token) for token in tokens]
|