A metalsmith plugin to flatten a directory hierarchy
JavaScript
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.gitattributes
.gitignore
LICENSE
README.md
package.json

README.md

metalsmith-flatten

A metalsmith plugin to flatten a directory hierarchy.

Installation

$ npm install metalsmith-flatten --save-dev

Usage

Flatten the Entire Directory

var Metalsmith = require('metalsmith');
var flatten = require('metalsmith-flatten');

var metalsmith = new Metalsmith(__dirname)
  .use(flatten());

This will transform a directory structure that looks like this:

my-dir/
	one.html
	two.html
	nested/
		three.html
my-other-dir/
	four.html

to a flat output directory:

my-dir-one.html
my-dir-two.html
my-dir-nested-three.html
my-other-dir-four.html

Flatten a Specific Directory

The flatten function takes a string pattern that it will use to determine which directories it should flatten.

var metalsmith = new Metalsmith(__dirname)
  .use(flatten("my-dir"));

Add a Prefix

You can pass a prefix option to the flatten function to prefix all the flattened files with a string.

var metalsmith = new Metalsmith(__dirname)
  .use(flatten({
    prefix: 'my-optional-prefix-',
	pattern: "my-dir"
  }));

This will transform a directory structure that looks like this:

my-dir/
	one.html
	two.html
	nested/
		three.html
my-other-dir/
	four.html

to a flat output directory:

my-optional-prefix-my-dir-one.html
my-optional-prefix-my-dir-two.html
my-optional-prefix-my-dir-nested-three.html
my-other-dir/
	four.html

Flatten Specific Directories from Multiple Patterns

The pattern can also be an array:

var metalsmith = new Metalsmith(__dirname)
  .use(flatten({
  	pattern: [
		"**/*",
		"!my-other-dir"
	]
  }));

Skipping Flattening for a File

A file will be ignored by the plugin if you set the skipFlatten option in the yaml metadata of a file to true.