• Stars
    star
    1,732
  • Rank 26,907 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTML processor powered by plugins part of the @unifiedjs collective

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?

You can use plugins to format or minify HTML. In:

<!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>

Out:

<!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.

You can use plugins to change HTML. In:

<h1>Hi, Saturn!</h1>

Plugin:

import {visit} from 'unist-util-visit'

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

Out:

<h2>Hi, Saturn!</h2>

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:

  • rehype-parse β€” plugin to take HTML as input and turn it into a syntax tree (hast)
  • rehype-stringify β€” plugin to take a syntax tree (hast) and turn it into HTML as output
  • rehype β€” unified, rehype-parse, and rehype-stringify, useful when input and output are HTML
  • rehype-cli β€” CLI around rehype to inspect and format HTML in scripts

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 all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

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!

Vercel

Motif

HashiCorp

GitBook

Gatsby

Netlify

Coinbase

ThemeIsle

Expo

Boost Note

Markdown Space

Holloway


You?

License

MIT Β© Titus Wormer

More Repositories

1

rehype-react

plugin to transform to preact, react, vue, etc
JavaScript
390
star
2

rehype-highlight

plugin to highlight code blocks
JavaScript
253
star
3

rehype-autolink-headings

plugin to add links to headings in HTML
JavaScript
206
star
4

rehype-slug

plugin to add `id` attributes to headings
JavaScript
191
star
5

awesome-rehype

Curated list of awesome rehype resources
171
star
6

rehype-sanitize

plugin to sanitize HTML
JavaScript
138
star
7

rehype-raw

plugin to parse the tree again
JavaScript
130
star
8

rehype-minify

plugins to minify HTML
JavaScript
89
star
9

rehype-external-links

rehype plugin to add rel (and target) to external links
JavaScript
85
star
10

rehype-remark

plugin to transform from HTML (rehype) to Markdown (remark)
JavaScript
78
star
11

rehype-meta

plugin to add metadata to the head of a document
JavaScript
31
star
12

rehype-document

plugin to wrap a fragment in a document
JavaScript
31
star
13

rehype-format

plugin to format HTML
JavaScript
28
star
14

rehype-dom

HTML processor to parse and compile with browser APIs, powered by plugins
JavaScript
25
star
15

rehype-retext

plugin to transform from HTML (rehype) to prose (retext)
JavaScript
17
star
16

rehype-twoslash

plugin to process JavaScript and TypeScript code with `twoslash` and highlight it with `starry-night`
JavaScript
14
star
17

rehype-github

rehype plugins that match how GitHub transforms markdown on their site
HTML
14
star
18

rehype-picture

plugin to wrap images in pictures
JavaScript
12
star
19

rehype-infer-reading-time-meta

rehype plugin to infer reading time as file metadata from the document
JavaScript
10
star
20

rehype-infer-description-meta

rehype plugin to infer file metadata from the contents of the document
JavaScript
9
star
21

camomile

Node.js HTTP image proxy to route images through SSL
JavaScript
8
star
22

ideas

Share ideas for new utilities and tools built with @rehypejs
6
star
23

rehype-starry-night

plugin to apply syntax highlighting to code with `starry-night`
JavaScript
5
star
24

rehype-infer-title-meta

plugin to infer file metadata from the main title of a document
JavaScript
5
star
25

.github

Community health files for rehype
TypeScript
2
star
26

rehype-shift-heading

plugin to change the rank (depth, level) of headings
JavaScript
2
star