highlight your code using tree-sitter-highlight
npm install rehype-tree-sitter-highlight
This is a rehype plugin. To highlight code blocks in html:
import fromHtml from 'rehype-parse'
import toHtml from 'rehype-stringify'
import rehypeTreeSitterHighlight from 'rehype-tree-sitter-highlight'
import { unified } from 'unified'
const doc = "```js\nconst hello = 'World';\n```\n"
async function createProcessor() {
const processor = unified()
.use(fromHtml)
.use(rehypeTreeSitterHighlight)
.use(toHtml)
return processor
}
const processor = await createProcessor()
const vfile = await processor.process(doc)
console.log(vfile.toString())
Refer to the tree-sitter-highlight docs for information on how to theme the output html.
Refer to the tree-sitter-highlight for supported languages.
Unknown languages are ignored by default. You can set ignoreUnknownLanguage: false
to throw an error when an unsupported language is encountered.
See examples for ways to use this plugin.
Much of the docs and code is taken from rehype-shiki.