rehype is an open source tool for processing HTML with JavaScript. It reads HTML into a structured tree, lets plugins inspect and change that tree, and writes HTML back out. The official repository describes it as an HTML processor powered by plugins and part of the unified collective.
The problem rehype solves
Sooner or later, most web projects need to change HTML they did not write by hand: remove dangerous markup from a comment, add ids to headings, mark external links, highlight code samples, or shrink pages for production. The tempting approach is string manipulation with regular expressions. It works on the examples you test and fails on the ones you did not think of, because HTML has optional quotes, optional closing tags, comments, entities and error recovery rules. Our article Why syntax trees beat regex shows real failures.
rehype parses HTML with the same algorithm browsers use, so the tree it gives you matches what a browser would build. Changes are made to nodes, not to characters.
A first look
import {rehype} from 'rehype'
import rehypeSlug from 'rehype-slug'
import rehypeExternalLinks from 'rehype-external-links'
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeSlug)
.use(rehypeExternalLinks, {rel: ['nofollow']})
.process('<h2>Quick start</h2><p>Read the <a href="https://unifiedjs.com">unified docs</a>.</p>')
console.log(String(file))<h2 id="quick-start">Quick start</h2><p>Read the <a href="https://unifiedjs.com" rel="nofollow">unified docs</a>.</p>The heading got an id and the external link got rel="nofollow", each done by a small, dedicated plugin. The input could have been messy or broken; the result would still be well-formed HTML.
The pieces
- rehype-parse turns HTML text into a tree.
- hast is the tree format: plain objects for elements, text, comments and the doctype.
- Plugins transform the tree. There are plugins for sanitizing, highlighting, linking, formatting, minifying, metadata and much more.
- rehype-stringify turns the tree back into HTML.
- The rehype package combines the parser and serializer, and rehype-cli runs pipelines from the terminal.
All of this runs on unified, a general content-processing engine. How rehype works explains the details.
The unified family
| Project | Content | Tree format |
|---|---|---|
| rehype | HTML | hast |
| remark | Markdown | mdast |
| retext | Natural language | nlcst |
Because they share one engine, you can chain them. A blog pipeline commonly parses Markdown with remark, converts it to hast, runs rehype plugins, and outputs HTML. MDX, the format that mixes Markdown and JSX, is built on the same ecosystem and accepts rehype plugins directly.
Where you may already be using it
Many documentation tools and static site generators run remark and rehype under the hood and let you add plugins in their configuration, for example through a rehypePlugins option. If you have configured rehype-slug or rehype-highlight in a framework, you have used rehype. See rehype with MDX and Next.js.
When to use rehype
- You transform HTML at build time or on the server: content pipelines, static sites, email templates, scraping and migration.
- You accept HTML from users and must make it safe.
- You want reusable, testable transformations instead of one-off string hacks.
When something else fits better
- Changing the live page in a browser: use DOM APIs.
- Minifying stylesheets or scripts on their own: use CSS and JavaScript tools.
Is it free and maintained?
rehype is MIT licensed. It is developed in the open on GitHub by the unified collective, which also maintains remark and many related packages. For current activity and releases, look at the repository (opens in a new tab) directly.
Next steps
Install rehype, follow getting started, or try it without installing anything in the playground.