Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rehype-format

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rehype-format

rehype plugin to format HTML

  • 4.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
113K
increased by6.14%
Maintainers
2
Weekly downloads
 
Created
Source

rehype-format

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to format HTML.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install rehype-format

Use

Say we have the following file, index.html:

<!doCTYPE HTML><html>
 <head>
    <title>Hello!</title>
<meta charset=utf8>
      </head>
  <body><section>    <p>hi there</p>
     </section>
 </body>
</html>

And our module, example.js, looks as follows:

import {readSync} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {rehype} from 'rehype'
import rehypeFormat from 'rehype-format'

const file = readSync('index.html')

rehype()
  .use(rehypeFormat)
  .process(file)
  .then((file) => {
    console.error(reporter(file))
    console.log(String(file))
  })

Now, running node example yields:

index.html: no issues found
<!doctype html>
<html>
  <head>
    <title>Hello!</title>
    <meta charset="utf8">
  </head>
  <body>
    <section>
      <p>hi there</p>
    </section>
  </body>
</html>

API

This package exports no identifiers. The default export is rehypeFormat.

unified().use(rehypeFormat[, options])

Format whitespace in the processed tree.

  • Collapse all white space (to a single space or newline)
  • Remove unneeded white space
  • Inject needed newlines and indentation
  • Indent previously collapsed newlines properly

All superfluous white space is removed. However, as newlines are kept (and later properly indented), your code will still line-wrap as expected.

options
options.indent

Indentation per level (number, string, default: 2). When number, uses that amount of spaces. When string, uses that per indentation level.

options.indentInitial

Whether to indent the first level (boolean, default: true). This is usually the <html>, thus not indenting head and body.

options.blanks

List of tag names to join with a blank line (Array.<string>, default: []). These tags, when next to each other, are joined by a blank line (\n\n). For example, when ['head', 'body'] is given, a blank line is added between these two.

Security

Use of rehype-format changes white space in the syntax tree. White space in <script>, <style>, <pre>, or <textarea> is not modified. If the tree is already safe, use of this plugin does not open you up for a cross-site scripting (XSS) attack. When in doubt, use rehype-sanitize.

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md for ways to get help.

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

License

MIT © Titus Wormer

Keywords

FAQs

Package last updated on 01 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc