Gathering detailed insights and metrics for rehype-highlight
Gathering detailed insights and metrics for rehype-highlight
Gathering detailed insights and metrics for rehype-highlight
Gathering detailed insights and metrics for rehype-highlight
rehype-prism-plus
rehype plugin to highlight code blocks in HTML with Prism (via refractor) with line highlighting and line numbers
@mapbox/rehype-prism
rehype plugin to highlight code blocks in HTML with Prism
rehype-highlight-code-lines
Rehype plugin to add line numbers to code blocks and allow highlighting of desired code lines
rehype-shiki-reloaded
Rehype plugin to highlight code blocks with shiki, with dark mode.
npm install rehype-highlight
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
307 Stars
145 Commits
21 Forks
7 Watchers
1 Branches
13 Contributors
Updated on Jul 08, 2025
Latest Version
7.0.2
Package Id
rehype-highlight@7.0.2
Unpacked Size
25.57 kB
Size
8.08 kB
File Count
9
NPM Version
11.0.0
Node Version
23.1.0
Published on
Feb 03, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
rehype plugin to apply syntax highlighting to code with
lowlight
.
This package is a unified (rehype) plugin to perform syntax
highlighting.
It uses highlight.js
through lowlight
, which is pretty fast, relatively
small, and quite good.
This package bundles 37 common languages by default and you
can register more (190 with all
).
It looks for <code>
elements (when directly in <pre>
elements) and changes
them.
You can specify the code language (such as Python) with a language-*
or
lang-*
class, where the *
can be for example js
(so language-js
), md
,
css
, etc.
By default, code without such a language class is not highlighted.
Pass detect: true
to detect their programming language and highlight the code
anyway.
You can prevent specific blocks from being highlighted with a no-highlight
or
nohighlight
class on the <code>
.
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 applies syntax highlighting to the AST.
This project is useful when you want to perform syntax highlighting in rehype. One reason to do that is that it typically means the highlighting happens once at build time instead of every time at run time.
When you want a high quality highlighter that can support tons of grammars and
approaches how GitHub renders code,
you can use rehype-starry-night
.
This plugin is built on lowlight
, which is a virtual version of
highlight.js.
You can make a plugin based on this one with lowlight when you want to do things
differently.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install rehype-highlight
In Deno with esm.sh
:
1import rehypeHighlight from 'https://esm.sh/rehype-highlight@6'
In browsers with esm.sh
:
1<script type="module"> 2 import rehypeHighlight from 'https://esm.sh/rehype-highlight@6?bundle' 3</script>
Say we have the following file example.html
:
1<h1>Hello World!</h1> 2 3<pre><code class="language-js">var name = "World"; 4console.warn("Hello, " + name + "!")</code></pre>
…and our module example.js
contains:
1import rehypeHighlight from 'rehype-highlight' 2import rehypeParse from 'rehype-parse' 3import rehypeStringify from 'rehype-stringify' 4import {read} from 'to-vfile' 5import {unified} from 'unified' 6 7const file = await read('example.html') 8 9await unified() 10 .use(rehypeParse, {fragment: true}) 11 .use(rehypeHighlight) 12 .use(rehypeStringify) 13 .process(file) 14 15console.log(String(file))
…then running node example.js
yields:
1<h1>Hello World!</h1> 2 3<pre><code class="hljs language-js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">"World"</span>; 4<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(<span class="hljs-string">"Hello, "</span> + name + <span class="hljs-string">"!"</span>)</code></pre>
This package exports no identifiers.
The default export is rehypeHighlight
.
unified().use(rehypeHighlight[, options])
Apply syntax highlighting.
options
(Options
, optional)
— configurationTransform (Transformer
).
Options
Configuration (TypeScript type).
aliases
(Record<string, Array<string> | string>
, optional)
— register more aliases;
passed to lowlight.registerAlias
detect
(boolean
, default: false
)
— highlight code without language classes by guessing its programming
languagelanguages
(Record<string, LanguageFn>
, default:
common
)
— register languages; passed to lowlight.register
plainText
(Array<string>
, optional)
— list of language names to not highlight;
note you can also add no-highlight
classesprefix
(string
, default: 'hljs-'
)
— class prefixsubset
(Array<string>
, default: default: all registered
languages)
— names of languages to check when detectingThere are three ways to not apply syntax highlighting to code blocks.
They can be ignored with an explicit class of no-highlight
(or nohighlight
),
an explicit language name that’s listed in options.plainText
, or by setting
options.detect
to false
(default), which prevents <code>
without a class
from being automatically detected.
For example, with example.html
:
1<pre><code>this won’t be highlighted due to `detect: false` (default)</code></pre> 2 3<pre><code class="no-highlight">this won’t be highlighted due to its class</code></pre> 4 5<pre><code class="language-txt">this won’t be highlighted due to `plainText: ['txt']`</code></pre>
…and example.js
:
1import {rehype} from 'rehype' 2import rehypeHighlight from 'rehype-highlight' 3import {read} from 'to-vfile' 4 5const file = await rehype() 6 .data('settings', {fragment: true}) 7 .use(rehypeHighlight, {plainText: ['txt', 'text']}) 8 .process(await read('example.html')) 9 10console.log(String(file))
…then running that yields the same as example.html
: none of them are
highlighted.
rehype-highlight
supports 37 commonly used languages by default.
This makes it small to load in browsers and Node.js, while supporting enough
default cases.
You can add more languages.
For example, with example.html
:
1<pre><code class="language-bnf">a ::= 'a' | 'A'</code></pre>
…and example.js
:
1import bnf from 'highlight.js/lib/languages/bnf' 2import {common} from 'lowlight' 3import {rehype} from 'rehype' 4import rehypeHighlight from 'rehype-highlight' 5import {read} from 'to-vfile' 6 7const file = await rehype() 8 .data('settings', {fragment: true}) 9 .use(rehypeHighlight, {languages: {...common, bnf}}) 10 .process(await read('example.html')) 11 12console.log(String(file))
…then running that yields:
1<pre><code class="hljs language-bnf">a ::= <span class="hljs-string">'a'</span> | <span class="hljs-string">'A'</span></code></pre>
You can map your own language flags to highlight.js
languages.
For example, with example.html
:
1<pre><code class="language-custom-script">console.log(1)</code></pre>
…and example.js
:
1import {rehype} from 'rehype' 2import rehypeHighlight from 'rehype-highlight' 3import {read} from 'to-vfile' 4 5const file = await rehype() 6 .data('settings', {fragment: true}) 7 // 👉 **Note**: the keys are language names, values are the aliases that you 8 // want to also allow as `x` in `language-x` classes. 9 .use(rehypeHighlight, {aliases: {'javascript': 'custom-script'}}) 10 .process(await read('example.html')) 11 12console.log(String(file))
…then running that yields:
1<pre><code class="hljs language-custom-script"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">1</span>)</code></pre>
Applying syntax highlighting in rehype operates on <code>
elements with
certain classes and it injects many <span>
elements with classes.
Allowing arbitrary classes is an opening for security vulnerabilities.
To make HTML safe in rehype, use rehype-sanitize
.
It specifically allows /^language-./
class names on <code>
elements.
Which we also use.
So you can use rehype-highlight
after rehype-sanitize
:
1import {unified} from 'unified' 2import rehypeHighlight from './index.js' 3import rehypeParse from 'rehype-parse' 4import rehypeSanitize, {defaultSchema} from 'rehype-sanitize' 5import rehypeStringify from 'rehype-stringify' 6 7const file = await unified() 8 .use(rehypeParse, {fragment: true}) 9 .use(rehypeSanitize) 10 .use(rehypeHighlight) 11 .use(rehypeStringify) 12 .process('<pre><code className="language-js">console.log(1)</code></pre>') 13 14console.log(String(file))
…yields:
1<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">1</span>)</code></pre>
Using plugins after rehype-sanitize
, like we just did, is safe assuming
you trust those plugins.
If you do not trust rehype-highlight
, you can use it before.
But then you need to configure rehype-sanitize
to keep the classes you allow:
1import {unified} from 'unified' 2import rehypeHighlight from './index.js' 3import rehypeParse from 'rehype-parse' 4import rehypeSanitize, {defaultSchema} from 'rehype-sanitize' 5import rehypeStringify from 'rehype-stringify' 6 7const file = await unified() 8 .use(rehypeParse, {fragment: true}) 9 .use(rehypeHighlight) 10 .use(rehypeSanitize, { 11 ...defaultSchema, 12 attributes: { 13 ...defaultSchema.attributes, 14 span: [ 15 ...(defaultSchema.attributes?.span || []), 16 // Allow all class names starting with `hljs-`. 17 ['className', /^hljs-./] 18 // Alternatively, to allow only certain class names: 19 // ['className', 'hljs-number', 'hljs-title', 'hljs-variable'] 20 ] 21 }, 22 tagNames: [...(defaultSchema.tagNames || []), 'span'] 23 }) 24 .use(rehypeStringify) 25 .process('<pre><code className="language-js">console.log(1)</code></pre>') 26 27console.log(String(file))
You can add support for line numbers and line highlighting with a separate
plugin, rehype-highlight-code-lines
.
For example, with example.html
:
1<pre><code class="language-js">console.log("Hi!")</code></pre>
…and example.js
:
1import {rehype} from 'rehype' 2import rehypeHighlight from 'rehype-highlight' 3import rehypeHighlightCodeLines from 'rehype-highlight-code-lines' 4import {read} from 'to-vfile' 5 6const file = await rehype() 7 .data('settings', {fragment: true}) 8 .use(rehypeHighlight) 9 .use(rehypeHighlightCodeLines, { 10 showLineNumbers: true, 11 lineContainerTagName: 'div' 12 }) 13 .process(await read('example.html')) 14 15console.log(String(file))
…then running that yields:
1<pre><code class="hljs language-js"><div class="code-line numbered-code-line" data-line-number="1"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"Hi!"</span>)</div></code></pre>
This package is fully typed with TypeScript.
It exports the additional type Options
.
On the input side,
this plugin looks for code blocks with a language-*
class.
On the output side,
this plugin generates span
elements with classes that can be enhanced with
CSS.
See “CSS” in lowlight
for more info.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, rehype-highlight@^7
,
compatible with Node.js 16.
This plugin works with rehype-parse
version 1+, rehype-stringify
version 1+,
rehype
version 1+, and unified
version 4+.
Use of rehype-highlight
should be safe to use as highlight.js
and
lowlight
should be safe to use.
When in doubt, use rehype-sanitize
.
rehype-starry-night
— apply syntax highlighting with starry-night
rehype-meta
— add metadata to the head of a documentrehype-document
— wrap a fragment in a documentrehype-highlight-code-lines
— add line numbers and highlight linesSee contributing.md
in rehypejs/.github
for ways
to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More