tree_sitter_md/
lib.rs

1//! This crate provides Markdown language support for the [tree-sitter][] parsing library.
2//!
3//! It contains two grammars: [`LANGUAGE`] to parse the block structure of markdown documents and
4//! [`INLINE_LANGUAGE`] to parse inline content.
5//!
6//! It also supplies [`MarkdownParser`] as a convenience wrapper around the two grammars.
7//! [`MarkdownParser::parse`] returns a [`MarkdownTree`] instread of a [`Tree`][Tree]. This struct
8//! contains a block tree and an inline tree for each node in the block tree that has inline
9//! content.
10//!
11//! [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
12//! [Tree]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Tree.html
13//! [tree-sitter]: https://tree-sitter.github.io/
14
15#![cfg_attr(docsrs, feature(doc_cfg))]
16
17use tree_sitter_language::LanguageFn;
18
19extern "C" {
20    fn tree_sitter_markdown() -> *const ();
21    fn tree_sitter_markdown_inline() -> *const ();
22}
23
24/// The tree-sitter [`LanguageFn`][LanguageFn] for the block grammar.
25pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_markdown) };
26
27/// The tree-sitter [`LanguageFn`][LanguageFn] for the inline grammar.
28pub const INLINE_LANGUAGE: LanguageFn =
29    unsafe { LanguageFn::from_raw(tree_sitter_markdown_inline) };
30
31/// The syntax highlighting queries for the block grammar.
32pub const HIGHLIGHT_QUERY_BLOCK: &str =
33    include_str!("../../tree-sitter-markdown/queries/highlights.scm");
34
35/// The language injection queries for the block grammar.
36pub const INJECTION_QUERY_BLOCK: &str =
37    include_str!("../../tree-sitter-markdown/queries/injections.scm");
38
39/// The syntax highlighting queries for the inline grammar.
40pub const HIGHLIGHT_QUERY_INLINE: &str =
41    include_str!("../../tree-sitter-markdown-inline/queries/highlights.scm");
42
43/// The language injection queries for the inline grammar.
44pub const INJECTION_QUERY_INLINE: &str =
45    include_str!("../../tree-sitter-markdown-inline/queries/injections.scm");
46
47/// The content of the [`node-types.json`][] file for the block grammar.
48///
49/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
50pub const NODE_TYPES_BLOCK: &str = include_str!("../../tree-sitter-markdown/src/node-types.json");
51
52/// The content of the [`node-types.json`][] file for the inline grammar.
53///
54/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
55pub const NODE_TYPES_INLINE: &str =
56    include_str!("../../tree-sitter-markdown-inline/src/node-types.json");
57
58#[cfg(feature = "parser")]
59#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
60mod parser;
61
62#[cfg(feature = "parser")]
63#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
64pub use parser::*;
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn can_load_block_grammar() {
72        let mut parser = tree_sitter::Parser::new();
73        parser
74            .set_language(&LANGUAGE.into())
75            .expect("Error loading Markdown block grammar");
76    }
77
78    #[test]
79    fn can_load_inline_grammar() {
80        let mut parser = tree_sitter::Parser::new();
81        parser
82            .set_language(&INLINE_LANGUAGE.into())
83            .expect("Error loading Markdown inline grammar");
84    }
85}