Files
inline-html/README.md

101 lines
2.6 KiB
Markdown
Raw Normal View History

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
[![npm](https://img.shields.io/npm/v/inline-html.svg)]()
2015-08-05 16:16:35 -07:00
[![npm](https://img.shields.io/npm/l/inline-html.svg)]()
2015-08-05 15:36:27 -07:00
[![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:
2015-08-10 08:55:40 -07:00
```js
var inlineHtml = require('inline-html');
inlineHtml('path/to/file.html').then(function (html) {
...
});
```
2015-08-05 15:36:27 -07:00
Turns this:
2015-08-10 08:55:40 -07:00
```html
<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"/>
```
2015-08-05 15:36:27 -07:00
Into this:
2015-08-10 08:55:40 -07:00
```html
<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:..."/>
```
2015-08-05 15:36:27 -07:00
Where:
2015-08-05 15:46:46 -07:00
- `main.less`
2015-08-05 15:36:27 -07:00
2015-08-10 08:55:40 -07:00
```css
@import (inline) 'main.css';
div { background-image: url('path/to/file'); }
```
2015-08-05 15:36:27 -07:00
2015-08-05 15:46:46 -07:00
- `main.css`
2015-08-05 15:36:27 -07:00
2015-08-10 08:55:40 -07:00
```css
@font-face { src: url('path/to/file'); }
```
2015-08-05 15:36:27 -07:00
## API
### inlineHtml( filename [, options] )
2015-08-05 15:52:08 -07:00
Reads an HTML file and embeds the contents of local assets referenced by the following elements and data types:
2015-08-05 15:36:27 -07:00
- 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
2015-08-05 15:46:46 -07:00
The `Promise` returned by this function is optionally fulfilled with a `results` object that has the following properties:
2015-08-05 15:36:27 -07:00
- `html` - The inlined html
- `files` - An array of filenames of the inlined local assets.