Home

Awesome

Metalsmith Plugin Kit

Remove the boring part of making Metalsmith plugins and simply your life. Rely on tested code to do the menial tasks. Reuse the same base libraries in order to try to mitigate the explosion of files in the node_modules/ folder.

This is not a plugin! It helps you make one.

npm version Build Status Dependencies Dev Dependencies codecov.io

Overview

An example goes a long way.

// This is based on the plugin skeleton from Metalsmith.io
var debug = require("debug")("metalsmith-myplugin");
var multimatch = require("multimatch");

module.exports = function myplugin(opts) {
    var matchFunction;

    if (!opts || typeof opts !== "object") {
        opts = {};
    }

    opts.pattern = opts.pattern || [];

    return function (files, metalsmith, done) {
        Object.keys(files).forEach(function(file) {
            if (multimatch(file, opts.pattern).length) {
                debug("myplugin working on: %s", file);
                //
                // here would be your code
                //
            }
        });
        done();
    };
}

Plugin Kit helps remove the need for a matching library and iterating over the properties of the files object. Here's the same plugin rewritten for Plugin Kit.

// Now, the same plugin written using Plugin Kit
var debug = require("debug")("metalsmith-myplugin");
var pluginKit = require("metalsmith-plugin-kit");

module.exports = function myplugin(opts) {
    opts = pluginKit.defaultOptions({
        pattern: []
    }, opts);

    return pluginKit.middleware({
        each: function (filename, fileObject) {
            //
            // here would be your code
            //
        };
        match: opts.pattern
    });
}

There are two huge benefits to this. The first shows up when you want to perform asynchronous tasks because all of the callbacks can return a value (synchronous), return a Promise (asynchronous) or accept a node-style callback (asynchronous).

The second big bonus you get is you don't need to test the file matching code nor do you need to construct tests that confirm the order of events. That's handled and tested by metalsmith-plugin-kit.

There's additional helper methods that simplify common tasks that plugins need to perform, such as creating files, cloning data structures, matching file globs, and renaming functions. The example above shows how to default options and it illustrates the middleware function.

If that wasn't enough, you should make sure you're not underestimating the positive aspects of being able to shift responsibility away from your code and into someone else's. You no longer need to make sure you are matching files correctly. It's not a problem for you to start to use asynchronous processing. When Metalsmith requires a property for a file to be properly created, this library handles it instead of you. I take on that responsibility and will work to maintain and test the features this module exposes.

Installation

Use npm to install this package easily.

$ npm install --save metalsmith-plugin-kit

Alternately you may edit your package.json and add this to your dependencies object:

{
    ...
    "dependencies": {
        ...
        "metalsmith-plugin-kit": "*"
        ...
    }
    ...
}

If you relied on a library like micromatch, minimatch, or multimatch, you can safely remove those lines.

API

<a name="module_metalsmith-plugin-kit"></a>

metalsmith-plugin-kit

Metalsmith Plugin Kit

Example

var pluginKit = require("metalsmith-plugin-kit");

<a name="module_metalsmith-plugin-kit.addFile"></a>

metalsmith-plugin-kit.addFile(files, filename, contents, [options])

Adds a file to the files object. Converts the contents for you automatically. Sets the file mode to 0644 as well.

The contents can be converted:

Kind: static method of <code>metalsmith-plugin-kit</code>
Params

Example

// Make a sample plugin that adds hello.txt.
return pluginKit.middleware({
    after: (files) => {
        pluginKit.addFile(files, "hello.txt", "Hello world!");
    }
});

<a name="module_metalsmith-plugin-kit.callFunction"></a>

metalsmith-plugin-kit.callFunction(fn, [args]) ⇒ <code>Promise.<*></code>

Calls a function and passes it a number of arguments. The function can be synchronous and return a value, asynchronous and return a Promise, or asynchronous and support a Node-style callback.

The result of the promise is the value provided by the returned value, the resolved Promise, or the callback's result. If there is an error thrown or one supplied via a Promise rejection or the callback, this function's Promise will be rejected.

Kind: static method of <code>metalsmith-plugin-kit</code>
Params

Example

function testSync(message) {
    console.log(message);
}

promise = pluginKit.callFunction(testSync, [ "sample message" ]);
// sample message is printed and promise will be resolved asynchronously

Example

function testPromise(message) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(message);
            resolve();
        }, 1000);
    });
}

promise = pluginKit.callFunction(testPromise, [ "sample message" ]);
// promise will be resolved after message is printed

Example

function testCallback(message, done) {
    setTimeout(() => {
        console.log(message);
        done();
    });
}

promise = pluginKit.callFunction(testCallback, [ "sample message" ]);
// promise will be resolved after message is printed

<a name="module_metalsmith-plugin-kit.chain"></a>

metalsmith-plugin-kit.chain() ⇒ <code>function</code>

Chains multiple plugins into one

Kind: static method of <code>metalsmith-plugin-kit</code>
Returns: <code>function</code> - Combined function
Params

        - . <code>function</code> - Plugins to combine

Example

const plugin1 = require('metalsmith-markdown')();
const plugin2 = require('metalsmith-data-loader')();
const pluginKit = require('metalsmith-plugin-kit');

const combined = pluginKit.chain(plugin1, plugin2);
metalsmith.use(combined);

<a name="module_metalsmith-plugin-kit.clone"></a>

metalsmith-plugin-kit.clone(original) ⇒ <code>*</code>

Lightweight object clone function, primarily designed for plain objects, expecially targeted for options to middleware.

Copies functions, regular expressions, Buffers, and other specialty objects; does not close those items. Does not handle circular references.

Kind: static method of <code>metalsmith-plugin-kit</code>
Returns: <code>*</code> - clone
Params

Example

a = {};
b = pluginKit.clone(a);
a.test = true;

// This didn't update b because it's a clone.
console.log(JSON.stringify(b)); // {}

<a name="module_metalsmith-plugin-kit.defaultOptions"></a>

metalsmith-plugin-kit.defaultOptions(defaults, override) ⇒ <code>Object</code>

Defaults options by performing a limited, shallow merge of two objects. Returns a new object. Will not assign properties that are not defined in the defaults.

Kind: static method of <code>metalsmith-plugin-kit</code>
Params

Example

result = pluginKit.defaultOptions({
    a: "default",
    b: {
        bDefault: "default"
    }
}, {
    b: {
        bOverride: "override"
    },
    c: "override but won't make it to the result"
});

// result = {
//     a: "default"
//     b: {
//         bOverride: "override"
//     }
// }

<a name="module_metalsmith-plugin-kit.filenameMatcher"></a>

metalsmith-plugin-kit.filenameMatcher(match, [options]) ⇒ <code>matchFunction</code>

Builds a function to determine if a file matches patterns.

The chance that we switch from micromatch is extremely remote, but it could happen. If another library is used, all existing Plugin Kit tests must continue to pass unchanged. It is the duty of this function to remap options or perform the calls in another way so the alternate library can work. All of the flags documented below must continue to work as expected.

Kind: static method of <code>metalsmith-plugin-kit</code>
Params

Example

var matcher;

matcher = pluginKit.filenameMatcher("*.txt");
[
    "test.txt",
    "test.html"
].forEach((fileName) => {
    console.log(fileName, matcher(fileName));
    // test.txt true
    // test.html false
});

<a name="module_metalsmith-plugin-kit.middleware"></a>

metalsmith-plugin-kit.middleware([options]) ⇒ <code>function</code>

Return middleware function. This is why Plugin Kit was created. It helps handle asynchronous tasks, eliminates the need for using your own matcher and you no longer iterate through the files with Object.keys().

Kind: static method of <code>metalsmith-plugin-kit</code>
Returns: <code>function</code> - middleware function
Params

Example

var fileList;

return pluginKit.middleware({
    after: (files) => {
        pluginKit.addFile(files, "all-files.json", fileList);
    },
    before: () => {
        fileList = [];
    },
    each: (filename) => {
        fileList.push(filename);
    }
});

Example

// This silly plugin changes all instances of "fidian" to lower case
// in all text-like files.
return pluginKit.middleware({
    each: (filename, file) => {
        var contents;

        contents = file.contents.toString("utf8");
        contents = contents.replace(/fidian/ig, "fidian");
        file.contents = Buffer.from(contents, "utf8");
    },
    match: "*.{c,htm,html,js,json,md,txt}",
    matchOptions: {
        basename: true
    },

    // Providing a name will rename this middleware so it can be displayed
    // by metalsmith-debug-ui and other tools.
    name: "metalsmith-lowercase-fidian"
});

Example

// Illustrates asynchronous processing.
return pluginKit.middleware({
    after: (files) => {
        // Promise-based. Delay 5 seconds when done building.
        return new Promise((resolve) => {
            setTimeout(() => {
                console.log("I paused for 5 seconds");
                resolve();
            }, 5000);
        });
    },
    before: (files, metalsmith, done) => {
        // Callback-based. Add a file to the build.
        fs.readFile("content.txt", (err, buffer) => {
            if (!err) {
                pluginKit.addFile(files, "content.txt", buffer);
            }
            done(err);
        });
    }
});

<a name="module_metalsmith-plugin-kit.renameFunction"></a>

metalsmith-plugin-kit.renameFunction(fn, name)

Renames a function by assigning the name property. This isn't as simple as just using yourFunction.name = "new name". Because it was done in Plugin Kit, it is also exposed in the unlikely event that plugins want to use it.

Kind: static method of <code>metalsmith-plugin-kit</code>
Params

Example

x = () => {};
console.log(x.name); // Could be undefined, could be "x".
pluginKit.renameFunction(x, "MysteriousFunction");
console.log(x.name); // "MysteriousFunction"

<a name="module_metalsmith-plugin-kit..metalsmithFile"></a>

metalsmith-plugin-kit~metalsmithFile : <code>Object</code>

This is a typical file object from Metalsmith.

Other properties may be defined, but the ones listed here must be defined.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
Properties

<a name="module_metalsmith-plugin-kit..metalsmithFileCollection"></a>

metalsmith-plugin-kitmetalsmithFileCollection : <code>Object.<string, metalsmith-plugin-kitmetalsmithFile></code>

Metalsmith's collection of files.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
<a name="module_metalsmith-plugin-kit..matchItem"></a>

metalsmith-plugin-kit~matchItem : <code>string</code> | <code>RegExp</code> | <code>function</code> | <code>Object</code>

As a string, this is a single match pattern. It supports the following features, which are taken from the Bash 4.3 specification. With each feature listed, a couple sample examples are shown.

When a RegExp, the file is tested against the regular expression.

When this is a function, the filename is passed as the first argument and the file contents are the second argument. If the returned value is truthy, the file matches. This function may not be asynchronous.

When an object, this uses the object's .test() method. Make sure one exists.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
See: https://github.com/micromatch/micromatch#extended-globbing for extended globbing features.
<a name="module_metalsmith-plugin-kit..matchList"></a>

metalsmith-plugin-kit~matchList : <code>matchItem</code> | <code>Array.<matchItem></code>

This can be one matchItem or an array of matchItem values.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
<a name="module_metalsmith-plugin-kit..matchOptions"></a>

metalsmith-plugin-kit~matchOptions : <code>Object</code>

These options control the matching library, which is only used when a string is passed as the matchItem. All listed options will be supported, even in the unlikely future that the matching library is replaced.

Other options are also available from the library itself.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
See: https://github.com/micromatch/micromatch#options for additional options supported by current backend library.
Properties

<a name="module_metalsmith-plugin-kit..matchFunction"></a>

metalsmith-plugin-kit~matchFunction ⇒ <code>boolean</code>

The function that's returned by filenameMatcher. Pass it your filenames and it will synchronously determine if that matches any of the patterns that were previously passed into filenameMatcher.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
See: filenameMatcher
Params

<a name="module_metalsmith-plugin-kit..middlewareDefinition"></a>

metalsmith-plugin-kit~middlewareDefinition : <code>Object</code>

Middleware defintion object.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
See

Properties

<a name="module_metalsmith-plugin-kit..endpointCallback"></a>

metalsmith-plugin-kit~endpointCallback : <code>function</code>

A callback that is called before processing any file or after processing any file.

Uses Node-style callbacks if your function expects more than 2 parameters.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
See

Params

<a name="module_metalsmith-plugin-kit..eachCallback"></a>

metalsmith-plugin-kit~eachCallback : <code>function</code>

This is the function that will be fired when a file matches your match criteria. It will be executed once for each file. It also could run concurrently with other functions when you are performing asynchronous work.

Uses Node-style callbacks if your function expects more than 4 parameters.

Kind: inner typedef of <code>metalsmith-plugin-kit</code>
See

Params

License

This software is licensed under a MIT license that contains additional non-advertising and patent-related clauses. Read full license terms