mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-12-08 20:29:06 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a3b6f9d50 | ||
|
|
5b88484888 | ||
|
|
dff5f1160b | ||
|
|
abb3dd95a7 | ||
|
|
8f27f9d04f | ||
|
|
76ff6ca703 | ||
|
|
8e3a9b0390 | ||
|
|
56a6f43657 | ||
|
|
d5c6a6b540 | ||
|
|
a126b341cc | ||
|
|
734cd940a8 | ||
|
|
8854629943 | ||
|
|
070fc331e7 | ||
|
|
4d9eeaa926 | ||
|
|
840dba9901 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
node_modules
|
||||
test/test.html
|
||||
.idea
|
||||
*.log
|
||||
test/test.html
|
||||
|
||||
3
.travis.yml
Normal file
3
.travis.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "node"
|
||||
105
README.md
Normal file
105
README.md
Normal file
@@ -0,0 +1,105 @@
|
||||
# inline-html
|
||||
|
||||
Inline local assets referenced in an HTML document.
|
||||
|
||||
[](https://www.npmjs.com/package/inline-html)
|
||||
[](https://www.npmjs.com/package/inline-html)
|
||||
[](https://travis-ci.org/panosoft/inline-html)
|
||||
[](https://david-dm.org/panosoft/inline-html)
|
||||
[](https://www.npmjs.com/package/inline-html)
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install inline-html
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Reads an HTML file, embeds the contents of local assets that are referenced within that file, and returns the inlined `html` string.
|
||||
|
||||
The following elements and data types are inlined:
|
||||
|
||||
- LESS stylesheets - The LESS is compiled and the result is inlined within a `<style>` element.
|
||||
- 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.
|
||||
- Images - The source path is replaced with a datauri.
|
||||
|
||||
Assuming we have the following files:
|
||||
|
||||
- `index.html`
|
||||
|
||||
```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"/>
|
||||
```
|
||||
|
||||
- `main.less`
|
||||
|
||||
```css
|
||||
@import (inline) 'main.css';
|
||||
div { background-image: url('path/to/file'); }
|
||||
```
|
||||
|
||||
- `main.css`
|
||||
|
||||
```css
|
||||
@font-face { src: url('path/to/file'); }
|
||||
```
|
||||
|
||||
We can use `inline-html`:
|
||||
|
||||
```js
|
||||
var inlineHtml = require('inline-html');
|
||||
|
||||
inlineHtml('index.html').then(function (html) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
To create the following `html` string:
|
||||
|
||||
```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:..."/>
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
- [`inlineHtml`](#inlineHtml)
|
||||
|
||||
---
|
||||
|
||||
<a name="inlineHtml"/>
|
||||
### inlineHtml ( filename [, options] )
|
||||
|
||||
Reads an HTML file 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__
|
||||
|
||||
- `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. Supported values are:
|
||||
- `true`: An instance of [`Results`](#Results).
|
||||
- `false`: An `html` string. (_default_)
|
||||
|
||||
<a name="Results"/>
|
||||
__Results__
|
||||
|
||||
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 for the local assets that were inlined.
|
||||
@@ -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);
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "inline-html",
|
||||
"version": "0.1.0",
|
||||
"description": "Inline an html file.",
|
||||
"version": "0.1.6",
|
||||
"description": "Inline local assets referenced in an HTML document.",
|
||||
"repository": "panosoft/inline-html",
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cheerio": "^0.19.0",
|
||||
"co": "^4.6.0",
|
||||
|
||||
Reference in New Issue
Block a user