mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-12-08 20:29:06 +00:00
Added support for verbose option that returns filenames of each inlined file alongside inlined html.
This commit is contained in:
26
lib/index.js
26
lib/index.js
@@ -2,28 +2,42 @@ 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 inlineCss = require('./inline-css');
|
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 inline = co.wrap(function * (filename, options) {
|
var inline = co.wrap(function * (filename, options) {
|
||||||
options = _.defaults(options || {}, {
|
options = _.defaults(options || {}, {
|
||||||
less: {}
|
less: {},
|
||||||
|
verbose: false
|
||||||
});
|
});
|
||||||
var html = yield fs.readFile(filename, 'utf8');
|
var html = yield fs.readFile(filename, 'utf8');
|
||||||
|
var files = [filename];
|
||||||
|
|
||||||
// Inline links
|
// Inline links
|
||||||
html = yield inlineLinkLess(html, filename, options.less);
|
var lessResult = yield inlineLinkLess(html, filename, options.less);
|
||||||
|
html = lessResult.html;
|
||||||
|
files.push(lessResult.files);
|
||||||
|
|
||||||
// TODO inline links: css
|
// TODO inline links: css
|
||||||
|
|
||||||
// TODO inline scripts
|
// TODO inline scripts
|
||||||
// browserify js? => scriptify
|
// browserify js? => scriptify
|
||||||
|
|
||||||
// Inline paths -> datauris
|
// Inline paths -> datauris
|
||||||
html = inlineCss(html, filename);
|
var styleResult = inlineStyle(html, filename); // Inline styles
|
||||||
html = inlineImg(html, filename);
|
html = styleResult.html;
|
||||||
|
files.push(styleResult.files);
|
||||||
|
|
||||||
return html;
|
var imgResult = inlineImg(html, filename); // Inline images
|
||||||
|
html = imgResult.html;
|
||||||
|
files.push(imgResult.files);
|
||||||
|
|
||||||
|
var result = {
|
||||||
|
html: html,
|
||||||
|
files: _.unique(_.flatten(files, true))
|
||||||
|
};
|
||||||
|
return (options.verbose ? result : result.html);
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = inline;
|
module.exports = inline;
|
||||||
|
|||||||
@@ -4,18 +4,23 @@ var path = require('path');
|
|||||||
var rework = require('rework');
|
var rework = require('rework');
|
||||||
var url = require('rework-plugin-url');
|
var url = require('rework-plugin-url');
|
||||||
|
|
||||||
var inline = function (css, filePath) {
|
var inline = function (css, filename) {
|
||||||
var basePath = path.dirname(filePath);
|
var files = [];
|
||||||
|
var basePath = path.dirname(filename);
|
||||||
css = rework(css)
|
css = rework(css)
|
||||||
.use(url(function (url) {
|
.use(url(function (url) {
|
||||||
if (isLocalPath(url)) {
|
if (isLocalPath(url)) {
|
||||||
url = path.resolve(basePath, url);
|
url = path.resolve(basePath, url);
|
||||||
|
files.push(url);
|
||||||
url = datauri(url);
|
url = datauri(url);
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
}))
|
}))
|
||||||
.toString();
|
.toString();
|
||||||
return css;
|
return {
|
||||||
|
css: css,
|
||||||
|
files: files
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = inline;
|
module.exports = inline;
|
||||||
|
|||||||
@@ -3,19 +3,24 @@ var datauri = require('datauri');
|
|||||||
var isLocalPath = require('is-local-path');
|
var isLocalPath = require('is-local-path');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var inline = function (html, filePath) {
|
var inline = function (html, filename) {
|
||||||
var basedir = path.dirname(filePath);
|
var files = [];
|
||||||
|
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'));
|
return isLocalPath($(element).attr('src'));
|
||||||
});
|
});
|
||||||
images.each(function (index, element) {
|
images.each(function (index, element) {
|
||||||
var src = $(element).attr('src');
|
var src = $(element).attr('src');
|
||||||
var filePath = path.resolve(basedir, src);
|
var filename = path.resolve(basedir, src);
|
||||||
src = datauri(filePath);
|
files.push(filename);
|
||||||
|
src = datauri(filename);
|
||||||
$(element).attr('src', src);
|
$(element).attr('src', src);
|
||||||
});
|
});
|
||||||
return $.html();
|
return {
|
||||||
|
html: $.html(),
|
||||||
|
files: files
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = inline;
|
module.exports = inline;
|
||||||
|
|||||||
@@ -7,13 +7,12 @@ var less = require('less');
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
|
||||||
var render = co.wrap(function * (filepath, options) {
|
var render = co.wrap(function * (filename, options) {
|
||||||
options = _.assign(options || {}, {
|
options = _.assign(options || {}, {
|
||||||
filename: filepath
|
filename: filename
|
||||||
});
|
});
|
||||||
var contents = yield fs.readFile(filepath, 'utf8');
|
var contents = yield fs.readFile(filename, 'utf8');
|
||||||
var output = yield less.render(contents, options);
|
return yield less.render(contents, options);
|
||||||
return output.css;
|
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
* @params html
|
* @params html
|
||||||
@@ -21,8 +20,9 @@ var render = co.wrap(function * (filepath, options) {
|
|||||||
* @params options
|
* @params options
|
||||||
* LESS compiler options
|
* LESS compiler options
|
||||||
*/
|
*/
|
||||||
var inline = co.wrap(function * (html, filepath, options) {
|
var inline = co.wrap(function * (html, filename, options) {
|
||||||
var basedir = path.dirname(filepath);
|
var files = [];
|
||||||
|
var basedir = path.dirname(filename);
|
||||||
options = _.defaults(options || {}, {
|
options = _.defaults(options || {}, {
|
||||||
relativeUrls: true
|
relativeUrls: true
|
||||||
});
|
});
|
||||||
@@ -36,23 +36,29 @@ var inline = co.wrap(function * (html, filepath, options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// render stylesheets
|
// render stylesheets
|
||||||
var contents = [];
|
var outputs = [];
|
||||||
links.each(function (index, element) {
|
links.each(function (index, element) {
|
||||||
var href = $(element).attr('href');
|
var href = $(element).attr('href');
|
||||||
var filepath = path.resolve(basedir, href);
|
var filename = path.resolve(basedir, href);
|
||||||
contents.push(render(filepath, options));
|
files.push(filename);
|
||||||
|
outputs.push(render(filename, options));
|
||||||
});
|
});
|
||||||
contents = yield contents;
|
outputs = yield outputs;
|
||||||
|
|
||||||
// TODO for each content: convert css url path -> datauri
|
|
||||||
// can use inline-css-url
|
|
||||||
|
|
||||||
// replace links
|
// replace links
|
||||||
links.each(function (index, element) {
|
links.each(function (index, element) {
|
||||||
var style = $('<style>').html(contents[index]);
|
var style = $('<style>').html(outputs[index].css);
|
||||||
$(element).replaceWith(style);
|
$(element).replaceWith(style);
|
||||||
});
|
});
|
||||||
|
|
||||||
return $.html();
|
// create list of imported files from all outputs, unique listing
|
||||||
|
files.push(_.map(outputs, function (output) {
|
||||||
|
return output.imports;
|
||||||
|
}));
|
||||||
|
|
||||||
|
return {
|
||||||
|
html: $.html(),
|
||||||
|
files: files
|
||||||
|
};
|
||||||
});
|
});
|
||||||
module.exports = inline;
|
module.exports = inline;
|
||||||
|
|||||||
@@ -11,15 +11,17 @@ var unwrap = function (value) {
|
|||||||
var regexp = new RegExp('^' + prefix + '\\s*(.*)\\s+' + suffix + '$');
|
var regexp = new RegExp('^' + prefix + '\\s*(.*)\\s+' + suffix + '$');
|
||||||
return value.replace(regexp, '$1');
|
return value.replace(regexp, '$1');
|
||||||
};
|
};
|
||||||
var inline = function (html, filePath) {
|
var inline = function (html, filename) {
|
||||||
|
var files = [];
|
||||||
var $ = cheerio.load(html, {decodeEntities: false});
|
var $ = cheerio.load(html, {decodeEntities: false});
|
||||||
|
|
||||||
// style elements
|
// style elements
|
||||||
var styles = $('style');
|
var styles = $('style');
|
||||||
styles.each(function (index, element) {
|
styles.each(function (index, element) {
|
||||||
var css = $(element).html();
|
var css = $(element).html();
|
||||||
css = inlineUrl(css, filePath);
|
result = inlineUrl(css, filename);
|
||||||
$(element).html(css);
|
files.push(result.files);
|
||||||
|
$(element).html(result.css);
|
||||||
});
|
});
|
||||||
|
|
||||||
// style attributes
|
// style attributes
|
||||||
@@ -27,13 +29,17 @@ var inline = function (html, filePath) {
|
|||||||
attributes.each(function (index, element) {
|
attributes.each(function (index, element) {
|
||||||
var css = $(element).attr('style');
|
var css = $(element).attr('style');
|
||||||
css = wrap(css);
|
css = wrap(css);
|
||||||
css = inlineUrl(css, filePath);
|
result = inlineUrl(css, filename);
|
||||||
css = string(css).collapseWhitespace().toString();
|
files.push(result.files);
|
||||||
|
css = string(result.css).collapseWhitespace().toString();
|
||||||
css = unwrap(css);
|
css = unwrap(css);
|
||||||
$(element).attr('style', css);
|
$(element).attr('style', css);
|
||||||
});
|
});
|
||||||
|
|
||||||
return $.html();
|
return {
|
||||||
|
html: $.html(),
|
||||||
|
files: files
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = inline;
|
module.exports = inline;
|
||||||
@@ -3,7 +3,7 @@ var path = require('path');
|
|||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
var filepath = path.resolve(__dirname, './fixtures/index.html');
|
var filepath = path.resolve(__dirname, './fixtures/index.html');
|
||||||
var options = {};
|
var options = {verbose: true};
|
||||||
|
|
||||||
inline(filepath, options)
|
inline(filepath, options)
|
||||||
.then(function (html) {
|
.then(function (html) {
|
||||||
|
|||||||
Reference in New Issue
Block a user