mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-12-08 20:29:06 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f28f0e718a | ||
|
|
ff5e4401f7 | ||
|
|
99eed58f96 | ||
|
|
dad1267607 | ||
|
|
f0b9b3cc1e | ||
|
|
f143065dbf | ||
|
|
4501132051 | ||
|
|
006d059e1b | ||
|
|
0a743f6b2e |
@@ -1,3 +1,7 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "node"
|
- "iojs"
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
on_success: change
|
||||||
|
on_failure: always
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
var _ = require('lodash');
|
|
||||||
var co = require('co');
|
var co = require('co');
|
||||||
var cheerio = require('cheerio');
|
var cheerio = require('cheerio');
|
||||||
var fs = require('mz/fs');
|
var fs = require('mz/fs');
|
||||||
var inlineStyle = require('./inline-style');
|
var inlineStyle = require('./inline-style');
|
||||||
var inlineImg = require('./inline-img');
|
var inlineImg = require('./inline-img');
|
||||||
var inlineLinkLess = require('./inline-link-less');
|
var inlineLinkLess = require('./inline-link-less');
|
||||||
|
var R = require('ramda');
|
||||||
|
var Ru = require('@panosoft/ramda-utils');
|
||||||
|
|
||||||
var inline = co.wrap(function * (html, options) {
|
var inline = co.wrap(function * (html, options) {
|
||||||
options = _.defaults(options || {}, {
|
options = Ru.defaults({
|
||||||
filename: null,
|
filename: null,
|
||||||
less: {},
|
less: {},
|
||||||
verbose: false
|
verbose: false
|
||||||
});
|
}, options || {});
|
||||||
var filename;
|
var filename;
|
||||||
try {
|
try {
|
||||||
filename = html;
|
filename = html;
|
||||||
@@ -48,7 +49,7 @@ var inline = co.wrap(function * (html, options) {
|
|||||||
|
|
||||||
var result = {
|
var result = {
|
||||||
html: html,
|
html: html,
|
||||||
files: _.unique(_.flatten(files, true))
|
files: R.uniq(R.flatten(files, true))
|
||||||
};
|
};
|
||||||
return (options.verbose ? result : result.html);
|
return (options.verbose ? result : result.html);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
var _ = require('lodash');
|
|
||||||
var datauri = require('datauri');
|
var datauri = require('datauri');
|
||||||
var isLocalPath = require('is-local-path');
|
var isLocalPath = require('is-local-path');
|
||||||
|
var isTemplateExpression = require('./is-template-expression');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var postcss = require('postcss');
|
var postcss = require('postcss');
|
||||||
var postcssUrl = require('postcss-url');
|
var postcssUrl = require('postcss-url');
|
||||||
|
var R = require('ramda');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,8 +16,10 @@ var url = require('url');
|
|||||||
*/
|
*/
|
||||||
var clean = function (path) {
|
var clean = function (path) {
|
||||||
path = url.parse(path);
|
path = url.parse(path);
|
||||||
path = _.pick(path, ['protocol', 'host', 'pathname']);
|
path = R.pick(['protocol', 'host', 'pathname'], path);
|
||||||
return url.format(path);
|
path = url.format(path);
|
||||||
|
path = decodeURI(path);
|
||||||
|
return path;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* Convert local url data type paths to datauris.
|
* Convert local url data type paths to datauris.
|
||||||
@@ -31,7 +34,7 @@ var inline = function (css, filename) {
|
|||||||
var result = postcss()
|
var result = postcss()
|
||||||
.use(postcssUrl({
|
.use(postcssUrl({
|
||||||
url: function (urlPath) {
|
url: function (urlPath) {
|
||||||
if (isLocalPath(urlPath)) {
|
if (isLocalPath(urlPath) && !isTemplateExpression(urlPath)) {
|
||||||
urlPath = clean(urlPath);
|
urlPath = clean(urlPath);
|
||||||
urlPath = path.resolve(basePath, urlPath);
|
urlPath = path.resolve(basePath, urlPath);
|
||||||
files.push(urlPath);
|
files.push(urlPath);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
var cheerio = require('cheerio');
|
var cheerio = require('cheerio');
|
||||||
var datauri = require('datauri');
|
var datauri = require('datauri');
|
||||||
var isLocalPath = require('is-local-path');
|
var isLocalPath = require('is-local-path');
|
||||||
|
var isTemplateExpression = require('./is-template-expression');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var inline = function (html, filename) {
|
var inline = function (html, filename) {
|
||||||
@@ -8,7 +9,8 @@ var inline = function (html, filename) {
|
|||||||
var basedir = path.dirname(filename);
|
var basedir = path.dirname(filename);
|
||||||
var $ = cheerio.load(html, {decodeEntities: false});
|
var $ = cheerio.load(html, {decodeEntities: false});
|
||||||
var images = $('img').filter(function (index, element) {
|
var images = $('img').filter(function (index, element) {
|
||||||
return isLocalPath($(element).attr('src'));
|
var path = $(element).attr('src');
|
||||||
|
return isLocalPath(path) && !isTemplateExpression(path);
|
||||||
});
|
});
|
||||||
images.each(function (index, element) {
|
images.each(function (index, element) {
|
||||||
var src = $(element).attr('src');
|
var src = $(element).attr('src');
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
var _ = require('lodash');
|
|
||||||
var co = require('co');
|
var co = require('co');
|
||||||
var cheerio = require('cheerio');
|
var cheerio = require('cheerio');
|
||||||
var fs = require('mz/fs');
|
var fs = require('mz/fs');
|
||||||
var isLocalPath = require('is-local-path');
|
var isLocalPath = require('is-local-path');
|
||||||
var less = require('less');
|
var less = require('less');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
var R = require('ramda');
|
||||||
|
var Ru = require('@panosoft/ramda-utils');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
|
||||||
var render = co.wrap(function * (filename, options) {
|
var render = co.wrap(function * (filename, options) {
|
||||||
options = _.assign(options || {}, {
|
options = R.merge(options || {}, {
|
||||||
filename: filename
|
filename: filename
|
||||||
});
|
});
|
||||||
var contents = yield fs.readFile(filename, 'utf8');
|
var contents = yield fs.readFile(filename, 'utf8');
|
||||||
@@ -23,9 +24,9 @@ var render = co.wrap(function * (filename, options) {
|
|||||||
var inline = co.wrap(function * (html, filename, options) {
|
var inline = co.wrap(function * (html, filename, options) {
|
||||||
var files = [];
|
var files = [];
|
||||||
var basedir = path.dirname(filename);
|
var basedir = path.dirname(filename);
|
||||||
options = _.defaults(options || {}, {
|
options = Ru.defaults({
|
||||||
relativeUrls: true
|
relativeUrls: true
|
||||||
});
|
}, options || {});
|
||||||
|
|
||||||
// TODO Import less links
|
// TODO Import less links
|
||||||
// get links
|
// get links
|
||||||
@@ -52,9 +53,9 @@ var inline = co.wrap(function * (html, filename, options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// create list of imported files from all outputs, unique listing
|
// create list of imported files from all outputs, unique listing
|
||||||
files.push(_.map(outputs, function (output) {
|
files.push(R.map(function (output) {
|
||||||
return output.imports;
|
return output.imports;
|
||||||
}));
|
}, outputs));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
html: $.xml(),
|
html: $.xml(),
|
||||||
|
|||||||
14
lib/is-template-expression.js
Normal file
14
lib/is-template-expression.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/**
|
||||||
|
* Tests whether a path is a mustache template expression.
|
||||||
|
*
|
||||||
|
* Note: would be best if this was file extension specific
|
||||||
|
* (i.e. *.hbs => test for `{{ }}` )
|
||||||
|
*
|
||||||
|
* @param {String} path
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isTemplateExpression = function (path) {
|
||||||
|
return /^{{.*}}$/.test(path);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = isTemplateExpression;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "inline-html",
|
"name": "inline-html",
|
||||||
"version": "0.1.7",
|
"version": "0.1.10",
|
||||||
"description": "Inline local assets referenced in an HTML document.",
|
"description": "Inline local assets referenced in an HTML document.",
|
||||||
"repository": "panosoft/inline-html",
|
"repository": "panosoft/inline-html",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
@@ -10,15 +10,16 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@panosoft/ramda-utils": "^0.1.12",
|
||||||
"cheerio": "^0.19.0",
|
"cheerio": "^0.19.0",
|
||||||
"co": "^4.6.0",
|
"co": "^4.6.0",
|
||||||
"datauri": "^0.7.1",
|
"datauri": "^0.7.1",
|
||||||
"is-local-path": "^0.1.0",
|
"is-local-path": "^0.1.0",
|
||||||
"less": "^2.5.1",
|
"less": "^2.5.1",
|
||||||
"lodash": "^3.10.0",
|
|
||||||
"mz": "^2.0.0",
|
"mz": "^2.0.0",
|
||||||
"postcss": "^5.0.0",
|
"postcss": "^5.0.0",
|
||||||
"postcss-url": "^4.0.0",
|
"postcss-url": "^5.0.0",
|
||||||
|
"ramda": "^0.17.1",
|
||||||
"string": "^3.3.0"
|
"string": "^3.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
1
test/fixtures/file space.txt
vendored
Normal file
1
test/fixtures/file space.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Test.
|
||||||
@@ -152,6 +152,14 @@ describe('inlineHtml', function () {
|
|||||||
var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
|
var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
|
||||||
return expect(inline(html)).to.eventually.equal(html);
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
|
it('ignore css url template expression paths', function () {
|
||||||
|
var html = `<style> div { background-image: url({{path}}); }</style>`;
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
it('ignore img src template expression paths', function () {
|
||||||
|
var html = `<img src="{{path}}"/>`;
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
it('ignore query strings and hashes on local paths', function () {
|
it('ignore query strings and hashes on local paths', function () {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var url = `${filename}?query=string#hash`;
|
var url = `${filename}?query=string#hash`;
|
||||||
@@ -159,5 +167,10 @@ describe('inlineHtml', function () {
|
|||||||
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
||||||
return expect(inline(html(url))).to.eventually.equal(html(uri));
|
return expect(inline(html(url))).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
|
it('handle assets with a space in their filename', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file space.txt');
|
||||||
|
var uri = datauri(filename);
|
||||||
|
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
||||||
|
return expect(inline(html(filename))).to.eventually.equal(html(uri));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user