- Category
- Code
- Maintained by
- rehype team
- License
- MIT
- Module format
- ESM, default export
npm install rehype-highlightWhat it does
rehype-highlight finds <code> elements, typically inside <pre>, whose class names include language-* (or lang-*), and wraps tokens in <span class="hljs-…"> elements. It uses lowlight, which runs highlight.js grammars on a syntax tree instead of a string. The work happens when you build your HTML, so readers do not download a highlighting library.
Example
import {rehype} from 'rehype'
import rehypeHighlight from 'rehype-highlight'
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeHighlight)
.process('<pre><code class="language-js">const answer = 42</code></pre>')
console.log(String(file))<pre><code class="hljs language-js"><span class="hljs-keyword">const</span> answer = <span class="hljs-number">42</span></code></pre>Styling
The plugin adds class names only; it does not add colors. Include any highlight.js theme stylesheet, or write your own rules for classes like .hljs-keyword and .hljs-string.
Useful options
detect(defaultfalse): guess the language of code blocks that have nolanguage-*class.languages: register grammars. By default the lowlightcommonset (37 languages) is available. Passing a smaller set reduces bundle size in browsers.plainText: language names to leave unhighlighted. Ano-highlightclass also skips a block.aliases,prefixandsubsetfor finer control.
Alternatives
rehype-starry-night (TextMate grammars) and community Shiki-based plugins such as rehype-pretty-code produce different output styles. Compare them in the Plugin Explorer. The highlighting tutorial builds a full Markdown-to-highlighted-HTML pipeline.
Official references
- rehype-highlight readme (API and all options) (opens in a new tab)
- rehype-highlight on npm (current version) (opens in a new tab)
Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.