All Projects β†’ magiclen β†’ html-minifier

magiclen / html-minifier

Licence: MIT license
This library can help you generate and minify your HTML code at the same time. It also supports to minify JS and CSS in `<style>`, `<script>` elements, and ignores the minification of `<pre>` elements.

Programming Languages

rust
11053 projects
HTML
75241 projects

Projects that are alternatives of or similar to html-minifier

MinifyAllCli
πŸ“¦ A lightweight, simple and easy npm tool to π—Ίπ—Άπ—»π—Άπ—³π˜† JSON/C, HTML and CSS! Also known as MinifyAll core! ⭐ Usable as π‘ͺ𝑳𝑰 tool or π’Šπ’Žπ’‘π’π’“π’•π’‚π’ƒπ’π’† in TS/JS as a 𝑴𝑢𝑫𝑼𝑳𝑬 πŸ₯°
Stars: ✭ 21 (+40%)
Mutual labels:  minify-html
alter.email
Transform your HTML emails.
Stars: ✭ 66 (+340%)
Mutual labels:  minify-html
MinifyAll
A 𝗩𝗦𝗖𝗼𝗱𝗲 𝗺𝗢𝗻𝗢𝗳𝗢𝗲𝗿 for JS, JSON/C, CSS, and HTML, you will love its simplicity! 🌟 π˜Ύπ™€π™’π™₯π™§π™šπ™¨π™¨ and π™œπ™―π™žπ™₯ files and folders πŸ“¦ Reduce your bundle and file sizes with lightning speed ⚑
Stars: ✭ 54 (+260%)
Mutual labels:  minify-html
Laravel Page Speed
Package to optimize your site automatically which results in a 35%+ optimization
Stars: ✭ 2,097 (+13880%)
Mutual labels:  minify-html
kirby-html-minifier
Minify the html output of a site built with Kirby CMS
Stars: ✭ 20 (+33.33%)
Mutual labels:  minify-html

HTML Minifier

CI

This library can help you generate and minify your HTML code at the same time. It also supports to minify JS and CSS in <style>, <script> elements, and ignores the minification of <pre>, <code> and <textarea> elements.

HTML is minified by the following rules:

  • ASCII control characters (0x00-0x08, 0x11-0x1F, 0x7F) are always removed.
  • Comments can be optionally removed. (removed by default)
  • Useless whitespaces (spaces, tabs and newlines) are removed.
  • Whitespaces (spaces, tabs and newlines) are converted to a single '\x20' or a single '\n', if possible.
  • Empty attribute values are collapsed. (e.g <input readonly=""> => <input readonly> )
  • The inner HTML of all elements is minified except for the following elements:
    • <pre>
    • <textarea>
    • <code> (optionally, minified by default)
    • <style> (if the type attribute is unsupported)
    • <script> (if the type attribute is unsupported)
  • JS code and CSS code in <script> and <style> elements are minified by minifier.

The original (non-minified) HTML doesn't need to be completely generated before using this library because this library doesn't do any deserialization to create DOMs.

Examples

use html_minifier::HTMLMinifier;

let mut html_minifier = HTMLMinifier::new();

html_minifier.digest("<!DOCTYPE html>   <html  ").unwrap();
html_minifier.digest("lang=  en >").unwrap();
html_minifier.digest("
<head>
    <meta name=viewport>
</head>
").unwrap();
html_minifier.digest("
<body class=' container   bg-light '>
    <input type='text' value='123   456' readonly=''  />

    123456
    <b>big</b> 789
    ab
    c
    δΈ­ζ–‡
    ε­—
</body>
").unwrap();
html_minifier.digest("</html  >").unwrap();

assert_eq!("<!DOCTYPE html> <html lang=en>
<head>
<meta name=viewport>
</head>
<body class='container bg-light'>
<input type='text' value='123   456' readonly/>
123456
<b>big</b> 789
ab
c
δΈ­ζ–‡
ε­—
</body>
</html>".as_bytes(), html_minifier.get_html());
use html_minifier::HTMLMinifier;

let mut html_minifier = HTMLMinifier::new();

html_minifier.digest("<pre  >   Hello  world!   </pre  >").unwrap();

assert_eq!(b"<pre>   Hello  world!   </pre>", html_minifier.get_html());
use html_minifier::HTMLMinifier;

let mut html_minifier = HTMLMinifier::new();

html_minifier.digest("<script type='  application/javascript '>   alert('Hello!')    ;   </script>").unwrap();

assert_eq!("<script type='application/javascript'>alert('Hello!')</script>".as_bytes(), html_minifier.get_html());

Write HTML to a Writer

If you don't want to store your HTML in memory (e.g. writing to a file instead), you can use the HTMLMinifierHelper struct which provides a low-level API that allows you to pass your output instance when invoking the digest method.

use html_minifier::HTMLMinifierHelper;

use std::fs::File;
use std::io::Read;

let mut input_file = File::open("tests/data/w3schools.com_tryhow_css_example_website.htm").unwrap();
let mut output_file = File::create("tests/data/index.min.html").unwrap();

let mut buffer = [0u8; 256];

let mut html_minifier_helper = HTMLMinifierHelper::new();

loop {
    let c = input_file.read(&mut buffer).unwrap();

    if c == 0 {
        break;
    }

    html_minifier_helper.digest(&buffer[..c], &mut output_file).unwrap();
}

No Std

Disable the default features to compile this crate without std.

[dependencies.html-minifier]
version = "*"
default-features = false

Crates.io

https://crates.io/crates/html-minifier

Documentation

https://docs.rs/html-minifier

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].