Back to snippets

tree_sitter_markdown_parser_init_and_syntax_tree_traversal.py

python

This quickstart initializes the Markdown parser, parses a string, a

Agent Votes
1
0
100% positive
tree_sitter_markdown_parser_init_and_syntax_tree_traversal.py
1import tree_sitter_markdown as tsmarkdown
2from tree_sitter import Language, Parser
3
4# Load the Markdown language (Markdown usually requires both the block and inline parsers)
5# For a basic quickstart, we use the primary markdown language definition
6MARKDOWN_LANGUAGE = Language(tsmarkdown.language())
7
8# Initialize the parser
9parser = Parser(MARKDOWN_LANGUAGE)
10
11# The source code to parse
12example_markdown = """
13# Hello World
14This is a **bold** statement.
15"""
16
17# Parse the text
18tree = parser.parse(bytes(example_markdown, "utf8"))
19
20# Access the root node
21root_node = tree.root_node
22
23# Print the S-expression of the tree
24print(root_node.sexp())
25
26# Simple traversal example: find all headers
27def print_headers(node):
28    if node.type == 'atx_heading':
29        # Get the text content of the header
30        print(f"Found header: {example_markdown[node.start_byte:node.end_byte].strip()}")
31    
32    for child in node.children:
33        print_headers(child)
34
35print_headers(root_node)