6 Commits

Author SHA1 Message Date
Alexandre Gigliotti
a126b341cc 0.1.2 2015-08-05 15:52:37 -07:00
Alexandre Gigliotti
734cd940a8 Updated README. 2015-08-05 15:52:08 -07:00
Alexandre Gigliotti
8854629943 Updated README. 2015-08-05 15:46:46 -07:00
Alexandre Gigliotti
070fc331e7 Added README and .travis files. 2015-08-05 15:36:27 -07:00
Alexandre Gigliotti
4d9eeaa926 0.1.1 2015-07-30 09:13:44 -07:00
Alexandre Gigliotti
840dba9901 Fixed bug where style attributes of elements outside a body element would not be inlined. 2015-07-30 09:13:39 -07:00
4 changed files with 97 additions and 6 deletions

3
.travis.yml Normal file
View File

@@ -0,0 +1,3 @@
language: node_js
node_js:
- "node"

89
README.md Normal file
View File

@@ -0,0 +1,89 @@
# inline-html
Embed local assets in an HTML document.
[![npm](https://img.shields.io/npm/v/inline-html.svg)]()
[![Travis](https://img.shields.io/travis/panosoft/inline-html.svg)]()
[![David](https://img.shields.io/david/panosoft/inline-html.svg)]()
[![npm](https://img.shields.io/npm/dm/inline-html.svg)]()
## Installation
npm install inline-html
## Usage
This:
var inlineHtml = require('inline-html');
inlineHtml('path/to/file.html').then(function (html) {
...
});
Turns this:
<link rel="stylesheet/less" href="main.less" />
<style>
div { background-image: url('path/to/file'); }
</style>
<div style="background-image: url('path/to/file');"></div>
<img src="path/to/file" />
Into this:
<style>
@font-face { src: url('data:...'); }
div { background-image: url('data:...'); }
</style>
<style>
div { background-image: url('data:...'); }
</style>
<div style="background-image: url('data:...');"></div>
<img src="data:..." />
Where:
- `main.less`
@import (inline) 'main.css';
div { background-image: url('path/to/file'); }
- `main.css`
@font-face { src: url('path/to/file'); }
## API
### inlineHtml( filename [, options] )
Reads an HTML file and embeds the contents of local assets referenced by the following elements and data types:
- LESS stylesheets - The LESS is compiled and the result is inlined within a `<style>` element.
<link rel="stylesheet/less" href="main.less"/> -> <style>...</style>
- CSS url data types - The reference path is replaced with a datauri. These can be used in linked stylesheets, style elements, and element style attributes.
url('file.ext') -> url('data:...')
- Images - The source path is replaced with a datauri.
<img src="file.ext"/> -> <img src="data:..."/>
Returns a `Promise` that is fulfilled with an `html` string or a `results` object depending on the value of `options.verbose`.
#### Arguments
- `filename` - The filename of the HTML file to be inlined. Relative file paths are resolved relative to the filename directory.
- `options`
- `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}`.
- `verbose` - A boolean that determines the promises fulfillment value. Defaults to `false`.
- `true`: promise is resolved with an instance of `Results`
- `false`: promise is resolved with `html`
#### Results object
The `Promise` returned by this function is optionally fulfilled with a `results` object that has the following properties:
- `html` - The inlined html
- `files` - An array of filenames of the inlined local assets.

View File

@@ -14,22 +14,21 @@ var unwrap = function (value) {
var inline = function (html, filename) {
var files = [];
var $ = cheerio.load(html, {decodeEntities: false});
// style elements
var styles = $('style');
styles.each(function (index, element) {
var css = $(element).html();
result = inlineUrl(css, filename);
var result = inlineUrl(css, filename);
files.push(result.files);
$(element).html(result.css);
});
// style attributes
var attributes = $('body *').filter('[style]');
var attributes = $('*').filter('[style]');
attributes.each(function (index, element) {
var css = $(element).attr('style');
css = wrap(css);
result = inlineUrl(css, filename);
var result = inlineUrl(css, filename);
files.push(result.files);
css = string(result.css).collapseWhitespace().toString();
css = unwrap(css);

View File

@@ -1,7 +1,7 @@
{
"name": "inline-html",
"version": "0.1.0",
"description": "Inline an html file.",
"version": "0.1.2",
"description": "Embed local assets in an HTML document.",
"repository": "panosoft/inline-html",
"main": "lib/index.js",
"scripts": {