### Node.js: Install Package Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Install the @minify-html/node package for Node.js using either npm or Yarn. ```bash npm i @minify-html/node ``` ```bash yarn add @minify-html/node ``` -------------------------------- ### Install Deno Package Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Add the package to your Deno project using the JSR registry. ```bash deno add jsr:@minify-html/deno ``` -------------------------------- ### CLI minhtml Command Usage Source: https://context7.com/wilsonzlin/minify-html/llms.txt Examples of using the minhtml CLI tool for single-file minification, stdin/stdout piping, and parallel batch processing. ```bash # Install via Cargo cargo install minhtml # Minify a single file with output minhtml --output /path/to/output.min.html --keep-closing-tags --minify-css /path/to/src.html # Minify from stdin to stdout cat index.html | minhtml --minify-js --minify-css > index.min.html # Parallel batch processing (modifies files in place) minhtml --keep-closing-tags --minify-css /path/to/**/*.html # Full configuration example minhtml \ --minify-js \ --minify-css \ --minify-doctype \ --allow-removing-spaces-between-attributes \ --remove-processing-instructions \ --preserve-brace-template-syntax \ --output dist/index.html \ src/index.html ``` -------------------------------- ### Content elements whitespace example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Demonstrates whitespace trimming and collapsing in content elements like paragraphs. ```html

↵ ··Hey,·I·just·found↵ ··out·about·this·cool·website!↵ ··[1]

``` -------------------------------- ### Script tag parsing examples Source: https://github.com/wilsonzlin/minify-html/blob/master/notes/Script data.md Various scenarios showing how script tags and HTML comments interact during parsing. ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` -------------------------------- ### Deno/WASM API Usage Source: https://context7.com/wilsonzlin/minify-html/llms.txt This snippet demonstrates how to initialize the WASM module and minify HTML content using the Deno/WASM binding. It includes an example with various configuration options and a helper function for repeated use. ```APIDOC ## Deno/WASM API - @minify-html/deno The Deno/WASM binding requires initialization before use and works with Uint8Array data. ```typescript import init, { minify } from "@minify-html/deno"; // For WASM in browser: import init, { minify } from "@minify-html/wasm"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); // Initialize the WASM module (required before first use) await init(); const html = ` Deno Example

Card Title

Card content goes here.

`; // Minify with configuration const minified = decoder.decode( minify(encoder.encode(html), { minify_js: true, minify_css: true, minify_doctype: true, keep_closing_tags: false, keep_comments: false, allow_removing_spaces_between_attributes: true }) ); console.log(minified); // Output: Deno Example

Card Title

Card content goes here.

// Helper function for repeated use function minifyHtml(html: string, options = {}): string { return decoder.decode( minify(encoder.encode(html), { minify_js: true, minify_css: true, ...options }) ); } ``` ``` -------------------------------- ### Deno: Minify HTML with Options Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Minify HTML content in Deno using the WASM binding. This example shows how to initialize the library and minify a string, with options to preserve spaces between attributes and comments. ```typescript import init, {minify} from "@minify-html/deno"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); await init(); const minified = decoder.decode(minify(encoder.encode("

Hello, world!

"), { keep_spaces_between_attributes: true, keep_comments: true })); ``` -------------------------------- ### Collapse whitespace example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Reduces sequences of whitespace characters in text nodes to a single space. ```html

↵ ··The·quick·brown·fox↵ ··jumps·over·the·lazy↵ ··dog.↵

``` ```html

·The·quick·brown·fox·jumps·over·the·lazy·dog.·

``` -------------------------------- ### HTML Whitespace Collapse Example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Demonstrates how sequences of whitespace characters in text nodes are reduced to a single space. This method applies to most elements, excluding whitespace-sensitive ones. ```html

The·quick·brown·fox jumps·over·the·lazy dog.

``` ```html

The·quick·brown·fox·jumps·over·the·lazy·dog.

``` -------------------------------- ### Destroy whole whitespace example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Removes text nodes between tags that consist only of whitespace. ```html ``` ```html ``` -------------------------------- ### Trim whitespace example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Removes leading and trailing whitespace from text nodes within a tag. ```html

↵ ··Hey,·I·just·found↵ ··out·about·this·cool·website!↵ ··[1]

``` ```html

Hey,·I·just·found↵ ··out·about·this·cool·website!↵ ··[1]

``` -------------------------------- ### Attribute Name Character Set Source: https://github.com/wilsonzlin/minify-html/blob/master/notes/Parsing.md An attribute name can start with any character except whitespace, `/`, or `>`. The `=` character is permitted within the name. The name continues until the next `=`, `/`, `>`, or whitespace. ```html == "a": {}#$'=/> ``` -------------------------------- ### Interpreting Literal '<' in Content Source: https://github.com/wilsonzlin/minify-html/blob/master/notes/Parsing.md If a `<` character in the content is not followed by an alphanumeric, `:`, or `=` character, it is treated as a literal character, not the start of a tag. ```html
< /div>< span> ``` -------------------------------- ### HTML Whitespace Trimming Example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Shows the removal of leading and trailing whitespace from text nodes adjacent to a tag. This method is used for elements that are not whitespace-sensitive or formatting elements. ```html

Hey,·I·just·found out·about·this·cool·website! [1]

``` ```html

Hey,·I·just·found out·about·this·cool·website! [1]

``` -------------------------------- ### Handling Malformed Script/Textarea Tags Source: https://github.com/wilsonzlin/minify-html/blob/master/notes/Parsing.md Special tags like script, style, textarea, and title do not close until a case-insensitive ` ``` -------------------------------- ### Collapse Whitespace in Paragraphs Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-python/README.md Whitespace within paragraph tags is collapsed. This example shows the 'before' and 'after' states of a paragraph with emphasis tags. ```html

Hey,·just·found·out·about·this·cool·website!·[1]

``` -------------------------------- ### HTML Whitespace Destruction Example Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Illustrates the removal of text nodes that consist solely of whitespace characters between tags. This is applicable to elements that are not whitespace-sensitive, content, content-first, or formatting elements. ```html ``` ```html ``` -------------------------------- ### Minify HTML with Java Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Use the Configuration.Builder to set options and minify HTML strings. ```java import in.wilsonl.minifyhtml.Configuration; import in.wilsonl.minifyhtml.MinifyHtml; Configuration cfg = new Configuration.Builder() .setKeepHtmlAndHeadOpeningTags(true) .setMinifyCss(true) .build(); String minified = MinifyHtml.minify("

Hello, world!

", cfg); ``` -------------------------------- ### Configuration Options Reference Source: https://context7.com/wilsonzlin/minify-html/llms.txt This section details all available configuration options for the minify-html library, controlling various aspects of the HTML minification process. ```APIDOC ## Configuration Options Reference All configuration options control minification behavior. All options default to `false`. ```typescript interface MinifyConfig { // Minification options minify_js: boolean; // Minify JavaScript in "; // Create configuration with all options let mut cfg = Cfg::new(); cfg.minify_js = true; cfg.minify_css = true; cfg.keep_closing_tags = false; cfg.keep_comments = false; cfg.keep_html_and_head_opening_tags = false; cfg.remove_processing_instructions = true; let minified = minify(html, &cfg); let result = String::from_utf8(minified).unwrap(); // Output: Hello World

Hello, world!

println!("{}", result); } // Enable all non-compliant optimizations for maximum compression fn aggressive_minify(html: &[u8]) -> Vec { let mut cfg = Cfg::new(); cfg.minify_js = true; cfg.minify_css = true; cfg.enable_possibly_noncompliant(); // Enables: minify_doctype, allow_optimal_entities, allow_removing_spaces_between_attributes, allow_noncompliant_unquoted_attribute_values minify(html, &cfg) } // Preserve template syntax for Jinja/Handlebars fn minify_template(html: &[u8]) -> Vec { let mut cfg = Cfg::new(); cfg.minify_js = true; cfg.minify_css = true; cfg.preserve_brace_template_syntax = true; // Preserves {{ }}, {% %}, {# #} cfg.preserve_chevron_percent_template_syntax = true; // Preserves <% %> minify(html, &cfg) } ``` -------------------------------- ### Node.js API - @minify-html/node Source: https://context7.com/wilsonzlin/minify-html/llms.txt The Node.js binding provides a `minify` function that accepts a Buffer and configuration object, returning a minified Buffer. ```APIDOC ## Node.js API - @minify-html/node The Node.js binding provides a `minify` function that accepts a Buffer and configuration object, returning a minified Buffer. ### Usage ```typescript import { Buffer } from "node:buffer"; import minifyHtml from "@minify-html/node"; // CommonJS: const minifyHtml = require("@minify-html/node"); // Basic usage const html = Buffer.from(` Example

Hello, world!

`); const minified = minifyHtml.minify(html, { minify_js: true, minify_css: true, minify_doctype: true, keep_closing_tags: false, keep_comments: false, keep_html_and_head_opening_tags: false, allow_removing_spaces_between_attributes: true }); console.log(minified.toString()); // Output: Example

Hello, world!

// Preserve template syntax for frameworks like Handlebars or Nunjucks const templateHtml = Buffer.from(`
{{ user.name }} {% if user.admin %} Admin {% endif %}
`); const minifiedTemplate = minifyHtml.minify(templateHtml, { preserve_brace_template_syntax: true, minify_css: true }); console.log(minifiedTemplate.toString()); // Output:
{{ user.name }}{% if user.admin %}Admin{% endif %}
``` ### Configuration Options - **minify_js** (boolean) - Optional - Whether to minify JavaScript. - **minify_css** (boolean) - Optional - Whether to minify CSS. - **minify_doctype** (boolean) - Optional - Whether to minify the doctype declaration. - **keep_closing_tags** (boolean) - Optional - Whether to keep closing tags. - **keep_comments** (boolean) - Optional - Whether to keep comments. - **keep_html_and_head_opening_tags** (boolean) - Optional - Whether to keep `` and `` opening tags. - **allow_removing_spaces_between_attributes** (boolean) - Optional - Whether to allow removing spaces between attributes. - **preserve_brace_template_syntax** (boolean) - Optional - Whether to preserve brace template syntax (e.g., for Handlebars or Nunjucks). ``` -------------------------------- ### Minify HTML in Python Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Use the minify-html package where configuration options are passed as keyword arguments. ```python import minify_html minified = minify_html.minify("

Hello, world!

", minify_js=True, remove_processing_instructions=True) ``` -------------------------------- ### Python API - minify_html Source: https://context7.com/wilsonzlin/minify-html/llms.txt The Python binding exposes a `minify` function that accepts HTML as a string and configuration as keyword arguments. ```APIDOC ## Python API - minify_html The Python binding exposes a `minify` function that accepts HTML as a string and configuration as keyword arguments. ### Usage ```python import minify_html # Basic minification html = """ Python Example

Welcome

""" # Full minification with JS and CSS minified = minify_html.minify( html, minify_js=True, minify_css=True, minify_doctype=True, keep_closing_tags=False, keep_comments=False, remove_processing_instructions=True, allow_removing_spaces_between_attributes=True ) print(minified) # Output: Python Example

Welcome

# Minify Django/Jinja templates template_html = """
{% for item in items %} {{ item.name }} {% endfor %}
""" minified_template = minify_html.minify( template_html, preserve_brace_template_syntax=True ) print(minified_template) # Output:
{% for item in items %}{{ item.name }}{% endfor %}
``` ### Configuration Options - **minify_js** (boolean) - Optional - Whether to minify JavaScript. - **minify_css** (boolean) - Optional - Whether to minify CSS. - **minify_doctype** (boolean) - Optional - Whether to minify the doctype declaration. - **keep_closing_tags** (boolean) - Optional - Whether to keep closing tags. - **keep_comments** (boolean) - Optional - Whether to keep comments. - **remove_processing_instructions** (boolean) - Optional - Whether to remove processing instructions. - **allow_removing_spaces_between_attributes** (boolean) - Optional - Whether to allow removing spaces between attributes. - **preserve_brace_template_syntax** (boolean) - Optional - Whether to preserve brace template syntax (e.g., for Django or Jinja templates). ``` -------------------------------- ### Initialize and use minify-html in Deno Source: https://context7.com/wilsonzlin/minify-html/llms.txt Requires initialization of the WASM module before calling the minify function. Input and output data must be encoded and decoded using TextEncoder and TextDecoder respectively. ```typescript import init, { minify } from "@minify-html/deno"; // For WASM in browser: import init, { minify } from "@minify-html/wasm"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); // Initialize the WASM module (required before first use) await init(); const html = ` Deno Example

Card Title

Card content goes here.

`; // Minify with configuration const minified = decoder.decode( minify(encoder.encode(html), { minify_js: true, minify_css: true, minify_doctype: true, keep_closing_tags: false, keep_comments: false, allow_removing_spaces_between_attributes: true }) ); console.log(minified); // Output: Deno Example

Card Title

Card content goes here.

// Helper function for repeated use function minifyHtml(html: string, options = {}): string { return decoder.decode( minify(encoder.encode(html), { minify_js: true, minify_css: true, ...options }) ); } ``` -------------------------------- ### Define minification configuration options Source: https://context7.com/wilsonzlin/minify-html/llms.txt Interface defining all available configuration flags for the minification process. All options default to false. ```typescript interface MinifyConfig { // Minification options minify_js: boolean; // Minify JavaScript in """; // Build configuration using the Builder pattern Configuration cfg = new Configuration.Builder() .setMinifyJs(true) .setMinifyCss(true) .setMinifyDoctype(true) .setKeepClosingTags(false) .setKeepComments(false) .setKeepHtmlAndHeadOpeningTags(false) .setAllowRemovingSpacesBetweenAttributes(true) .setRemoveProcessingInstructions(true) .build(); String minified = MinifyHtml.minify(html, cfg); System.out.println(minified); // Output: Java Example

Hello from Java!

} // Preserve JSP template syntax public static String minifyJsp(String html) { Configuration cfg = new Configuration.Builder() .setMinifyJs(true) .setMinifyCss(true) .setPreserveChevronPercentTemplateSyntax(true) // Preserves <% %> and <%= %> .build(); return MinifyHtml.minify(html, cfg); } } ``` -------------------------------- ### Minify HTML in Java Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Add the dependency via Maven and use the Configuration builder to set minification options. ```xml in.wilsonl.minifyhtml minify-html 0.18.1 ``` ```java import in.wilsonl.minifyhtml.Configuration; import in.wilsonl.minifyhtml.MinifyHtml; Configuration cfg = new Configuration.Builder() .setKeepHtmlAndHeadOpeningTags(true) .setMinifyCss(true) .build(); String minified = MinifyHtml.minify("

Hello, world!

", cfg); ``` -------------------------------- ### Legacy script data behavior Source: https://github.com/wilsonzlin/minify-html/blob/master/notes/Script data.md Demonstrates how nested script tags within a script block are treated as text rather than executable code. ```html ``` -------------------------------- ### Ruby API: Minify HTML with Options Source: https://context7.com/wilsonzlin/minify-html/llms.txt Use the global `minify_html` function with an HTML string and a hash of options. Options include `minify_js`, `minify_css`, and `keep_comments`. ```ruby require 'minify_html' # Basic minification html = <<~HTML Ruby Example HTML # Minify with full configuration minified = minify_html(html, { :minify_js => true, :minify_css => true, :minify_doctype => true, :keep_closing_tags => false, :keep_comments => false, :allow_removing_spaces_between_attributes => true, :remove_processing_instructions => true }) puts minified # Output: Ruby Example # Preserve ERB template syntax erb_html = <<~ERB
    <% @items.each do |item| %>
  • <%= item.name %>
  • <% end %>
ERB minified_erb = minify_html(erb_html, { :preserve_chevron_percent_template_syntax => true }) puts minified_erb # Output:
    <% @items.each do |item| %>
  • <%= item.name %>
  • <% end %>
``` -------------------------------- ### Rust: Add Dependency Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Add the minify-html crate to your Rust project's Cargo.toml file to include its minification capabilities. ```toml [dependencies] minify-html = "0.18.1" ``` -------------------------------- ### Add Maven Dependency for Java Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Include the minify-html library in your project via Maven. ```xml in.wilsonl.minifyhtml minify-html 0.18.1 ``` -------------------------------- ### CLI: Minify HTML File Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Use the minhtml CLI to minify a single HTML file, with options to keep closing tags and minify CSS within the HTML. ```bash minhtml --output /path/to/output.min.html --keep-closing-tags --minify-css /path/to/src.html ``` -------------------------------- ### CLI: Parallel File Processing Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Process multiple HTML files in place using the minhtml CLI, applying options like keeping closing tags and minifying CSS. ```bash minhtml --keep-closing-tags --minify-css /path/to/**/*.html ``` -------------------------------- ### Enable Non-Compliant Minification in Rust Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-onepass-python/README.md Enables all options that may output non-compliant HTML for further minification. Use with caution as it might affect validation. ```rust use minify_html::Cfg; let mut cfg = Cfg::new(); cfg.enable_possibly_noncompliant(); ``` -------------------------------- ### Enable Non-Compliant Minification in Rust Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html-python/README.md Enables all options that may output non-compliant but renderable HTML. Use this to achieve maximum compression when strict validation is not required. ```rust Cfg::enable_possibly_noncompliant ``` -------------------------------- ### Minify HTML in Node.js Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Use the @minify-html/node package to minify HTML strings. Configuration fields are provided as snake_case properties. ```ts import { Buffer } from "node:buffer"; import minifyHtml from "@minify-html/node"; // Or `const minifyHtml = require("@minify-html/node")` if not using TS/ESM. const minified = minifyHtml.minify(Buffer.from("

Hello, world!

"), { keep_spaces_between_attributes: true, keep_comments: true }); ``` -------------------------------- ### Collapse Whitespace in Content-First Elements Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Illustrates whitespace handling for elements that contain content, such as `
  • ` with an `
    ` child. ```html
  • ↵ ··
    ↵ ····
    ↵ ····
    ↵ ··
  • ``` ```html
  • ``` -------------------------------- ### Minify HTML in Ruby Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Use the minify_html gem to process HTML strings with a hash of configuration options. ```ruby require 'minify_html' print minify_html("

    Hello, world!

    ", { :keep_spaces_between_attributes => true, :minify_js => true }) ``` -------------------------------- ### Ignoring Subsequent HTML, Head, and Body Tags Source: https://github.com/wilsonzlin/minify-html/blob/master/notes/Parsing.md Any ``, ``, or `` tags encountered after the first instance of each are ignored, simplifying the document structure. ```html
    ``` -------------------------------- ### Minify HTML with WebAssembly Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Use the @minify-html/wasm package, requiring a TextEncoder/Decoder to handle data buffers. ```ts import init, {minify} from "@minify-html/wasm"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); await init(); const minified = decoder.decode(minify(encoder.encode("

    Hello, world!

    "), { keep_spaces_between_attributes: true, keep_comments: true })); ``` -------------------------------- ### Collapse Whitespace in Paragraphs Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Demonstrates how whitespace within paragraph tags is collapsed. ```html

    Hey,·I·just·found·out·about·this·cool·website!·[1]

    ``` -------------------------------- ### Minify HTML in Node.js Source: https://context7.com/wilsonzlin/minify-html/llms.txt Uses the @minify-html/node package to minify HTML buffers with configurable options. ```typescript import { Buffer } from "node:buffer"; import minifyHtml from "@minify-html/node"; // CommonJS: const minifyHtml = require("@minify-html/node"); // Basic usage const html = Buffer.from(` Example

    Hello, world!

    `); const minified = minifyHtml.minify(html, { minify_js: true, minify_css: true, minify_doctype: true, keep_closing_tags: false, keep_comments: false, keep_html_and_head_opening_tags: false, allow_removing_spaces_between_attributes: true }); console.log(minified.toString()); // Output: Example

    Hello, world!

    // Preserve template syntax for frameworks like Handlebars or Nunjucks const templateHtml = Buffer.from(`
    {{ user.name }} {% if user.admin %} Admin {% endif %}
    `); const minifiedTemplate = minifyHtml.minify(templateHtml, { preserve_brace_template_syntax: true, minify_css: true }); console.log(minifiedTemplate.toString()); // Output:
    {{ user.name }}{% if user.admin %}Admin{% endif %}
    ``` -------------------------------- ### Collapse Whitespace in Layout Elements Source: https://github.com/wilsonzlin/minify-html/blob/master/minify-html/README.md Shows whitespace trimming and collapsing for layout elements like `