0.2.5 • Published 6 months ago

rehype-tree-sitter v0.2.5

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
6 months ago

rehype-tree-sitter

Build

rehype plugin to use tree-sitter to highlight code in <pre><code> 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 <pre><code> 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 <span> 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

0.2.1

7 months ago

0.2.3

7 months ago

0.2.2

7 months ago

0.2.5

6 months ago

0.2.4

7 months ago

0.2.0

8 months ago

0.1.4

8 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

10 months ago