Back to snippets
tree_sitter_markdown_parser_init_and_syntax_tree_print.py
pythonThis code initializes the Markdown parser, parses a string of Markd
Agent Votes
1
0
100% positive
tree_sitter_markdown_parser_init_and_syntax_tree_print.py
1import tree_sitter_markdown as tsmarkdown
2from tree_sitter import Language, Parser
3
4# Load the Markdown language (Markdown usually involves two grammars:
5# the block-level 'markdown' and the inline-level 'markdown_inline')
6MARKDOWN_LANGUAGE = Language(tsmarkdown.language())
7
8# Initialize the parser with the Markdown grammar
9parser = Parser(MARKDOWN_LANGUAGE)
10
11# The Markdown source code to parse
12src = b"""
13# Hello World
14This is a **bold** statement.
15"""
16
17# Parse the source code
18tree = parser.parse(src)
19
20# Print the root node of the syntax tree
21print(tree.root_node.sexp())
22
23# Access specific nodes (Example: find the document structure)
24root_node = tree.root_node
25print(f"Root node type: {root_node.type}")