mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2026-01-11 19:55:55 +00:00
Change api to separate html file and string processing.
This commit is contained in:
25
lib/index.js
25
lib/index.js
@@ -6,30 +6,22 @@ var inlineLess = require('./inline-less');
|
||||
var R = require('ramda');
|
||||
var Ru = require('@panosoft/ramda-utils');
|
||||
|
||||
var inlineHtml = {};
|
||||
/**
|
||||
* Embed referenced local assets within and HTML file.
|
||||
*
|
||||
* @param {String} html
|
||||
* Filename or html string.
|
||||
* @param {Object} options
|
||||
*
|
||||
* @return {Promise}
|
||||
*/
|
||||
var inlineHtml = co.wrap(function * (html, options) {
|
||||
inlineHtml.html = co.wrap(function * (html, options) {
|
||||
options = Ru.defaults({
|
||||
filename: null,
|
||||
less: {},
|
||||
verbose: false
|
||||
}, options || {});
|
||||
var filename;
|
||||
try {
|
||||
filename = html;
|
||||
html = yield fs.readFile(filename, 'utf8');
|
||||
}
|
||||
catch (error) {
|
||||
if (error.code === 'ENOENT') filename = options.filename;
|
||||
else throw error;
|
||||
}
|
||||
var filename = options.filename;
|
||||
|
||||
// Embed assets
|
||||
var files = [filename];
|
||||
@@ -53,11 +45,14 @@ var inlineHtml = co.wrap(function * (html, options) {
|
||||
}
|
||||
|
||||
files = R.uniq(files);
|
||||
var result = {
|
||||
html,
|
||||
files
|
||||
};
|
||||
var result = { html, files };
|
||||
return (options.verbose ? result : result.html);
|
||||
});
|
||||
|
||||
inlineHtml.file = co.wrap(function * (filename, options) {
|
||||
var html = yield fs.readFile(filename, 'utf8');
|
||||
options = R.merge(options || {}, {filename});
|
||||
return yield inlineHtml.html(html, options);
|
||||
});
|
||||
|
||||
module.exports = inlineHtml;
|
||||
|
||||
Reference in New Issue
Block a user