Skip to content

wilsaj/metalsmith-each

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published