rehype-highlight

Highlights code blocks at build time with highlight.js grammars through lowlight, so pages ship no highlighter.

Category
Code
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-highlight

What 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

highlight.mjs
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))
Output node highlight.mjs
<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 (default false): guess the language of code blocks that have no language-* class.
  • languages: register grammars. By default the lowlight common set (37 languages) is available. Passing a smaller set reduces bundle size in browsers.
  • plainText: language names to leave unhighlighted. A no-highlight class also skips a block.
  • aliases, prefix and subset for 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

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.