Rehype features
What rehype does well, what the ecosystem adds, and where another tool is a better fit. Each feature links to a deeper, tested walkthrough.
Standards-based parsing
rehype-parse uses parse5 to parse HTML according to the WHATWG HTML standard, the same rules browsers follow. Unclosed tags, misnested elements and missing <html> wrappers are repaired exactly as a browser would repair them. Parse errors can be reported as warnings.
The hast syntax tree
Documents become plain JavaScript objects in the hast format: elements with tagName, properties and children, plus text, comment and doctype nodes. Because the tree is plain data, you can inspect it, test it and transform it with ordinary code and shared utilities.
Plugins
A plugin is a function that receives the tree (and the file) and changes it. Plugins compose in order with .use(). Presets bundle several plugins with settings, as rehype-preset-minify does.
Sanitizing
rehype-sanitize removes anything not on an allowlist. The default schema follows GitHub's rules for user content, and you can extend or restrict it.
Markdown in both directions
With remark-rehype you turn Markdown into HTML. With rehype-remark you turn HTML into Markdown. Both work through the shared unified interface.
Command line interface
rehype-cli processes files and folders, reads .rehyperc config files and rehype fields in package.json, supports ignore files, watch mode and syntax tree output.
Messages and reporting
Plugins report warnings on the virtual file (vfile) with line and column positions, so linting-style checks fit naturally into a pipeline.
Typed
The packages ship TypeScript types, including types for hast nodes, which helps when writing plugins.
Tree basicsUse cases
Where rehype fits
| Task | Typical plugins | Learn more |
|---|---|---|
| Render user-submitted HTML safely | rehype-sanitize | Sanitize HTML safely |
| Docs and blog pipelines from Markdown | remark-rehype, rehype-slug, rehype-autolink-headings, rehype-highlight | Markdown to safe HTML |
| Production HTML output | rehype-preset-minify | Format and minify |
| Readable HTML for review or diffs | rehype-format | HTML Formatter |
| Migrate HTML content to Markdown | rehype-remark, remark-stringify | HTML to Markdown |
| MDX and Next.js content | any rehype plugin via rehypePlugins | rehype with MDX |
| Custom checks (missing alt text, banned tags) | your own plugin | Build a plugin |
When rehype is not the right tool
- Live DOM manipulation in the browser. rehype works on a tree in memory, not on the page's live DOM. For changing the current page, use DOM APIs. (The related
rehype-dompackages parse and serialize with browser APIs, for smaller bundles when you trust the content.) - CSS or JavaScript processing. rehype handles HTML. Some minify plugins call CSS and JavaScript minifiers for inline code, but for stylesheets and scripts, use dedicated tools.
- Scraping with selectors only. If you only need to query elements, a selector library may be simpler, although
hast-util-selectbrings CSS selectors to hast. - CommonJS-only toolchains on old Node.js. rehype is ESM only. Node.js 20.19+ and 22.12+ can
require()it, older versions cannot. See troubleshooting.
Try the features without installing anything
Every tool on this site runs a real rehype pipeline in your browser.