File object manipulation / transformation in the metalsmith pipeline
JavaScript
Latest commit 9353f12 Jul 12, 2015 @yeojz 1.2.4

README.md

metalsmith-transform

File object manipulation plugin for metalsmith.

npm Build Status

About

metalsmith-transform is a metalsmith plugin which allows the use of custom functions to manipulate the file object in a metalsmith pipeline.

Furthermore, instead of writing a full plugin, you can just pass in the transformation function into this instead.

String methods are inspired by gulp-insert.

Usage

npm install metalsmith-transform

API

import transform from 'metalsmith-transform';

You can pass in an object or a function as an argument. i.e.

metalsmith.use(transform(function(fileObject, metalsmithData){
  return fileObject
}));

// or

metalsmith.use(transform({
  action: '', // append / prepend / wrap / transform
  value: '', // string | function (only if using transform)
  pattern: '*.md'
}));

CLI

"plugins": {
  "metalsmith-transform": {
    "action": "append",
    "value": "Include Me!"
  }
}

Note on CLI Usage: Since JSON does not take functions, only append, prepend and wrap actions are accepted.

Actions

Transform

Calls a function with the file object and metalsmith instance. Function should return the modified contents of the file.

metalsmith.use(transform(function(data, m){
  let contents = data.contents.toString().toUpperCase();
  data.contents = new Buffer(contents);

  return data;
})); 

Append

Appends a string onto the contents.

// Appends 'world' to the contents of every file
metalsmith.use(transform({
  action: 'append',
  value: 'world'
})); 

Prepend

Prepends a string onto the contents.

// Prepends 'Hello' to the contents of every file
metalsmith.use(transform({
  action: 'prepend',
  value: 'hello'
}));

Wrap

Wraps the contents with two strings.

// prepends 'hello' and appends 'world' to the contents
metalsmith.use(transform({
  action: 'wrap',
  value: ['hello', 'world']
}));