A Metalsmith plugin for easily applying a function to each file or filename.
This offers a simpler API than creating a full metalsmith plugin for the common case of iterating over each file individually and doing something to the file or filename.
$ npm install metalsmith-each
Just pass a function to metalsmith-each that accepts the metalsmith file object and, optionally, the filename.
var each = require('metalsmith-each');
metalsmith.use(
each(function (file, filename) {
file.build_date = new Date();
}
));
To change the filename, just return a string with the new name.
var each = require('metalsmith-each');
metalsmith.use(
each(function (file, filename) {
return filename.toUpperCase();
}
));
Functions can be async, too. Just accept a done()
function as the third
argument. Note: If you accept the done function, you must call it or your build
will hang.
var each = require('metalsmith-each');
metalsmith.use(
each(function (file, filename, done) {
setTimeout(function () {
file.build_date = new Date();
done();
}, 1000);
}
));
To rename a file with an async function, pass the new name to the done()
function.
var each = require('metalsmith-each');
metalsmith.use(
each(function (file, filename, done) {
setTimeout(function () {
done(filename.toUpperCase());
}, 1000);
}
));
MIT