HTML processor powered by plugins part of the @unifiedjs collective
Installations
npm install rehype-stringify
Releases
Developer
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
Yes
Node Version
22.7.0
NPM Version
10.8.3
Statistics
1,831 Stars
345 Commits
91 Forks
12 Watching
1 Branches
46 Contributors
Updated on 27 Nov 2024
Bundle Size
31.36 kB
Minified
10.97 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
83,418,068
Last day
0%
179,701
Compared to previous day
Last week
7.8%
989,351
Compared to previous week
Last month
13.7%
3,887,774
Compared to previous month
Last year
75.2%
36,718,923
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
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.
- to learn HTML, see MDN and WHATWG HTML
- for more about us, see
unifiedjs.com
- for updates, see Twitter
- for questions, see support
- to help, see contribute or sponsor below
Contents
- What is this?
- When should I use this?
- Plugins
- Types
- Compatibility
- Security
- Contribute
- Sponsor
- License
What is this?
With this project and a plugin, you can turn this HTML:
1<!doctype html> 2<html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Saturn</title> 6 </head> 7 <body> 8 <h1>Saturn</h1> 9 <p>Saturn is a gas giant composed predominantly of hydrogen and helium.</p> 10 </body> 11</html>
…into the following HTML:
1<!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.
Show example code
1import rehypeParse from 'rehype-parse' 2import rehypePresetMinify from 'rehype-preset-minify' 3import rehypeStringify from 'rehype-stringify' 4import {unified} from 'unified' 5 6const file = await unified() 7 .use(rehypeParse) 8 .use(rehypePresetMinify) 9 .use(rehypeStringify).process(`<!doctype html> 10<html lang="en"> 11 <head> 12 <meta charset="utf-8"> 13 <title>Saturn</title> 14 </head> 15 <body> 16 <h1>Saturn</h1> 17 <p>Saturn is a gas giant composed predominantly of hydrogen and helium.</p> 18 </body> 19</html>`) 20 21console.log(String(file))
With another plugin, you can turn this HTML:
1<h1>Hi, Saturn!</h1>
…into the following HTML:
1<h2>Hi, Saturn!</h2>
Show example code
1/** 2 * @import {Root} from 'hast' 3 */ 4 5import rehypeParse from 'rehype-parse' 6import rehypeStringify from 'rehype-stringify' 7import {unified} from 'unified' 8import {visit} from 'unist-util-visit' 9 10const file = await unified() 11 .use(rehypeParse, {fragment: true}) 12 .use(myRehypePluginToIncreaseHeadings) 13 .use(rehypeStringify) 14 .process('<h1>Hi, Saturn!</h1>') 15 16console.log(String(file)) 17 18function myRehypePluginToIncreaseHeadings() { 19 /** 20 * @param {Root} tree 21 */ 22 return function (tree) { 23 visit(tree, 'element', function (node) { 24 if (['h1', 'h2', 'h3', 'h4', 'h5'].includes(node.tagName)) { 25 node.tagName = 'h' + (Number(node.tagName.charAt(1)) + 1) 26 } 27 }) 28 } 29}
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 outputrehype
—unified
,rehype-parse
, andrehype-stringify
, useful when input and output are HTMLrehype-cli
— CLI aroundrehype
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:
awesome-rehype
— selection of the most awesome projects- List of plugins — list of all plugins
rehype-plugin
topic — any tagged repo on GitHub
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!
Vercel |
Motif |
HashiCorp |
GitBook |
Gatsby | |||||
Netlify |
Coinbase |
ThemeIsle |
Expo |
Boost Note |
Markdown Space |
Holloway | |||
You? |
License
MIT © Titus Wormer
No vulnerabilities found.
Reason
13 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/rehypejs/.github/security.md:1
- Info: Found linked content: github.com/rehypejs/.github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/rehypejs/.github/security.md:1
- Info: Found text in security policy: github.com/rehypejs/.github/security.md:1
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/bb.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bb.yml:5: update your workflow using https://app.stepsecurity.io/secureworkflow/rehypejs/rehype/bb.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:6: update your workflow using https://app.stepsecurity.io/secureworkflow/rehypejs/rehype/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:7: update your workflow using https://app.stepsecurity.io/secureworkflow/rehypejs/rehype/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/rehypejs/rehype/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:11
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 9 are checked with a SAST tool
Score
5.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to rehype-stringify
rehype
HTML processor powered by plugins part of the unified collective
rehype-rewrite
Rewrite element with rehype.
rehype-external-links
rehype plugin to automatically add `target` and `rel` attributes to external links
remark-rehype
remark plugin that turns markdown into HTML to support rehype