tree-sitter-md
Fast, incremental Markdown grammar for the Tree-sitter parsing framework.
Repository Health
Technical Analysis
tree-sitter-md is the Markdown grammar for Tree-sitter, the incremental parsing library that underpins syntax highlighting and code intelligence in modern editors. It compiles Markdown source into a concrete syntax tree that can be re-parsed efficiently as the document changes, providing the structural foundation for highlighting, folding, navigation, and analysis. Published on crates.io as tree-sitter-md, this grammar uniquely splits parsing into a block-structure grammar and a companion inline grammar to handle Markdown’s context-sensitive syntax.
The grammar ships with prebuilt bindings for Rust, C, Node.js, Python, Go, and Swift, so the same parser can be embedded in editors, language servers, and static-analysis pipelines regardless of host language.
What You Get
- Two coordinated grammars: a block grammar (tree-sitter-markdown) and an inline grammar (tree-sitter-markdown-inline)
- The generated C parsers for both grammars, ready to compile
- Rust bindings exposing both parsers through the tree-sitter-language interface
- Additional bindings for C, Node.js, Python, Go, and Swift from the same grammars
- Highlight and injection query files for syntax highlighting and embedded code
Common Use Cases
- Powering syntax highlighting for Markdown in code editors and terminal viewers
- Building language servers and linters that need a structured view of Markdown code
- Enabling code folding, structural selection, and outline views for Markdown files
- Extracting or transforming Markdown content programmatically via the syntax tree
Under The Hood
Architecture - Markdown’s syntax is context-sensitive, so the project is split into two grammars under tree-sitter-markdown/ and tree-sitter-markdown-inline/, sharing definitions from common/. The block grammar produces document structure (headings, lists, code fences, block quotes) and the inline grammar re-parses text spans (emphasis, links, code spans, HTML). Each grammar.js is compiled by the Tree-sitter CLI into a C parser with a custom external scanner that tracks indentation and open blocks. Tech Stack - The source of truth is the JavaScript grammar DSL; the generated parser is C. The Rust binding depends on tree-sitter-language and compiles the generated parser via a cc-based build script, exposing a LANGUAGE constant. Parallel bindings for C, Node.js, Python, Go, and Swift are generated from the same grammar. Code Quality - Correctness is anchored by Tree-sitter’s corpus test format (input snippets paired with expected S-expression trees) plus highlight fixtures, and CI runs on GitHub Actions. The generated parser is machine-produced and never hand-edited, so quality tracks the grammar definition and its tests. API Design - Integration is minimal: consumers pass the language’s exported LANGUAGE entry point to a Tree-sitter parser instance in their host language, so adding Markdown parsing to an editor or tool takes only a few lines once the Tree-sitter runtime is present.