metalsmith-collections/lib/index.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-02-05 15:06:46 -08:00
2014-02-06 22:55:26 -08:00
var debug = require('debug')('metalsmith-collections');
var merge = require('merge');
2014-02-05 15:06:46 -08:00
/**
* Expose `plugin`.
*/
module.exports = plugin;
/**
* Metalsmith plugin that adds `collections` of files to the global
* metadata as a sorted array.
*
* @param {Object} collections (optional)
* @return {Function}
*/
function plugin(collections){
var cols = collections || {};
var arrs = {};
return function(files, metalsmith, done){
setImmediate(done);
Object.keys(files).forEach(function(file){
2014-02-06 22:55:26 -08:00
debug('checking file: %s', file);
2014-02-05 15:06:46 -08:00
var data = files[file];
var col = data.collection;
if (!cols[col]) return;
arrs[col] = arrs[col] || [];
arrs[col].push(data);
});
Object.keys(arrs).forEach(function(name){
2014-02-06 22:55:26 -08:00
debug('sorting collection: %s', name);
2014-02-05 15:06:46 -08:00
var arr = arrs[name];
var opts = cols[name];
var key = opts.sortBy || 'date';
arr.sort(function(a, b){
a = a[key];
b = b[key];
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return 1;
if (b > a) return -1;
if (a > b) return 1;
return 0;
});
if (opts.reverse) arr.reverse();
});
var data = metalsmith.metadata();
metalsmith.metadata(merge(data, arrs));
2014-02-05 15:06:46 -08:00
};
}