mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-10-29 17:40:29 +00:00
Refactor.
This commit is contained in:
@@ -7,7 +7,6 @@ const R = require('ramda');
|
||||
const Ru = require('@panosoft/ramda-utils');
|
||||
|
||||
const forEachIndexed = R.addIndex(R.forEach);
|
||||
const resolve = R.curry((a,b) => path.resolve(a,b));
|
||||
|
||||
const render = R.curryN(2, co.wrap(function * (options, filename) {
|
||||
options = R.merge(options || {}, { filename });
|
||||
@@ -28,24 +27,21 @@ const render = R.curryN(2, co.wrap(function * (options, filename) {
|
||||
const inlineLess = co.wrap(function * ($, filename, options) {
|
||||
options = Ru.defaults({ less: {} }, options || {});
|
||||
options = Ru.defaults({ relativeUrls: true }, options.less);
|
||||
var files = [];
|
||||
const basedir = path.dirname(filename);
|
||||
const getAttr = R.curry((attr, element) => $(element).attr(attr));
|
||||
const getStylesheet = R.pipe(getAttr('href'), resolve(basedir));
|
||||
var files = [];
|
||||
try {
|
||||
const links = $('link[rel="stylesheet/less"]')
|
||||
.filter((index, link) => isLocalPath($(link).attr('href')))
|
||||
.toArray();
|
||||
|
||||
const stylesheets = R.map(getStylesheet, links);
|
||||
files = R.concat(files, stylesheets);
|
||||
const outputs = yield R.map(render(options), stylesheets);
|
||||
const getHref = element => path.resolve(basedir, $(element).attr('href'));
|
||||
const hrefs = R.map(getHref, links);
|
||||
files = R.concat(files, hrefs);
|
||||
const outputs = yield R.map(render(options), hrefs);
|
||||
const imports = R.flatten(R.map(R.prop('imports'), outputs));
|
||||
files = R.concat(files, imports);
|
||||
const styles = R.map(output => $('<style>').html(output.css), outputs);
|
||||
|
||||
forEachIndexed((link, index) => $(link).replaceWith(styles[index]), links);
|
||||
|
||||
const replaceLink = (link, index) => $(link).replaceWith(styles[index]);
|
||||
forEachIndexed(replaceLink, links);
|
||||
files = R.uniq(files);
|
||||
return { $, files };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user