tree-sitter-markdown

repository·split_parser·Indexed 20 days ago

https://github.com/tree-sitter-grammars/tree-sitter-markdown

A tree-sitter grammar for parsing Markdown, version 0.5.3, designed for syntax highlighting in editors like Neovim and Helix. It follows the CommonMark specification with support for GitHub Flavored Markdown (GFM). The parser utilizes a two-step process involving block and inline grammars and provides Node.js bindings for accessing the parser and query strings for highlights, injections, locals, and tags.

Tokens
1.6K
Snippets
5
Records
8
Agent score
69%

What's inside tree-sitter-markdown

  1. Overview of tree-sitter-markdown

    split_parser

    tree-sitter-markdown is a Markdown parser for the tree-sitter ecosystem. It is designed to follow the CommonMark specification, with support for various extensions like GitHub Flavored Markdown (GFM).

    Important Note on Correctness: This parser is primarily intended for providing syntactical information for syntax highlighting in editors (such as Neovim and Helix). Because Markdown is a complex format to map to tree-sitter's restrictive parsing rules, it is not recommended for use in applications where absolute parsing correctness is critical.

  2. How to use the grammars in standalone mode

    split_parser

    To use the two grammars (block and inline) together in a standalone implementation, follow this two-step process:

    1. Block Parse: First, parse the document using the block grammar.
    2. Inline Parse: Perform a second parse using the inline grammar. Use ts_parser_set_included_ranges to specify the segments that constitute inline content.

    Note on Ranges: The segments passed to ts_parser_set_included_ranges should be the parts marked as inline nodes from the first parse. You must exclude the children of those inline nodes from the specified ranges.

    For a reference implementation, see lib.rs in the bindings folder of the repository.

  3. Enable or disable Markdown extensions at compile time

    split_parser

    Extensions can be toggled during compilation using environment variables. By default, several extensions are enabled. To disable all default extensions, set the NO_DEFAULT_EXTENSIONS environment variable.

    To specifically enable or disable individual extensions, use the corresponding environment variables listed below:

    | Name | Environment variable | Specification | Default |
    |:----:|:--------------------:|:-------------:|:-------:|
    | Github flavored markdown | `EXTENSION_GFM` | [link](https://github.github.com/gfm/) | ✓ |
    | Task lists | `EXTENSION_TASK_LIST` | [link](https://github.github.com/gfm/#task-list-items-extension-) | ✓ |
    | Strikethrough | `EXTENSION_STRIKETHROUGH` | [link](https://github.github.com/gfm/#strikethrough-extension-) | ✓ |
    | Pipe tables | `EXTENSION_PIPE_TABLE` | [link](https://github.github.com/gfm/#tables-extension-) | ✓ |
    | YAML metadata | `EXTENSION_MINUS_METADATA` | [link](https://gohugo.io/content-management/front-matter/) | ✓ |
    | TOML metadata | `EXTENSION_PLUS_METADATA` | [link](https://gohugo.io/content-management/front-matter/) | ✓ |
    | Tags | `EXTENSION_TAGS` | [link](https://help.obsidian.md/Editing+and+formatting/Tags#Tag+format) |  |
    | Wiki Link | `EXTENSION_WIKI_LINK` | [link](https://help.obsidian.md/Linking+notes+and+files/Internal+links) |  |
  4. Use the tree-sitter-markdown Node.js binding

    split_parser

    The tree-sitter-markdown Node.js binding provides access to the Markdown parser and its associated query files. The default export is a binding object that includes the parser and several query strings used for syntax highlighting and injections.

    Note that query properties (like HIGHLIGHTS_QUERY) are lazily loaded from the filesystem via getters when first accessed.

    import parser from 'tree-sitter-markdown';
    
    // The parser is available on the default export
    // Queries are loaded lazily upon access
    const highlights = parser.HIGHLIGHTS_QUERY;
    const injections = parser.INJECTIONS_QUERY;
  5. Access query strings via binding properties

    split_parser

    The binding object exposes several query strings as properties. These are read from .scm files in the repository and are useful for syntax highlighting, injections, and local scope analysis.

    Available query properties:

    • HIGHLIGHTS_QUERY: Syntax highlighting queries.
    • INJECTIONS_QUERY: Injection queries for embedding other languages.
    • LOCALS_QUERY: Queries for local scope information.
    • TAGS_QUERY: Queries for tag-related information.
    // Example of accessing available queries
    console.log(parser.HIGHLIGHTS_QUERY);
    console.log(parser.INJECTIONS_QUERY);
    console.log(parser.LOCALS_QUERY);
    console.log(parser.TAGS_QUERY);