Home

Awesome

rehype

Build Coverage Downloads Size Sponsors Backers Chat

rehype is a tool that transforms HTML with plugins. These plugins can inspect and change the HTML. You can use rehype on the server, the client, CLIs, deno, etc.

Intro

rehype is an ecosystem of plugins that work with HTML as structured data, specifically ASTs (abstract syntax trees). ASTs make it easy for programs to deal with HTML. We call those programs plugins. Plugins inspect and change trees. You can use the many existing plugins or you can make your own.

Contents

What is this?

With this project and a plugin, you can turn this HTML:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Saturn</title>
  </head>
  <body>
    <h1>Saturn</h1>
    <p>Saturn is a gas giant composed predominantly of hydrogen and helium.</p>
  </body>
</html>

…into the following HTML:

<!doctypehtml><html lang=en><meta charset=utf8><title>Saturn</title><h1>Saturn</h1><p>Saturn is a gas giant composed predominantly of hydrogen and helium.
<details><summary>Show example code</summary>
import rehypeParse from 'rehype-parse'
import rehypePresetMinify from 'rehype-preset-minify'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'

const file = await unified()
  .use(rehypeParse)
  .use(rehypePresetMinify)
  .use(rehypeStringify).process(`<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Saturn</title>
  </head>
  <body>
    <h1>Saturn</h1>
    <p>Saturn is a gas giant composed predominantly of hydrogen and helium.</p>
  </body>
</html>`)

console.log(String(file))
</details>

With another plugin, you can turn this HTML:

<h1>Hi, Saturn!</h1>

…into the following HTML:

<h2>Hi, Saturn!</h2>
<details><summary>Show example code</summary>
/**
 * @import {Root} from 'hast'
 */

import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
import {visit} from 'unist-util-visit'

const file = await unified()
  .use(rehypeParse, {fragment: true})
  .use(myRehypePluginToIncreaseHeadings)
  .use(rehypeStringify)
  .process('<h1>Hi, Saturn!</h1>')

console.log(String(file))

function myRehypePluginToIncreaseHeadings() {
  /**
   * @param {Root} tree
   */
  return function (tree) {
    visit(tree, 'element', function (node) {
      if (['h1', 'h2', 'h3', 'h4', 'h5'].includes(node.tagName)) {
        node.tagName = 'h' + (Number(node.tagName.charAt(1)) + 1)
      }
    })
  }
}
</details>

You can use rehype for many different things. unified is the core project that transforms content with ASTs. rehype adds support for HTML to unified. hast is the HTML AST that rehype uses.

This GitHub repository is a monorepo that contains the following packages:

When should I use this?

Depending on the input you have and output you want, you can use different parts of rehype. If the input is HTML, you can use rehype-parse with unified. If the output is HTML, you can use rehype-stringify with unified If both the input and output are HTML, you can use rehype on its own. When you want to inspect and format HTML files in a project, you can use rehype-cli.

Plugins

rehype plugins deal with HTML. You can choose from the many plugins that already exist. Here are three good ways to find plugins:

Some plugins are maintained by us here in the @rehypejs organization while others are maintained by folks elsewhere. Anyone can make rehype plugins, so as always when choosing whether to include dependencies in your project, make sure to carefully assess the quality of rehype plugins too.

Types

The rehype organization and the unified collective as a whole is fully typed with TypeScript. Types for hast are available in @types/hast.

Compatibility

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 compatible with Node.js 16.

Security

As improper use of HTML can open you up to a cross-site scripting (XSS) attacks, use of rehype can also be unsafe. Use rehype-sanitize to make the tree safe.

Use of rehype plugins could also open you up to other attacks. Carefully assess each plugin and the risks involved in using them.

For info on how to submit a report, see our security policy.

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md for ways to get help. Join us in Discussions to chat with the community and contributors.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

Sponsor

Support this effort and give back by sponsoring on OpenCollective!

<table> <tr valign="middle"> <td width="20%" align="center" rowspan="2" colspan="2"> <a href="https://vercel.com">Vercel</a><br><br> <a href="https://vercel.com"><img src="https://avatars1.githubusercontent.com/u/14985020?s=256&v=4" width="128"></a> </td> <td width="20%" align="center" rowspan="2" colspan="2"> <a href="https://motif.land">Motif</a><br><br> <a href="https://motif.land"><img src="https://avatars1.githubusercontent.com/u/74457950?s=256&v=4" width="128"></a> </td> <td width="20%" align="center" rowspan="2" colspan="2"> <a href="https://www.hashicorp.com">HashiCorp</a><br><br> <a href="https://www.hashicorp.com"><img src="https://avatars1.githubusercontent.com/u/761456?s=256&v=4" width="128"></a> </td> <td width="20%" align="center" rowspan="2" colspan="2"> <a href="https://www.gitbook.com">GitBook</a><br><br> <a href="https://www.gitbook.com"><img src="https://avatars1.githubusercontent.com/u/7111340?s=256&v=4" width="128"></a> </td> <td width="20%" align="center" rowspan="2" colspan="2"> <a href="https://www.gatsbyjs.org">Gatsby</a><br><br> <a href="https://www.gatsbyjs.org"><img src="https://avatars1.githubusercontent.com/u/12551863?s=256&v=4" width="128"></a> </td> </tr> <tr valign="middle"> </tr> <tr valign="middle"> <td width="20%" align="center" rowspan="2" colspan="2"> <a href="https://www.netlify.com">Netlify</a><br><br> <!--OC has a sharper image--> <a href="https://www.netlify.com"><img src="https://images.opencollective.com/netlify/4087de2/logo/256.png" width="128"></a> </td> <td width="10%" align="center"> <a href="https://www.coinbase.com">Coinbase</a><br><br> <a href="https://www.coinbase.com"><img src="https://avatars1.githubusercontent.com/u/1885080?s=256&v=4" width="64"></a> </td> <td width="10%" align="center"> <a href="https://themeisle.com">ThemeIsle</a><br><br> <a href="https://themeisle.com"><img src="https://avatars1.githubusercontent.com/u/58979018?s=128&v=4" width="64"></a> </td> <td width="10%" align="center"> <a href="https://expo.io">Expo</a><br><br> <a href="https://expo.io"><img src="https://avatars1.githubusercontent.com/u/12504344?s=128&v=4" width="64"></a> </td> <td width="10%" align="center"> <a href="https://boostnote.io">Boost Note</a><br><br> <a href="https://boostnote.io"><img src="https://images.opencollective.com/boosthub/6318083/logo/128.png" width="64"></a> </td> <td width="10%" align="center"> <a href="https://markdown.space">Markdown Space</a><br><br> <a href="https://markdown.space"><img src="https://images.opencollective.com/markdown-space/e1038ed/logo/128.png" width="64"></a> </td> <td width="10%" align="center"> <a href="https://www.holloway.com">Holloway</a><br><br> <a href="https://www.holloway.com"><img src="https://avatars1.githubusercontent.com/u/35904294?s=128&v=4" width="64"></a> </td> <td width="10%"></td> <td width="10%"></td> </tr> <tr valign="middle"> <td width="100%" align="center" colspan="8"> <br> <a href="https://opencollective.com/unified"><strong>You?</strong></a> <br><br> </td> </tr> </table>

License

MIT © Titus Wormer

<!-- Definitions -->