From 6aca8f4e50cac76618b426524d7daeeb78de5124 Mon Sep 17 00:00:00 2001 From: Kent Oyer Date: Wed, 30 Dec 2020 16:23:04 -0600 Subject: [PATCH 1/4] Add feature: Command Line Interface --- bin/cli.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ 2 files changed, 66 insertions(+) create mode 100644 bin/cli.js diff --git a/bin/cli.js b/bin/cli.js new file mode 100644 index 0000000..0713034 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,63 @@ +#!/usr/bin/env node + +"use strict"; + +var fs = require("fs"); +var path = require("path"); +var pkg = require("../package.json"); +var inlineCss = require('../'); + +var binname = Object.keys(pkg.bin)[0]; + +var options = {}; +var html = process.argv[2]; + +switch (html) { + case "--version": + case "-v": + console.log(binname + " v" + pkg.version); + + break; + + case "--help": + case "-h": + console.log("Usage: " + binname + " [FILE]"); + console.log(""); + console.log("Description:"); + console.log(" " + pkg.description); + console.log(""); + console.log("Options:"); + console.log(" -h, --help Show this message."); + console.log(" -v, --version Print version information."); + console.log(""); + console.log("With no FILE, or when FILE is -, read standard input."); + + break; + + case "-": + case undefined: + options.url = 'file://' + process.cwd() + '/'; + var stdin = process.openStdin(); + html = ""; + stdin.setEncoding("utf-8"); + stdin.on("data", function (chunk) { + html += chunk; + }); + stdin.on("end", function () { + inlineCss(html, options) + .then(function(html) { + console.log(html); + }); + }); + + break; + + default: + options.url = 'file://' + path.dirname(path.resolve(html)) + '/'; + html = fs.readFileSync(html, "utf8"); + inlineCss(html, options) + .then(function(html) { + console.log(html); + }); +} + diff --git a/package.json b/package.json index c81435b..e998f7f 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,9 @@ "index.js", "lib/" ], + "bin": { + "inline-css": "bin/cli.js" + }, "repository": "jonkemp/inline-css", "keywords": [ "inline", From 39a046cda328bffd6cb43568c895fe513f291e0d Mon Sep 17 00:00:00 2001 From: Kent Oyer Date: Wed, 30 Dec 2020 18:35:05 -0600 Subject: [PATCH 2/4] Update README.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 7b8daeb..30b3e6f 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,21 @@ inlineCss(html, options) .then(function(html) { console.log(html); }); ``` +## CLI +```console +$ inline-css -h +Usage: inline-css [FILE] + +Description: + Inline css into an html file. + +Options: + -h, --help Show this message. + -v, --version Print version information. + +With no FILE, or when FILE is -, read standard input. +``` + ## API ### inlineCss(html, options) From 24bcb9d0173f783681b2d1bd0cb8ec310c3c825a Mon Sep 17 00:00:00 2001 From: Kent Oyer Date: Thu, 7 Jan 2021 15:38:13 -0600 Subject: [PATCH 3/4] Update README.md --- README.md | 184 +++++------------------------------------------------- 1 file changed, 16 insertions(+), 168 deletions(-) diff --git a/README.md b/README.md index 7b8daeb..1166db9 100644 --- a/README.md +++ b/README.md @@ -1,183 +1,31 @@ -# inline-css [![npm](http://img.shields.io/npm/v/inline-css.svg?style=flat)](https://badge.fury.io/js/inline-css) [![Build Status](https://travis-ci.org/jonkemp/inline-css.svg?branch=master)](https://travis-ci.org/jonkemp/inline-css) [![Coverage Status](https://coveralls.io/repos/jonkemp/inline-css/badge.svg?branch=master&service=github)](https://coveralls.io/github/jonkemp/inline-css?branch=master) +# inline-css -[![NPM](https://nodei.co/npm/inline-css.png?downloads=true)](https://nodei.co/npm/inline-css/) +This is a fork of [jonkemp/inline-css](https://github.com/jonkemp/inline-css/) with the addition of a Command Line Interface (CLI) to make it easier to integrate this tool with other scripts and development tools. I needed this functionality to use this tool as a file watcher in PhpStorm. -> Inline your CSS properties into the `style` attribute in an html file. Useful for emails. +## Installation -Inspired by the [juice](https://github.com/Automattic/juice) library. - -## Features -- Uses [cheerio](https://github.com/cheeriojs/cheerio) instead of jsdom -- Works on Windows -- Preserves Doctype -- Modular -- Gets your CSS automatically through style and link tags -- Functions return [A+ compliant](https://promisesaplus.com/) Promises - -## How It Works - -This module takes html and inlines the CSS properties into the style attribute. - -`/path/to/file.html`: -```html - - - - - - -

Test

- - -``` - -`style.css` -```css -p { - text-decoration: underline; -} -``` - -Output: -```html - - - - -

Test

- - ``` - -## What is this useful for ? - -- HTML emails. For a comprehensive list of supported selectors see -[here](http://www.campaignmonitor.com/css/) -- Embedding HTML in 3rd-party websites. -- Performance. Downloading external stylesheets delays the rendering of the page in the browser. Inlining CSS speeds up this process because the browser doesn't have to wait to download an external stylesheet to start rendering the page. - - -## Install - -Install with [npm](https://npmjs.org/package/inline-css) - -``` -npm install --save inline-css +npm install --save fkoyer/inline-css ``` ## Usage -```js -var inlineCss = require('inline-css'); -var html = "
"; - -inlineCss(html, options) - .then(function(html) { console.log(html); }); -``` - -## API - -### inlineCss(html, options) - - -#### options.extraCss - -Type: `String` -Default: `""` - -Extra css to apply to the file. - - -#### options.applyStyleTags - -Type: `Boolean` -Default: `true` - -Whether to inline styles in ``. - - -#### options.applyLinkTags - -Type: `Boolean` -Default: `true` - -Whether to resolve `` tags and inline the resulting styles. +```console +$ inline-css -h +Usage: inline-css [FILE] +Description: + Inline css into an html file. -#### options.removeStyleTags +Options: + -h, --help Show this message. + -v, --version Print version information. -Type: `Boolean` -Default: `true` - -Whether to remove the original `` tags after (possibly) inlining the css from them. - - -#### options.removeLinkTags - -Type: `Boolean` -Default: `true` - -Whether to remove the original `` tags after (possibly) inlining the css from them. - -#### options.url - -Type: `String` -Default: `filePath` - -How to resolve hrefs. **Required**. - -#### options.preserveMediaQueries - -Type: `Boolean` -Default: `false` - -Preserves all media queries (and contained styles) within `` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. - -#### options.applyWidthAttributes - -Type: `Boolean` -Default: `false` - -Whether to use any CSS pixel widths to create `width` attributes on elements. - -#### options.applyTableAttributes - -Type: `Boolean` -Default: `false` - -Whether to apply the `border`, `cellpadding` and `cellspacing` attributes to `table` elements. - -#### options.removeHtmlSelectors - -Type: `Boolean` -Default: `false` - -Whether to remove the `class` and `id` attributes from the markup. - -#### options.codeBlocks - -Type: `Object` -Default: `{ EJS: { start: '<%', end: '%>' }, HBS: { start: '{{', end: '}}' } }` - -An object where each value has a `start` and `end` to specify fenced code blocks that should be ignored during parsing and inlining. For example, Handlebars (hbs) templates are `HBS: {start: '{{', end: '}}'}`. `codeBlocks` can fix problems where otherwise inline-css might interpret code like `<=` as HTML, when it is meant to be template language code. Note that `codeBlocks` is a dictionary which can contain many different code blocks, so don't do `codeBlocks: {...}` do `codeBlocks.myBlock = {...}`. - -### Special markup - -#### data-embed - -When a data-embed attribute is present on a tag, inline-css will not inline the styles and will not remove the tags. - -This can be used to embed email client support hacks that rely on css selectors into your email templates. - -### cheerio options - -Options to passed to [cheerio](https://github.com/cheeriojs/cheerio). - -## Contributing +With no FILE, or when FILE is -, read standard input. +``` +## Documentation -See the [CONTRIBUTING Guidelines](https://github.com/jonkemp/inline-css/blob/master/CONTRIBUTING.md) +See https://github.com/jonkemp/inline-css/ ## License From 2e046249bf1c7e5eb5e0ac067f025f3f902afef9 Mon Sep 17 00:00:00 2001 From: Kent Oyer Date: Wed, 13 Jan 2021 12:08:15 -0600 Subject: [PATCH 4/4] Fix README --- README.md | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 172 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8532c3f..30b3e6f 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,83 @@ -# inline-css +# inline-css [![npm](http://img.shields.io/npm/v/inline-css.svg?style=flat)](https://badge.fury.io/js/inline-css) [![Build Status](https://travis-ci.org/jonkemp/inline-css.svg?branch=master)](https://travis-ci.org/jonkemp/inline-css) [![Coverage Status](https://coveralls.io/repos/jonkemp/inline-css/badge.svg?branch=master&service=github)](https://coveralls.io/github/jonkemp/inline-css?branch=master) -This is a fork of [jonkemp/inline-css](https://github.com/jonkemp/inline-css/) with the addition of a Command Line Interface (CLI) to make it easier to integrate this tool with other scripts and development tools. I needed this functionality to use this tool as a file watcher in PhpStorm. +[![NPM](https://nodei.co/npm/inline-css.png?downloads=true)](https://nodei.co/npm/inline-css/) -## Installation +> Inline your CSS properties into the `style` attribute in an html file. Useful for emails. +Inspired by the [juice](https://github.com/Automattic/juice) library. + +## Features +- Uses [cheerio](https://github.com/cheeriojs/cheerio) instead of jsdom +- Works on Windows +- Preserves Doctype +- Modular +- Gets your CSS automatically through style and link tags +- Functions return [A+ compliant](https://promisesaplus.com/) Promises + +## How It Works + +This module takes html and inlines the CSS properties into the style attribute. + +`/path/to/file.html`: +```html + + + + + + +

Test

+ + +``` + +`style.css` +```css +p { + text-decoration: underline; +} +``` + +Output: +```html + + + + +

Test

+ + ``` -npm install --save fkoyer/inline-css + +## What is this useful for ? + +- HTML emails. For a comprehensive list of supported selectors see +[here](http://www.campaignmonitor.com/css/) +- Embedding HTML in 3rd-party websites. +- Performance. Downloading external stylesheets delays the rendering of the page in the browser. Inlining CSS speeds up this process because the browser doesn't have to wait to download an external stylesheet to start rendering the page. + + +## Install + +Install with [npm](https://npmjs.org/package/inline-css) + +``` +npm install --save inline-css ``` ## Usage +```js +var inlineCss = require('inline-css'); +var html = "
"; + +inlineCss(html, options) + .then(function(html) { console.log(html); }); +``` + +## CLI ```console $ inline-css -h Usage: inline-css [FILE] @@ -24,9 +92,107 @@ Options: With no FILE, or when FILE is -, read standard input. ``` -## Documentation +## API + +### inlineCss(html, options) + + +#### options.extraCss + +Type: `String` +Default: `""` + +Extra css to apply to the file. + + +#### options.applyStyleTags + +Type: `Boolean` +Default: `true` + +Whether to inline styles in ``. + + +#### options.applyLinkTags + +Type: `Boolean` +Default: `true` + +Whether to resolve `` tags and inline the resulting styles. + + +#### options.removeStyleTags + +Type: `Boolean` +Default: `true` + +Whether to remove the original `` tags after (possibly) inlining the css from them. + + +#### options.removeLinkTags + +Type: `Boolean` +Default: `true` + +Whether to remove the original `` tags after (possibly) inlining the css from them. + +#### options.url + +Type: `String` +Default: `filePath` + +How to resolve hrefs. **Required**. + +#### options.preserveMediaQueries + +Type: `Boolean` +Default: `false` + +Preserves all media queries (and contained styles) within `` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. + +#### options.applyWidthAttributes + +Type: `Boolean` +Default: `false` + +Whether to use any CSS pixel widths to create `width` attributes on elements. + +#### options.applyTableAttributes + +Type: `Boolean` +Default: `false` + +Whether to apply the `border`, `cellpadding` and `cellspacing` attributes to `table` elements. + +#### options.removeHtmlSelectors + +Type: `Boolean` +Default: `false` + +Whether to remove the `class` and `id` attributes from the markup. + +#### options.codeBlocks + +Type: `Object` +Default: `{ EJS: { start: '<%', end: '%>' }, HBS: { start: '{{', end: '}}' } }` + +An object where each value has a `start` and `end` to specify fenced code blocks that should be ignored during parsing and inlining. For example, Handlebars (hbs) templates are `HBS: {start: '{{', end: '}}'}`. `codeBlocks` can fix problems where otherwise inline-css might interpret code like `<=` as HTML, when it is meant to be template language code. Note that `codeBlocks` is a dictionary which can contain many different code blocks, so don't do `codeBlocks: {...}` do `codeBlocks.myBlock = {...}`. + +### Special markup + +#### data-embed + +When a data-embed attribute is present on a tag, inline-css will not inline the styles and will not remove the tags. + +This can be used to embed email client support hacks that rely on css selectors into your email templates. + +### cheerio options + +Options to passed to [cheerio](https://github.com/cheeriojs/cheerio). + +## Contributing -See https://github.com/jonkemp/inline-css/ +See the [CONTRIBUTING Guidelines](https://github.com/jonkemp/inline-css/blob/master/CONTRIBUTING.md) ## License