mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-12-08 20:29:06 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f28f0e718a | ||
|
|
ff5e4401f7 | ||
|
|
99eed58f96 | ||
|
|
dad1267607 | ||
|
|
f0b9b3cc1e |
@@ -1,17 +1,18 @@
|
||||
var _ = require('lodash');
|
||||
var co = require('co');
|
||||
var cheerio = require('cheerio');
|
||||
var fs = require('mz/fs');
|
||||
var inlineStyle = require('./inline-style');
|
||||
var inlineImg = require('./inline-img');
|
||||
var inlineLinkLess = require('./inline-link-less');
|
||||
var R = require('ramda');
|
||||
var Ru = require('@panosoft/ramda-utils');
|
||||
|
||||
var inline = co.wrap(function * (html, options) {
|
||||
options = _.defaults(options || {}, {
|
||||
options = Ru.defaults({
|
||||
filename: null,
|
||||
less: {},
|
||||
verbose: false
|
||||
});
|
||||
}, options || {});
|
||||
var filename;
|
||||
try {
|
||||
filename = html;
|
||||
@@ -48,7 +49,7 @@ var inline = co.wrap(function * (html, options) {
|
||||
|
||||
var result = {
|
||||
html: html,
|
||||
files: _.unique(_.flatten(files, true))
|
||||
files: R.uniq(R.flatten(files, true))
|
||||
};
|
||||
return (options.verbose ? result : result.html);
|
||||
});
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
var _ = require('lodash');
|
||||
var datauri = require('datauri');
|
||||
var isLocalPath = require('is-local-path');
|
||||
var isTemplateExpression = require('./is-template-expression');
|
||||
var path = require('path');
|
||||
var postcss = require('postcss');
|
||||
var postcssUrl = require('postcss-url');
|
||||
var R = require('ramda');
|
||||
var url = require('url');
|
||||
|
||||
/**
|
||||
@@ -15,8 +16,10 @@ var url = require('url');
|
||||
*/
|
||||
var clean = function (path) {
|
||||
path = url.parse(path);
|
||||
path = _.pick(path, ['protocol', 'host', 'pathname']);
|
||||
return url.format(path);
|
||||
path = R.pick(['protocol', 'host', 'pathname'], path);
|
||||
path = url.format(path);
|
||||
path = decodeURI(path);
|
||||
return path;
|
||||
};
|
||||
/**
|
||||
* Convert local url data type paths to datauris.
|
||||
@@ -31,7 +34,7 @@ var inline = function (css, filename) {
|
||||
var result = postcss()
|
||||
.use(postcssUrl({
|
||||
url: function (urlPath) {
|
||||
if (isLocalPath(urlPath)) {
|
||||
if (isLocalPath(urlPath) && !isTemplateExpression(urlPath)) {
|
||||
urlPath = clean(urlPath);
|
||||
urlPath = path.resolve(basePath, urlPath);
|
||||
files.push(urlPath);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
var cheerio = require('cheerio');
|
||||
var datauri = require('datauri');
|
||||
var isLocalPath = require('is-local-path');
|
||||
var isTemplateExpression = require('./is-template-expression');
|
||||
var path = require('path');
|
||||
|
||||
var inline = function (html, filename) {
|
||||
@@ -8,7 +9,8 @@ var inline = function (html, filename) {
|
||||
var basedir = path.dirname(filename);
|
||||
var $ = cheerio.load(html, {decodeEntities: false});
|
||||
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) {
|
||||
var src = $(element).attr('src');
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
var _ = require('lodash');
|
||||
var co = require('co');
|
||||
var cheerio = require('cheerio');
|
||||
var fs = require('mz/fs');
|
||||
var isLocalPath = require('is-local-path');
|
||||
var less = require('less');
|
||||
var path = require('path');
|
||||
var R = require('ramda');
|
||||
var Ru = require('@panosoft/ramda-utils');
|
||||
var url = require('url');
|
||||
|
||||
var render = co.wrap(function * (filename, options) {
|
||||
options = _.assign(options || {}, {
|
||||
options = R.merge(options || {}, {
|
||||
filename: filename
|
||||
});
|
||||
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 files = [];
|
||||
var basedir = path.dirname(filename);
|
||||
options = _.defaults(options || {}, {
|
||||
options = Ru.defaults({
|
||||
relativeUrls: true
|
||||
});
|
||||
}, options || {});
|
||||
|
||||
// TODO Import less 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
|
||||
files.push(_.map(outputs, function (output) {
|
||||
files.push(R.map(function (output) {
|
||||
return output.imports;
|
||||
}));
|
||||
}, outputs));
|
||||
|
||||
return {
|
||||
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",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"description": "Inline local assets referenced in an HTML document.",
|
||||
"repository": "panosoft/inline-html",
|
||||
"main": "lib/index.js",
|
||||
@@ -10,15 +10,16 @@
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@panosoft/ramda-utils": "^0.1.12",
|
||||
"cheerio": "^0.19.0",
|
||||
"co": "^4.6.0",
|
||||
"datauri": "^0.7.1",
|
||||
"is-local-path": "^0.1.0",
|
||||
"less": "^2.5.1",
|
||||
"lodash": "^3.10.0",
|
||||
"mz": "^2.0.0",
|
||||
"postcss": "^5.0.0",
|
||||
"postcss-url": "^4.0.0",
|
||||
"postcss-url": "^5.0.0",
|
||||
"ramda": "^0.17.1",
|
||||
"string": "^3.3.0"
|
||||
},
|
||||
"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"/>`;
|
||||
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 () {
|
||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||
var url = `${filename}?query=string#hash`;
|
||||
@@ -159,5 +167,10 @@ describe('inlineHtml', function () {
|
||||
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
||||
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