-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c01c12d
commit ba2ade9
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 characters
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; |