npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

metalsmith-optimize-html

v0.2.0

Published

A Metalsmith plugin to optimize HTML files

Downloads

391

Readme

Metalsmith Optimize HTML

npm: version license: MIT

A Metalsmith plugin for optimizing and minifying HTML files - UNDER DEVELOPMENT

Build with lots of inspiration from htmlcompressor. This plugin optimizes HTML files by removing unnecessary whitespace, comments, and attributes. It also normalizes URLs, boolean attributes, and data attributes.

Why this plugin?

There was a time when Metalsmith HTML Minifier was the go-to plugin for HTML optimization. However, it looks abondoned and has some serious security issues. It is build as a wrapper for HTML Minifier which has now a security fix, but sadly the wrapper has not been updated. This plugin is build from scratch with just a few up-to-date dependencies. This is still a work in progress, but it is already usable. Please test it and report any issues you find.

Installation

npm install metalsmith-optimize-html

Usage

JavaScript API

import Metalsmith from 'metalsmith'
import optimizeHTML from 'metalsmith-optimize-html'

Metalsmith(__dirname)
  .use(optimizeHTML({
    // options
    removeComments: true,
    removeTagSpaces: true
  }))

CLI Usage

In your metalsmith.json:

{
  "plugins": {
    "metalsmith-optimize-html": {
      "removeComments": true,
      "removeTagSpaces": true,
      "simplifyDoctype": true
    }
  }
}

Options

Core Optimization (Always On)

Whitespace normalization

  • Collapses multiple whitespace to single space
  • Removes whitespace between HTML tags
  • Preserves whitespace in <pre>, <code>, <textarea>, <script>, <style> tags

Optional Optimizations

removeComments: boolean (default: false)

  • Removes all HTML comments
<!-- This comment will be removed -->

removeTagSpaces: boolean (default: false)

  • Removes extra spaces inside HTML tags
  • Normalizes spaces between attributes
<div   class="example"   id="test"  >
<!-- becomes -->
<div class="example" id="test">

removeDefaultAttributes: boolean (default: false)

  • Removes common default attributes that browsers assume anyway
<script type="text/javascript" src="main.js">
<link type="text/css" rel="stylesheet">
<form method="get">
<input type="text">
<!-- becomes -->
<script src="main.js">
<link rel="stylesheet">
<form>
<input>

normalizeBooleanAttributes: boolean (default: false)

  • Normalizes boolean attributes to their shorter form
<input type="checkbox" checked="checked" disabled="disabled">
<select multiple="multiple">
<!-- becomes -->
<input type="checkbox" checked disabled>
<select multiple>

cleanUrlAttributes: boolean (default: false)

  • Cleans and normalizes URLs in href, src, action, srcset, and data attributes
  • Removes unnecessary whitespace in URLs
<a href="  https://example.com/page   ">
<!-- becomes -->
<a href="https://example.com/page">

cleanDataAttributes: boolean (default: false)

  • Normalizes whitespace in data-* attribute values
  • Maintains valid JSON in data attributes
<div data-config='{ "key" :  "value" }'>
<!-- becomes -->
<div data-config='{"key":"value"}'>

removeEmptyAttributes: boolean (default: false)

  • Removes attributes with empty values
<div id="" class="   " data-value="">
<!-- becomes -->
<div>

removeProtocols: boolean (default: false)

  • Converts URLs to protocol-relative URLs
  • Preserves protocols in links with rel="external"
<a href="https://example.com">
<a href="http://example.com" rel="external">
<!-- becomes -->
<a href="//example.com">
<a href="http://example.com" rel="external">

simplifyDoctype: boolean (default: false)

  • Replaces any DOCTYPE declaration with HTML5 DOCTYPE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- becomes -->
<!DOCTYPE html>

aggressive: boolean (default: false)

  • Enables all optimizations with a single option:
    • removeComments
    • removeTagSpaces
    • removeDefaultAttributes
    • normalizeBooleanAttributes
    • cleanUrlAttributes
    • cleanDataAttributes
    • removeEmptyAttributes
    • removeProtocols
    • simplifyDoctype
Metalsmith(__dirname)
 .use(optimizeHTML({ aggressive: true }))

All individual option settings are ignored when aggressive is true, except when explicitly overridden:

Metalsmith(__dirname)
  .use(optimizeHTML({
    aggressive: true,
    removeComments: false  // This override will be respected
  }))

Debugging

Debug messages can be enabled by setting the DEBUG environment variable. Metalsmith is expecting the DEBUG environment variable in this format: metalsmith-<pluginName>. The debug variable should be set to metalsmith-htmlOptimize as we have previously imported the plugin as optimizeHTML.

metalsmith.env( 'DEBUG', 'metalsmith-htmlOptimize' );

Limitations and Edge Cases

Malformed HTML Comments

  • The plugin cannot safely handle nested or malformed HTML comments
  • If a comment is not properly closed, it might affect subsequent content

Preserved Content

  • Content within <pre>, <code>, <textarea>, <script>, and <style> tags is preserved
  • Whitespace and formatting within these tags remains untouched

URL Processing

  • Protocol removal only affects http:// and https:// protocols
  • Other protocols (ftp://, ws://, etc.) remain unchanged
  • Handles URLs in meta tags (og:url, twitter:url, canonical)
  • Processes SVG attributes (xmlns, xlink:href, href, src)

Data Attributes

  • JSON values in data attributes must be valid JSON
  • Invalid JSON structures are left unchanged

License

MIT