Skip to content

Commit 2ea51ca

Browse files
authored
Merge pull request #565 from Automattic/update-readme
chore: update readme.md
2 parents 9e165af + ba2c0c4 commit 2ea51ca

2 files changed

Lines changed: 135 additions & 77 deletions

File tree

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
33

4-
name: Node.js CI
4+
name: build
55

66
on:
77
push:

README.md

Lines changed: 134 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
[![Build Status](https://travis-ci.org/Automattic/juice.svg?branch=master)](https://travis-ci.org/Automattic/juice)
2-
[![Dependency Status](https://david-dm.org/Automattic/juice.svg)](https://david-dm.org/Automattic/juice)
3-
41
# Juice ![](https://i.imgur.com/jN8Ht.gif)
52

6-
Given HTML, juice will inline your CSS properties into the `style` attribute.
3+
[![CI][github-ci-shield]][github-ci]
4+
![Downloads](https://img.shields.io/npm/dw/juice)
5+
[![License][license-shield]][license]
6+
7+
Given HTML, Juice will inline your CSS properties into the `style` attribute.
78

8-
[Some projects using Juice](PROJECTS.md)
9+
[Some projects using Juice →](PROJECTS.md)
910

1011
## How to use
1112

12-
Juice has a number of functions based on whether you want to process a file, HTML string, or a cheerio document, and whether you want juice to automatically get remote stylesheets, scripts and image dataURIs to inline.
13+
Juice has a number of functions based on whether you want to process a file, HTML string, or a cheerio document, and whether you want Juice to automatically get remote stylesheets, scripts and image dataURIs to inline.
1314

1415
To inline HTML without getting remote resources, using default options:
1516

1617
```js
17-
var juice = require('juice');
18-
var result = juice("<style>div{color:red;}</style><div/>");
18+
const juice = require('juice');
19+
const result = juice("<style>div{color:red;}</style><div/>");
1920
```
2021

21-
result will be:
22+
`result` will be:
2223
```html
2324
<div style="color: red;"></div>
2425
```
2526

2627
[Try out the web client version](https://automattic.github.io/juice/)
2728

28-
## What is this useful for ?
29+
## What is this useful for?
2930

30-
- HTML emails. For a comprehensive list of supported selectors see [here](https://www.campaignmonitor.com/css/)
31+
- HTML emails. For CSS support in email clients see [Can I Email](https://www.caniemail.com/).
3132
- Embedding HTML in 3rd-party websites.
3233

3334
## Documentation
@@ -36,137 +37,191 @@ Juice is exposed as a standard module, and from CLI with a smaller set of option
3637

3738
### Options
3839

39-
All juice methods take an options object that can contain any of these properties, though not every method uses all of these:
40-
41-
* `applyAttributesTableElements` - whether to create attributes for styles in `juice.styleToAttribute` on elements set in `juice.tableElements`. Defaults to `true`.
42-
43-
* `applyHeightAttributes` - whether to use any CSS pixel heights to create `height` attributes on elements set in `juice.heightElements`. Defaults to `true`.
44-
45-
* `applyStyleTags` - whether to inline styles in `<style></style>` Defaults to `true`.
46-
47-
* `applyWidthAttributes` - whether to use any CSS pixel widths to create `width` attributes on elements set in `juice.widthElements`. Defaults to `true`.
48-
49-
* `decodeStyleAttributes` - whether to decode the value of `style=` attributes. Defaults to `false`.
50-
51-
* `extraCss` - extra css to apply to the file. Defaults to `""`.
52-
53-
* `insertPreservedExtraCss` - whether to insert into the document any preserved `@media` or `@font-face` content from `extraCss` when using `preserveMediaQueries`, `preserveFontFaces` or `preserveKeyFrames`. When `true` order of preference to append the `<style>` element is into `head`, then `body`, then at the end of the document. When a `string` the value is treated as a CSS/jQuery/cheerio selector, and when found, the `<style>` tag will be appended to the end of the first match. Defaults to `true`.
54-
55-
* `inlinePseudoElements` - Whether to insert pseudo elements (`::before` and `::after`) as `<span>` into the DOM. *Note*: Inserting pseudo elements will modify the DOM and may conflict with CSS selectors elsewhere on the page (e.g., `:last-child`).
56-
57-
* `preserveFontFaces` - preserves all `@font-face` within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
58-
59-
* `preserveImportant` - preserves `!important` in values. Defaults to `false`.
60-
61-
* `preserveMediaQueries` - preserves all media queries (and contained styles) within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
62-
63-
* `preserveKeyFrames` - preserves all key frames within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
64-
65-
* `preservePseudos` - preserves all rules containing pseudo selectors defined in `ignoredPseudos` within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
66-
67-
* `removeStyleTags` - whether to remove the original `<style></style>` tags after (possibly) inlining the css from them. Defaults to `true`.
68-
69-
* `resolveCSSVariables` - whether to resolve CSS variables. Defaults to `true`.
70-
71-
* `webResources` - An options object that will be passed to [web-resource-inliner](https://www.npmjs.com/package/web-resource-inliner) for juice functions that will get remote resources (`juiceResources` and `juiceFile`). Defaults to `{}`.
72-
73-
* `xmlMode` - whether to output XML/XHTML with all tags closed. Note that the input *must* also be valid XML/XHTML or you will get undesirable results. Defaults to `false`.
74-
40+
All Juice methods take an options object that can contain any of these properties, though not every method uses all of these:
41+
42+
| Option | Default&nbsp;value | Description |
43+
|--------|-------|------------|
44+
| `applyAttributesTableElements` | `true` | Create attributes for styles in `juice.styleToAttribute` on elements set in `juice.tableElements`. |
45+
| `applyHeightAttributes` | `true` | Use any CSS pixel heights to create `height` attributes on elements set in `juice.heightElements`. |
46+
| `applyStyleTags` | `true` | Inline styles in `<style>` tags. |
47+
| `applyWidthAttributes` | `true` | Use any CSS pixel widths to create `width` attributes on elements set in `juice.widthElements`. |
48+
| `decodeStyleAttributes` | `false` | Decode the value of `style` attributes. |
49+
| `extraCss` | `""` | Extra CSS to apply to the file. |
50+
| `insertPreservedExtraCss` | `true` | Whether to insert into the document any preserved `@media` or `@font-face` content from `extraCss` when using `preserveMediaQueries`, `preserveFontFaces` or `preserveKeyFrames`. <br><br> When set to `true`, order of preference to append the `<style>` element is into `head`, then `body`, then at the end of the document. <br><br> When a `string` the value is treated as a CSS/jQuery/cheerio selector, and when found, the `<style>` tag will be appended to the end of the first match. |
51+
| `inlinePseudoElements` | `false` | Insert pseudo elements (`::before` and `::after`) as `<span>` into the DOM. *Note*: Inserting pseudo elements will modify the DOM and may conflict with CSS selectors elsewhere on the page (e.g., `:last-child`). |
52+
| `preserveFontFaces` | `true` | Preserve all `@font-face` within `<style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. |
53+
| `preserveImportant` | `false` | Preserve `!important` in CSS values. |
54+
| `preserveMediaQueries` | `true` | Preserve all media queries (and contained styles) within `<style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. |
55+
| `preserveKeyFrames` | `true` | Preserve all key frames within `<style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. |
56+
| `preservePseudos` | `true` | Preserve all rules containing pseudo selectors defined in `ignoredPseudos` within `<style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. |
57+
| `removeStyleTags` | `true` | Remove the original `<style>` tags after (possibly) inlining their CSS content. |
58+
| `resolveCSSVariables` | `true` | Resolve CSS variables. |
59+
| `webResources` | `{}` | An options object that will be passed to [web-resource-inliner](https://www.npmjs.com/package/web-resource-inliner) for Juice functions that will get remote resources (`juiceResources` and `juiceFile`). |
60+
| `xmlMode` | `false` | Output XML/XHTML with all tags closed. Note that the input *must* also be valid XML/XHTML or you will get undesirable results. |
7561

7662
### Methods
7763

7864
#### juice(html [, options])
7965

80-
Returns string containing inlined HTML. Does not fetch remote resources.
66+
Returns a string containing the HTML with inlined CSS.
8167

82-
* `html` - html string, accepts complete documents as well as fragments
68+
Does not fetch remote resources.
69+
70+
* `html` - HTML string, accepts complete documents as well as fragments
8371
* `options` - optional, see Options above
8472

8573
#### juice.juiceResources(html, options, callback)
8674

87-
Callback returns string containing inlined HTML. Fetches remote resources.
75+
Callback returns a string containing the HTML with inlined CSS.
76+
77+
Fetches remote resources.
8878

89-
* `html` - html string
79+
* `html` - (string) HTML
9080
* `options` - see Options above
9181
* `callback(err, html)`
9282
- `err` - `Error` object or `null`
93-
- `html` - inlined HTML
83+
- `html` - (string) HTML with inlined CSS
9484

9585
#### juice.juiceFile(filePath, options, callback)
9686

97-
Callback returns string containing inlined HTML. Fetches remote resources.
87+
Callback returns a string containing the HTML with inlined CSS.
88+
89+
Fetches remote resources.
9890

99-
* `filePath` - path to the html file to be juiced
91+
* `filePath` - path to the HTML file to be Juiced
10092
* `options` - see Options above
10193
* `callback(err, html)`
10294
- `err` - `Error` object or `null`
103-
- `html` - inlined HTML
95+
- `html` - (string) HTML with inlined CSS
10496

10597
#### juice.juiceDocument($ [, options])
10698

107-
This takes a cheerio instance and performs inlining in-place. Returns the
108-
same cheerio instance. Does not fetch remote resources.
99+
This takes a `cheerio` instance and performs inlining in-place. Returns the same `cheerio` instance.
100+
Does not fetch remote resources.
109101

110-
* `$` - a cheerio instance, be sure to use the same cheerio version that juice uses
111-
* `options` - optional, see Options above`
102+
* `$` - a `cheerio` instance, be sure to use the same `cheerio` version that Juice uses
103+
* `options` - (object) optional, see Options above
112104

113105
#### juice.inlineContent(html, css [, options])
114106

115-
This takes html and css and returns new html with the provided css inlined.
107+
This takes HTML and CSS and returns new HTML with the provided CSS inlined.
108+
116109
It does not look at `<style>` or `<link rel="stylesheet">` elements at all.
117110

118-
* `html` - html string
119-
* `css` - css string
120-
* `options` - optional, see Options above
111+
* `html` - HTML string
112+
* `css` - CSS string
113+
* `options` - (object) optional, see Options above
121114

122115
#### juice.inlineDocument($, css [, options])
123116

124-
Given a cheerio instance and css, this modifies the cheerio instance so that the provided css is inlined. It does not look at `<style>` or `<link rel="stylesheet">` elements at all.
117+
Given a `cheerio` instance and CSS, this modifies the `cheerio` instance so that the provided CSS is inlined. It does not look at `<style>` or `<link rel="stylesheet">` elements at all.
125118

126-
* `$` - a cheerio instance, be sure to use the same cheerio version that juice uses
127-
* `css` - css string
128-
* `options` - optional, see Options above
119+
* `$` - a `cheerio` instance, be sure to use the same `cheerio` version that juice uses
120+
* `css` - CSS string
121+
* `options` - (object) optional, see Options above
129122

130123
### Global settings
131124

132125
#### juice.codeBlocks
133126

134-
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 `juice.codeBlocks.HBS = {start: '{{', end: '}}'}`. `codeBlocks` can fix problems where otherwise juice 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 `juice.codeBlocks = {...}` do `juice.codeBlocks.myBlock = {...}`
127+
Type: Object\
128+
Default:
129+
130+
```js
131+
{
132+
EJS: { start: '<%', end: '%>' },
133+
HBS: { start: '{{', end: '}}' }
134+
}
135+
```
136+
137+
An object where each value has a `start` and `end` to specify fenced code blocks that should be ignored during parsing and inlining.
138+
139+
For example, Handlebars (hbs) templates are `juice.codeBlocks.HBS = {start: '{{', end: '}}'}`. `codeBlocks` can fix problems where otherwise juice might interpret code like `<=` as HTML, when it is meant to be template language code.
140+
141+
Note that `codeBlocks` is a dictionary which can contain many different code blocks, so don't do `juice.codeBlocks = {...}` do `juice.codeBlocks.myBlock = {...}`
135142

136143
#### juice.ignoredPseudos
137144

145+
Type: Array\
146+
Default: `['hover', 'active', 'focus', 'visited', 'link']`
147+
138148
Array of ignored pseudo-selectors such as 'hover' and 'active'.
139149

140150
#### juice.widthElements
141151

152+
Type: Array\
153+
Default: `['TABLE', 'TD', 'TH', 'IMG']`
154+
142155
Array of HTML elements that can receive `width` attributes.
143156

144157
#### juice.heightElements
145158

159+
Type: Array\
160+
Default: `['TABLE', 'TD', 'TH', 'IMG']`
161+
146162
Array of HTML elements that can receive `height` attributes.
147163

148164
#### juice.styleToAttribute
149165

166+
Type: Object\
167+
Default:
168+
169+
```js
170+
{
171+
'background-color': 'bgcolor',
172+
'background-image': 'background',
173+
'text-align': 'align',
174+
'vertical-align': 'valign'
175+
}
176+
```
177+
150178
Object of style property names (key) to their respective attribute names (value).
151179

152180
#### juice.tableElements
153181

182+
Type: Array\
183+
Default: `['TABLE', 'TH', 'TR', 'TD', 'CAPTION', 'COLGROUP', 'COL', 'THEAD', 'TBODY', 'TFOOT']`
184+
154185
Array of table HTML elements that can receive attributes defined in `juice.styleToAttribute`.
155186

156187
#### juice.nonVisualElements
157188

189+
Type: Array\
190+
Default: `[ 'HEAD', 'TITLE', 'BASE', 'LINK', 'STYLE', 'META', 'SCRIPT', 'NOSCRIPT' ]`
191+
158192
Array of elements that will not have styles inlined because they are not intended to render.
159193

160194
#### juiceClient.excludedProperties
161195

162-
Array of css properties that won't be inlined.
196+
Type: Array\
197+
Default: `[]`
198+
199+
Array of CSS properties that won't be inlined.
163200

164201
### Special markup
165202

166203
#### data-embed
167-
When a `data-embed` attribute is present on a stylesheet `<link>` that has been inlined into the document as a `<style></style>` tag by the web-resource-inliner juice will not inline the styles and will not remove the `<style></style>` tags.
168204

169-
This can be used to embed email client support hacks that rely on css selectors into your email templates.
205+
Add `data-embed` to any `<style>` tag to prevent Juice from inlining its CSS and removing it.
206+
Can be used to embed email client support hacks that rely on CSS selectors into your email templates:
207+
208+
```html
209+
<style data-embed>
210+
u + .body .your-class-name {
211+
/* CSS here targets Gmail */
212+
}
213+
</style>
214+
```
215+
216+
The `data-embed` attribute will be removed from the output HTML, but no inlining or style tag removal will take place:
217+
218+
```html
219+
<style>
220+
u + .body .your-class-name {
221+
/* CSS here targets Gmail */
222+
}
223+
</style>
224+
```
170225

171226
### CLI Options
172227

@@ -176,8 +231,6 @@ For a listing of all available options, just type `juice -h`.
176231

177232
> Note that if you want to just type `juice` from the command line, you should `npm install juice -g` so it is globally available.
178233
179-
CLI Options:
180-
181234
The CLI should have all the above [options](#options) with the names changed from camel case to hyphen-delimited, so for example `extraCss` becomes `extra-css` and `webResources.scripts` becomes `web-resources-scripts`.
182235

183236
These are additional options not included in the standard `juice` options listed above:
@@ -192,12 +245,17 @@ Attempting to Browserify `require('juice')` fails because portions of Juice and
192245

193246
## License
194247

195-
MIT Licensed, see License.md
248+
MIT Licensed, see [LICENSE.md](LICENSE.md).
196249

197250
### 3rd-party
198251

199-
- Uses [cheerio](https://github.com/cheeriojs/cheerio) for the underlying DOM
200-
representation.
252+
- Uses [cheerio](https://github.com/cheeriojs/cheerio) for the underlying DOM representation.
201253
- Uses [mensch](https://github.com/brettstimmerman/mensch) to parse out CSS and
202254
[Slick](https://github.com/subtleGradient/slick) to tokenize them.
203255
- Icon by [UnheardSounds](http://unheardsounds.deviantart.com/gallery/26536908#/d2ngozi)
256+
257+
258+
[github-ci]: https://github.com/Automattic/juice/actions
259+
[github-ci-shield]: https://github.com/Automattic/juice/actions/workflows/nodejs.yml/badge.svg
260+
[license]: ./LICENSE
261+
[license-shield]: https://img.shields.io/npm/l/juice.svg

0 commit comments

Comments
 (0)