rehypejs/rehype-minify

plugins to minify HTML

101

stars

252

commits

JavaScript

primary language

Feb 19, 2025

updated

unifiedjs.com
html
mangle
minify
optimize
rehype
rehype-plugin
rehype-preset
Browse cluster: Rehype HTML Processing Plugins

README

rehype-minify

Build Coverage

rehype plugins to minify HTML.

Contents

What is this?

This GitHub repository is a monorepo that contains a couple utilities, ±30 plugins, and a preset with good and safe default, to minify HTML:

In
<!doctype html>
<html lang="en-GB">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="content-language" content="en-US">
    <script src="index.js" type="text/javascript" language="javascript">
      alert(true);
    </script>
    <link rel="stylesheet" href="index.css" type="text/css">
    <title>Foo  &amp;  bar</title>
  </head>
  <body>
    <h1 class="foo foo">bar  bar</h1>
    <p id="alfred" id="alfred">  <strong>foo</strong> <em>bar</em> </p>
    <button type="BUTTON" onclick="javascript:return false">Alpha</button>
  </body>
</html>
Out
<!doctypehtml><html lang=en><meta charset=utf8><script src=index.js></script><link href=index.css rel=stylesheet><title>Foo &#38 bar</title><h1 class=foo>bar bar</h1><p id=alfred><strong>foo</strong> <em>bar</em></p><button onclick=return!1 type=button>Alpha</button>

When should I use this?

This project is useful when you want to improve the size of HTML documents. It’s particularly useful when you’re already using rehype (and remark?) to process HTML.

Presets

Presets are combinations of plugins. One preset is maintained here:

Plugins

The following plugins maintained here are included in the above preset.

The following plugins are not included in the preset as they are potentially dangerous, can make sites slower in certain cases, or need extra configuration. Read their readmes before using:

Also: pass allowDangerousCharacters to rehype-stringify if you trust your content.

Benchmark

Here’s a benchmark comparing the results from html-minifier and rehype-minify. To summarize: differences are negligible, in fact, minifying HTML doesn’t matter much. What does matter is using good compressions, such as gzip. Note: html-minifier sometimes crashes, such as on amazon.

namerawgzip
originalrehype-minifyhtml-minifieroriginalrehype-minifyhtml-minifier
amazon567.85KB5.71%💥128.65KB5.07%💥
bbc531.54KB4.70%4.58%58.09KB1.85%1.96%
bootstrap33.96KB22.36%22.82%8.12KB5.17%5.36%
compat-table8.67MB16.34%16.74%275.06KB8.26%10.06%
ecmascript6.62MB10.86%11.56%789.66KB6.39%6.60%
eloquent80.08KB6.43%6.45%19.13KB1.25%1.26%
github233.14KB13.39%14.51%38.86KB7.48%7.91%
google253.98KB3.13%3.20%80.56KB3.64%3.63%
guardian765.54KB6.75%8.71%112.21KB3.96%4.38%
html12.72MB3.15%3.19%1.85MB5.49%5.58%
linkedin120.77KB32.29%32.34%14.4KB13.99%13.91%
microsoft174.26KB22.44%34.12%26.45KB12.96%15.45%
nbc1.64MB5.94%6.28%221.08KB1.86%1.92%
npm27.98KB3.03%2.61%8.28KB2.22%2.01%
nytimes822.38KB11.80%11.80%137.47KB2.71%2.72%
slack205.45KB4.76%4.55%49.63KB2.06%1.98%
stackoverflow169.8KB18.64%19.10%45.4KB6.42%6.67%
vice337.93KB3.48%3.22%92.58KB2.61%2.52%
wikipedia768.18KB6.08%6.76%144KB1.58%1.77%
total34.63MB31.9MB31.83MB4.05MB3.85MB3.85MB

Huge differences in results are suspicious and may point to bugs.

💥 — Crash.

Security

Use of rehype-preset-minify is safe by default if the tree is already safe. As rehype works on HTML and improper use of HTML can open you up to a cross-site scripting (XSS) attack, use of rehype can also be unsafe. Use rehype-sanitize to make the tree safe.

To further optimize the result disregarding security, use the extra plugins listed above and pass allowDangerousCharacters to rehype-stringify.

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

Contributors

wooorm

246 commits

ajoslin

1 commits

j-f1

1 commits

rehypejs/rehype-minify

plugins to minify HTML

101

stars

252

commits

JavaScript

primary language

Feb 19, 2025

updated

unifiedjs.com
html
mangle
minify
optimize
rehype
rehype-plugin
rehype-preset
Browse cluster: Rehype HTML Processing Plugins

README

rehype-minify

Build Coverage

rehype plugins to minify HTML.

Contents

What is this?

This GitHub repository is a monorepo that contains a couple utilities, ±30 plugins, and a preset with good and safe default, to minify HTML:

In
<!doctype html>
<html lang="en-GB">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="content-language" content="en-US">
    <script src="index.js" type="text/javascript" language="javascript">
      alert(true);
    </script>
    <link rel="stylesheet" href="index.css" type="text/css">
    <title>Foo  &amp;  bar</title>
  </head>
  <body>
    <h1 class="foo foo">bar  bar</h1>
    <p id="alfred" id="alfred">  <strong>foo</strong> <em>bar</em> </p>
    <button type="BUTTON" onclick="javascript:return false">Alpha</button>
  </body>
</html>
Out
<!doctypehtml><html lang=en><meta charset=utf8><script src=index.js></script><link href=index.css rel=stylesheet><title>Foo &#38 bar</title><h1 class=foo>bar bar</h1><p id=alfred><strong>foo</strong> <em>bar</em></p><button onclick=return!1 type=button>Alpha</button>

When should I use this?

This project is useful when you want to improve the size of HTML documents. It’s particularly useful when you’re already using rehype (and remark?) to process HTML.

Presets

Presets are combinations of plugins. One preset is maintained here:

Plugins

The following plugins maintained here are included in the above preset.

The following plugins are not included in the preset as they are potentially dangerous, can make sites slower in certain cases, or need extra configuration. Read their readmes before using:

Also: pass allowDangerousCharacters to rehype-stringify if you trust your content.

Benchmark

Here’s a benchmark comparing the results from html-minifier and rehype-minify. To summarize: differences are negligible, in fact, minifying HTML doesn’t matter much. What does matter is using good compressions, such as gzip. Note: html-minifier sometimes crashes, such as on amazon.

namerawgzip
originalrehype-minifyhtml-minifieroriginalrehype-minifyhtml-minifier
amazon567.85KB5.71%💥128.65KB5.07%💥
bbc531.54KB4.70%4.58%58.09KB1.85%1.96%
bootstrap33.96KB22.36%22.82%8.12KB5.17%5.36%
compat-table8.67MB16.34%16.74%275.06KB8.26%10.06%
ecmascript6.62MB10.86%11.56%789.66KB6.39%6.60%
eloquent80.08KB6.43%6.45%19.13KB1.25%1.26%
github233.14KB13.39%14.51%38.86KB7.48%7.91%
google253.98KB3.13%3.20%80.56KB3.64%3.63%
guardian765.54KB6.75%8.71%112.21KB3.96%4.38%
html12.72MB3.15%3.19%1.85MB5.49%5.58%
linkedin120.77KB32.29%32.34%14.4KB13.99%13.91%
microsoft174.26KB22.44%34.12%26.45KB12.96%15.45%
nbc1.64MB5.94%6.28%221.08KB1.86%1.92%
npm27.98KB3.03%2.61%8.28KB2.22%2.01%
nytimes822.38KB11.80%11.80%137.47KB2.71%2.72%
slack205.45KB4.76%4.55%49.63KB2.06%1.98%
stackoverflow169.8KB18.64%19.10%45.4KB6.42%6.67%
vice337.93KB3.48%3.22%92.58KB2.61%2.52%
wikipedia768.18KB6.08%6.76%144KB1.58%1.77%
total34.63MB31.9MB31.83MB4.05MB3.85MB3.85MB

Huge differences in results are suspicious and may point to bugs.

💥 — Crash.

Security

Use of rehype-preset-minify is safe by default if the tree is already safe. As rehype works on HTML and improper use of HTML can open you up to a cross-site scripting (XSS) attack, use of rehype can also be unsafe. Use rehype-sanitize to make the tree safe.

To further optimize the result disregarding security, use the extra plugins listed above and pass allowDangerousCharacters to rehype-stringify.

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

Contributors

wooorm

246 commits

ajoslin

1 commits

j-f1

1 commits

Languages

JavaScript

100.0%