#tree-sitter #mdbook #highlighting #html

bin+lib mdbook-treesitter

mdbook-treesitter is an mdBook preprocessor for html adding tree-sitter highlighting support

2 releases

0.1.1 Apr 5, 2023
0.1.0 Apr 2, 2023

#1781 in Text processing

36 downloads per month

MIT license

20KB
329 lines

mdbook-treesitter

mdbook-treesitter is an mdBook preprocessor for html adding tree-sitter highlighting support.

It simply translates the tree-sitter highlighting into highlightjs one.

Installation

Install the preprocessor:

cargo install mdbook-treesitter

Configure mdBook

Add this in your book.toml:

[output.html]
additional-js = ["treesitter.js"]

[preprocessor.treesitter]
command = "mdbook-treesitter"

Add this javascript in the file treesitter.js at the root of your project:

let t = document.getElementsByClassName("language-treesitter");
for (let i = 0; i < t.length; i++) {
  t[i].innerHTML = t[i].innerText;
}

Usage

Use usual codeblocks like that:

```treesitter javascript
console.log(this.a + b + "c" + 4);
```

Wait, you need to add related tree-sitter files:

  • Create a folder treesitter in the root of your mdBook project
  • Then, add your language_name.so in the created folder
  • Next, create a folder language_name in the tree-sitter folder
  • Finally, add in it your scm files

Example for javascript:

- My awesome mdBook/
    - book.toml
    - treesitter.js
    - book/
    - src/
    - treesitter/
        - javascript.so
        - javascript/
            - highlights.scm
            - injections.scm
            - locals.scm

   

🧃

Dependencies

~12–26MB
~357K SLoC