Skip to content

Commit

Permalink
Write initial plugin structure
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed May 13, 2017
1 parent c01c12d commit ba2ade9
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const minimatch = require('minimatch');
const purifyCSS = require('purify-css');

const plugin = (options) => {
const {content, css, output} = options;
// Delete options that conflict with running in Metalsmith
delete options.content;
delete options.css;
delete options.output;

return function(files, metalsmith, done) {
// Stringify HTML & other structural markup
const fileNames = Object.keys(files);
const filteredFiles = [...content].reduce((filteredFiles, filename) => {
return filteredFiles.concat(
minimatch.match(fileNames, filename, { matchBase: true })
);
}, []);

const structure = [...filteredFiles].reduce((structure, filename) => {
structure.push(files[filename].contents.toString());
return structure;
}, []).toString();

// Stringify CSS
const styles = [...css].reduce((styles, filename) => {
styles.push(files[filename].contents.toString());
return styles;
}, []).toString();

// Pass code and CSS to Purify
purifyCSS(structure, styles, options, (results) => {
files[output] = {
contents: new Buffer(results)
}
});

done();
}
}

module.exports = plugin;

0 comments on commit ba2ade9

Please sign in to comment.