Giter Site home page Giter Site logo

html-minifier's Introduction

html-minifier

html-minifier embeds the widely-used html-minifier NPM package in an easy-to-use Crystal shard via duktape.cr, which provides a fast, embedded Javascript execution environment within Crystal.

html-minifier can be used to minify arbitrary HTML content, including Javascript and/or CSS.

Some features:

  • minifies HTML and any embedded CSS/Javascript within the HTML
  • no non-Crystal dependencies (no Node.js or NPM required)
  • all html-minifier Javascript is baked into the shard, so you won't need to package any extra files with your app/tool/library
  • doesn't embed an entire Node.js runtime (Javascript is executed via duktape.cr)
  • simple, Crystal-based API (HtmlMinifier.minify!("source code")
  • full support for html-minifier options via HtmlMinifier.set_options
  • sane, one-size-fits-all options are included by default (unlike html-minifier on NPM)

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      html-minifier:
        github: sam0x17/html-minifier
  2. Run shards install

Minification

require "html-minifier"

HtmlMinifier.minify!("<html>  <body>minify  me!</body></html>") # => "<html> <body>minify me!</body></html>"
HtmlMinifier.minify!("<style>body { background-color: black }</style>") # => "<style>body{background-color:#000}</style>"
HtmlMinifier.minify!("<script> alert('hello world');</script>") # => "<script>alert(\"hello world\")</script>"

Configuration

All options supported by html-minifier on NPM are supported by this shard. Options can be specified by a JSON::Any object or by a JSON string, as shown below.

require "html-minifier"

HtmlMinifier.minify!("<html><!-- comment --></html>") # => "<html></html>"

HtmlMinifier.set_options("{\"removeComments\": false}")

HtmlMinifier.minify!("<html><!-- comment --></html>") # => "<html><!-- comment --></html>"

Note that user-specified options will override their respective default values. The default values for all options are shown below:

{
  "caseSensitive": true,
  "conservativeCollapse": true,
  "minifyCSS": true,
  "minifyJS": true,
  "useShortDoctype": true,
  "removeTagWhitespace": true,
  "removeScriptTypeAttributes": true,
  "removeComments": true,
  "collapseWhitespace": true,
  "collapseInlineTagWhitespace": true,
}

html-minifier's People

Contributors

sam0x17 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.