Syntax highlighting usually means shipping a JavaScript library that colors code after the page loads. With rehype you can do it once, when HTML is generated. Visitors receive plain HTML with class names and a stylesheet, and nothing runs in their browser.
Install
npm install rehype rehype-highlightHighlight HTML code blocks
rehype-highlight looks for <code> elements with a language-* class:
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>It added an hljs class to the <code> element and wrapped tokens in spans with hljs-* classes. Without a language class, nothing happens unless you enable detect: true.
From Markdown code fences
Markdown fences such as ```css become <code class="language-css"> when converted with remark-rehype, so highlighting fits naturally into a Markdown pipeline. This example also keeps txt blocks plain with the plainText option:
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeHighlight from 'rehype-highlight'
import rehypeStringify from 'rehype-stringify'
const markdown = [
'```css',
'a { color: teal; }',
'```',
'',
'```txt',
'plain text stays plain',
'```'
].join('\n')
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeHighlight, {plainText: ['txt']})
.use(rehypeStringify)
.process(markdown)
console.log(String(file))<pre><code class="hljs language-css"><span class="hljs-selector-tag">a</span> { <span class="hljs-attribute">color</span>: teal; }
</code></pre>
<pre><code class="language-txt">plain text stays plain
</code></pre>Add a theme
The plugin adds classes, not colors. Any highlight.js theme works because the class names match. Copy a theme stylesheet into your project, or write your own:
.hljs-keyword { color: #4338ca; }
.hljs-string { color: #0b7a55; }
.hljs-number { color: #b45309; }
.hljs-comment { color: #6b7a80; font-style: italic; }Control languages and bundle size
By default, the plugin registers lowlight's common set of 37 languages. That is fine on a server. In a browser bundle it is a lot of code, so register only what you need with the languages option, using grammars from lowlight or highlight.js:
import rehypeHighlight from 'rehype-highlight'
import css from 'highlight.js/lib/languages/css'
import javascript from 'highlight.js/lib/languages/javascript'
const options = {languages: {css, javascript}}
// unified().use(rehypeHighlight, options)For more languages than common, import the all set from lowlight. Use aliases to map extra names (for example {javascript: ['jsx']}), subset to limit auto-detection, and prefix to change the hljs- class prefix.
Combining with rehype-sanitize
The default sanitize schema allows language-* classes on <code> but not the hljs-* classes on spans. So run rehype-sanitize before rehype-highlight: sanitize what the author wrote, then highlight. If you sanitize afterwards, the colors disappear. The complete pipeline shows the working order.
Alternatives
rehype-starry-night, from the rehype team, uses TextMate grammars and produces highlighting similar to GitHub's.- Shiki-based community plugins, such as
rehype-pretty-code, offer VS Code themes, line highlighting and more, with heavier dependencies.
See highlighting live in the playground (enable rehype-highlight).