rehype plugin to use tree-sitter to highlight code in pre code blocks

Overview

rehype-tree-sitter

Build

rehype plugin to use tree-sitter to highlight code in

 blocks

Contents

What is this?

This package is a unified (rehype) plugin to highlight code within a hast document. This plugin scans the source node tree for a

 element pair that contains a language-{} attribute, and runs the tree-sitter highlight code. This produces a list of annotated spans that can be customized with css.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that highlights code using tree-sitter.

When should I use this?

This plugin is useful when you want a more sophisticated highlighting experience than what TextMate grammars provide.

This plugin is built on hastscript, which does the work on syntax trees. rehype focusses on making it easier to transform content by abstracting such internals away.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install rehype-tree-sitter

Use

TODO

API

This package exports no identifiers. The default export is rehypeTreeSitter.

unified().use(rehypeTreeSitter[, options])

Highlight the code within the tree. This works by transforming the text within the code block into annotated elements. The span elements will have their class list as the highlight name stack from tree-sitter.

options

Configuration.

options.treeSitterGrammarRoot

This option is REQUIRED. This is the source folder path on your file system where tree-sitter will look for grammar source repositories. Without this variable set, the plugin will throw an error asking you to set it.

options.scopeMap

This option is a mapping of language-{} to tree-sitter scope (source.js, scope.xml, scope.sh). There is a default mapping included that supports a couple languages, but if you are using a custom grammar, it's required that you place the mapping between language identifier (usually found in the code block attributes) and the scope.

This option is OPTIONAL. This is the source folder path on your file system where tree-sitter will look for grammar source repositories. Without this variable set, the plugin will throw an error asking you to set it.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+.

Security

This plugin compiles and executes Rust code. In the Rust code, an underlying dependency (tree-sitter) will attempt to read tree-sitter grammar source repositories and compile and execute them. Please take care with your tree-sitter grammar repositories, and audit them to make sure the code is safe to run. Other than that, the plugin is safe.

License

BSD-2-Clause Β© Haze Booth

You might also like...
scan markdown files and execute `console` blocks

exec-commands βˆ’ scan markdown files and execute console blocks exec-commands is a utility to update command-line-tool examples embedded in markdown fi

Rust crate for Ok-wrapping and try blocks

tryvial A small crate for Ok-wrapping and try blocks. This is compatible with Result, Option, and any type implementing the unstable std::ops::Try tra

A tool for collecting rollup blocks from the Aztec Connect rollup, and exporting them to csv

Aztec Connect Data Gobbler The Aztec Connect Data gobbler is a tool made for extracting data from the Aztec Connect system using only L1 as its source

Building blocks for handling potentially unsafe statics.

Grounded Building blocks for handling potentially unsafe statics. This crate aims to provide useful and sound components that serve as building blocks

Librarian runs pre-configured commands against a group of files that match a set of filters

Filesystem Librarian Librarian runs pre-configured commands against a group of files that match a set of filters. The group of files is called a libra

Easy access of struct fields in strings using different/custom pre/postfix:
Easy access of struct fields in strings using different/custom pre/postfix: "Hello, {field}" in rust

Easy access to struct fields in strings 🐠 add strung to the dependencies in the Cargo.toml: [dependencies] strung = "0.1.3" πŸ¦€ use/import everything

🧰 Download pre-built binaries of all your favourite tools with a single command
🧰 Download pre-built binaries of all your favourite tools with a single command

tool-sync tool-sync is a CLI tool that solves one problem: πŸ“₯ Download pre-built binaries of all your favourite tools with a single command. tool-sync

 (Pre-Release Software) Secure, Encrypted, P2P chat written atop Warp, IPFS, LibP2P, Dioxus and many more awesome projects and protocols.
(Pre-Release Software) Secure, Encrypted, P2P chat written atop Warp, IPFS, LibP2P, Dioxus and many more awesome projects and protocols.

Uplink Privacy First, Modular, P2P messaging client built atop Warp. Uplink is written in pure Rust with a UI in Dioxus (which is also written in Rust

Pre-commit hook to help me stop naming files *.yml half of the time

Disallow file endings Pre-commit hook that lets you specify banned file endings. I keep naming half my yaml files *.yaml and the other *.yml and it's

Owner
null
Like grep, but uses tree-sitter grammars to search

tree-grepper Works like grep, but uses tree-sitter to search for structure instead of strings. Installing This isn't available packaged anywhere. That

Brian Hicks 219 Dec 25, 2022
A syntax highlighter for Node powered by Tree Sitter. Written in Rust.

tree-sitter-highlight A syntax highlighter for Node.js powered by Tree Sitter. Written in Rust. Usage The following will output HTML: const treeSitter

Devon Govett 211 Dec 20, 2022
Tree-sitter - An incremental parsing system for programming tools

tree-sitter Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and effic

null 10.6k Jan 9, 2023
A tree-sitter based AST difftool to get meaningful semantic diffs

diffsitter Disclaimer diffsitter is very much a work in progress and nowhere close to production ready (yet). Contributions are always welcome! Summar

Afnan Enayet 1.3k Jan 8, 2023
Semantic find-and-replace using tree-sitter-based macro expansion

Semantic find-and-replace using tree-sitter-based macro expansion

Isaac Clayton 15 Nov 10, 2022
Mypyc DSL grammar for tree-sitter

tree-sitter-mypyc Mypyc DSL grammar for tree-sitter. Installing (Neovim) This is based on the Neovim Tree-sitter docs for adding new parsers. Basicall

dosisod 3 Dec 30, 2022
tree-sitter meets Kakoune

kak-tree-sitter This is a binary server that interfaces tree-sitter with kakoune. Features Install Usage Design Credits Features Semantic highlighting

Dimitri Sabadie 5 May 3, 2023
Count your code by tokens, types of syntax tree nodes, and patterns in the syntax tree. A tokei/scc/cloc alternative.

tcount (pronounced "tee-count") Count your code by tokens, types of syntax tree nodes, and patterns in the syntax tree. Quick Start Simply run tcount

Adam P. Regasz-Rethy 48 Dec 7, 2022
As-tree - Print a list of paths as a tree of paths 🌳

as-tree Print a list of paths as a tree of paths. For example, given: dir1/foo.txt dir1/bar.txt dir2/qux.txt it will print: . β”œβ”€β”€ dir1 β”‚ β”œβ”€β”€ foo.tx

Jake Zimmerman 396 Dec 10, 2022
Programming language that compiles into Scratch blocks (SB3).

Claw Hello, and welcome to my cool project! I've spent... at least a month on this, and it's still not finished! Feel free to check it out though. :D

BD103 3 Dec 21, 2022