Created
February 11, 2019 16:58
-
-
Save vanderb/69e34fbd3bea93697badf4aa9360bf33 to your computer and use it in GitHub Desktop.
Revisions
-
vanderb created this gist
Feb 11, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,80 @@ var inky = require('./i.js'); var argv = process.argv.slice(2); var cheerio = require('cheerio'); var path = require('path'); var through = require('through2'); var vfs = require('vinyl-fs'); var Inky = require('inky/lib/inky'); var fs = require('fs'); var mkdirp = require('mkdirp'); var juice = require('juice'); var inky; function Inky(opts, cb) { var stream; opts = opts || {}; opts.cheerio = Inky.mergeCheerioOpts(opts.cheerio); if (typeof inky === 'undefined') { inky = new Inky(opts); } // If the user passed in source files, create a stream if (opts.src) { stream = vfs .src(opts.src) .pipe(transform()); if (opts.dest && typeof cb === 'function') { stream.on('finish', cb); } } // Otherwise, return the transform function else { return transform(); } // This transform function takes in a Vinyl HTML file, converts the code from Inky to HTML, and returns the modified file. function transform() { return through.obj(function(file, enc, callback) { var convertedHtml = inky.releaseTheKraken(file.contents.toString(), opts.cheerio); var options = { preserveMediaQueries: true, webResources: { scripts: false, images: 500 } }; juice.juiceResources(convertedHtml, options, (err, html) => { convertedHtml = html; }); file.contents = new Buffer.from(convertedHtml); // Write to disk manually if the user specified it if (opts.dest) { var outputPath = path.join(opts.dest, path.basename(file.path)); mkdirp(opts.dest, function() { fs.writeFile(outputPath, convertedHtml, callback); }); } else { callback(null, file); } }); } } inky({ src: argv[0], dest: argv[1] }, () => { console.log('Done parsing.'); });