2015-08-05 15:36:27 -07:00
# inline-html
2015-08-06 08:37:37 -07:00
Inline local assets referenced in an HTML document.
2015-08-05 15:36:27 -07:00
2015-08-17 09:17:50 -07:00
[](https://www.npmjs.com/package/inline-html)
[](https://travis-ci.org/panosoft/inline-html)
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
This library parses HTML, embeds the contents of local assets that are referenced within that HTML, and returns a new inlined HTML string.
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
The following HTML elements and CSS data types are inlined:
2015-08-05 15:36:27 -07:00
2015-12-01 09:20:24 -08:00
- Scripts - The source path is read and inlined.
2015-12-04 15:06:06 -08:00
- Linked CSS stylesheets - The stylesheet is read and inlined within a `<style>` element. Note that nested `@import` 's are also inlined.
- Linked LESS stylesheets - The LESS is compiled and the output is inlined within a `<style>` element. Note that `@import` 's are also inlined.
2015-08-05 15:36:27 -07:00
2015-12-04 15:06:06 -08:00
- Images - The source path is replaced with a datauri.
2015-08-05 15:36:27 -07:00
2015-08-17 09:17:50 -07:00
- 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.
2015-10-15 10:30:59 -07:00
Also, `inline-html` calls can be statically evaluated and included in Browserify bundles using the [`html-inlinify` ](https://github.com/panosoft/html-inlinify ) transform.
2015-10-13 13:59:25 -07:00
## Usage
2015-08-17 09:17:50 -07:00
2015-10-13 13:59:25 -07:00
Assuming ...
2015-08-17 09:17:50 -07:00
2015-12-01 09:20:24 -08:00
- `main.js`
```js
var a = 1;
```
2015-08-17 09:17:50 -07:00
- `main.less`
```css
2015-08-10 08:55:40 -07:00
div { background-image: url('path/to/file'); }
2015-08-17 09:17:50 -07:00
```
- `main.css`
```css
@font -face { src: url('path/to/file'); }
```
2015-10-13 13:59:25 -07:00
Then ...
2015-08-17 09:17:50 -07:00
```js
2015-10-13 13:59:25 -07:00
var co = require('co');
var inline = require('inline-html');
co(function * () {
var html = `
2015-12-01 09:20:24 -08:00
<script src="main.js"></script>
2015-12-04 15:06:06 -08:00
<link rel="stylesheet" href="main.css"/>
2015-10-13 13:59:25 -07:00
<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"/>
`;
html = yield inline.html(html);
console.log(html);
/**
2015-12-04 15:06:06 -08:00
<script> var a = 1; </script>
<style> @font -face { src: url('data:...'); } </style>
<style> div { background-image: url('data:...'); } </style>
2015-10-13 13:59:25 -07:00
<style> div { background-image: url('data:...'); } </style>
<div style="background-image: url('data:...');"></div>
<img src="data:..."/>
*/
2015-08-17 09:17:50 -07:00
});
2015-08-10 08:55:40 -07:00
```
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
## Installation
```sh
npm install inline-html
2015-08-10 08:55:40 -07:00
```
2015-08-05 15:36:27 -07:00
## API
2015-10-13 13:59:25 -07:00
- [`inline.html` ](#html )
- [`inline.file` ](#file )
- [`Results` ](#results )
2015-08-05 15:36:27 -07:00
2015-08-17 09:17:50 -07:00
---
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
<a name="html"/>
### inline.html ( html [, options] )
Parses an HTML string and embeds referenced local assets into the HTML.
Returns a `Promise` that is fulfilled with an `html` string or an instance of [`Results` ](#results ) depending on the value of `options.verbose` .
__Arguments__
- `html` - An HTML string to inline.
- `options`
- `filename` - The filename used to resolve relative paths. If this option is not provided, relative paths will be resolved relative to the process's current working directory.
- `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}` .
- `verbose` - A boolean that determines the promises fulfillment value. Supported values are:
- `true` : An instance of [`Results` ](#results ).
- `false` : An `html` string. (_Default_)
__Example__
```js
co(function * () {
var html = yield inline.html(`<img src="test.png">` );
console.log(html); // <img src="data:...">
});
```
---
<a name="file"/>
### inline.file ( filename [, options] )
2015-08-05 15:36:27 -07:00
2015-08-17 09:17:50 -07:00
Reads an HTML file and embeds referenced local assets into the HTML.
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
Returns a `Promise` that is fulfilled with an `html` string or an instance of [`Results` ](#results ) depending on the value of `options.verbose` .
2015-08-05 15:36:27 -07:00
2015-08-17 09:17:50 -07:00
__Arguments__
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
- `html` - A filename of an HTML file to inline. Relative file paths are resolved relative to the filename's directory.
2015-08-05 15:36:27 -07:00
- `options`
- `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}` .
2015-08-17 09:17:50 -07:00
- `verbose` - A boolean that determines the promises fulfillment value. Supported values are:
2015-10-13 13:59:25 -07:00
- `true` : An instance of [`Results` ](#results ).
- `false` : An `html` string. (_Default_)
__Example__
```js
co(function * () {
html = yield inline.file(`index.html` );
console.log(html); // <img src="data:...">
});
```
---
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
<a name="results"/>
### Results
2015-08-05 15:36:27 -07:00
2015-10-13 13:59:25 -07:00
The `Promise` returned by these functions is optionally fulfilled with a `results` object that has the following properties:
2015-08-05 15:46:46 -07:00
2015-08-05 15:36:27 -07:00
- `html` - The inlined html
2015-08-17 09:17:50 -07:00
- `files` - An array of filenames for the local assets that were inlined.