rehype on its own only parses and serializes. Everything interesting comes from plugins. This tutorial covers where to find them, how to judge them, and how to combine them.
Where to find plugins
- The official list. The rehype repository keeps a list of plugins (opens in a new tab), with more than a hundred entries, from the rehype team and the community.
- npm. Plugins follow the
rehype-*naming convention. Searching npm finds more, but also abandoned experiments. - This site. The plugin library covers common plugins in depth, and the Plugin Explorer filters a curated list.
A checklist for choosing
- Who maintains it? Plugins under the
rehypejsorremarkjsGitHub organizations are maintained by the unified collective. Community plugins can be excellent, but check activity. - Is it compatible? Current unified packages are ESM only and use the current hast format. A plugin last released many years ago may expect an older format; check its peer dependencies and recent issues.
- Does the readme explain options and security? Good plugins state what they change and whether they can introduce unsafe markup.
- Is it the right side? If the job is about Markdown syntax, a remark plugin may be simpler. See rehype vs remark.
- What does it cost? Some plugins pull in large dependencies (syntax highlighters, math renderers, browsers for diagrams). That matters in serverless functions and browser bundles.
Install and use
Install a plugin like any npm package, import its default export, and pass it to .use(), with options as the second argument:
import {rehype} from 'rehype'
import rehypeExternalLinks from 'rehype-external-links'
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeExternalLinks, {target: '_blank', rel: ['noopener', 'noreferrer']})
.process('<a href="https://example.com">Out</a> <a href="/docs/">In</a>')
console.log(String(file))<a href="https://example.com" rel="noopener noreferrer" target="_blank">Out</a> <a href="/docs/">In</a>Pass the plugin itself (.use(rehypeExternalLinks)), not the result of calling it.
Order is everything
Plugins run in the order they are added, and each one sees the tree the previous ones left. Here the same two plugins produce different ids depending on order:
import {rehype} from 'rehype'
import rehypeSanitize from 'rehype-sanitize'
import rehypeSlug from 'rehype-slug'
const html = '<h2>Intro</h2><p onclick="x()">Text</p>'
// Sanitize first, then add trusted ids: ids stay clean.
const sanitizeFirst = await rehype()
.data('settings', {fragment: true})
.use(rehypeSanitize)
.use(rehypeSlug)
.process(html)
// Add ids first, then sanitize: the ids get the clobber prefix.
const slugFirst = await rehype()
.data('settings', {fragment: true})
.use(rehypeSlug)
.use(rehypeSanitize)
.process(html)
console.log('sanitize, then slug:', String(sanitizeFirst))
console.log('slug, then sanitize:', String(slugFirst))sanitize, then slug: <h2 id="intro">Intro</h2><p>Text</p>
slug, then sanitize: <h2 id="user-content-intro">Intro</h2><p>Text</p>Rules of thumb that work for most pipelines:
- Re-parse raw HTML first (
rehype-raw), so later plugins can see it. - Sanitize untrusted content next (
rehype-sanitize). - Add trusted markup: ids (
rehype-slug) before anchor links (rehype-autolink-headings), then link attributes, highlighting and other enhancements. - Document-level steps such as
rehype-documentor metadata. - Formatting or minifying last, and only one of the two.
Presets
A preset bundles plugins and settings into one object you pass to .use(). rehype-preset-minify is the best-known example. Presets keep shared configuration consistent across projects; you can write your own as {plugins: [...], settings: {...}}.
Try combinations quickly
The playground runs a safe set of plugins in a fixed order and shows the output next to a sandboxed preview. When none of the existing plugins fit, writing your own usually takes fewer than 20 lines.