Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including metalsmith-each with all npm packages installed. Try it out:

var metalsmithEach = require("metalsmith-each")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

metalsmith-each v0.1.1

Metalsmith plugin for easily applying a function to each file or filename.

metalsmith-each

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.

Installation

$ npm install metalsmith-each

Usage

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);
  }
));

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free