Allow Objects...

This commit is contained in:
Valentin Boettcher 2017-09-01 11:52:15 +02:00
parent db2bbbdbc2
commit bac79a2a4d

View file

@ -1,4 +1,3 @@
var debug = require('debug')('metalsmith-collections');
var extend = require('extend');
var multimatch = require('multimatch');
@ -35,10 +34,9 @@ function plugin(opts, globalOpts){
Object.keys(files).forEach(function(file) {
debug('checking file: %s', file);
var data = files[file];
data.path = file;
data.path = file
match(file, data).forEach(function(key){
Object.keys(match(file, data)).forEach(function(key) {
if (key && keys.indexOf(key) < 0) {
opts[key] = {};
keys.push(key);
@ -53,7 +51,7 @@ function plugin(opts, globalOpts){
* Ensure that a default empty collection exists.
*/
keys.forEach(function(key) {
Object.keys(keys).forEach(function(key) {
metadata[key] = metadata[key] || [];
});
@ -70,19 +68,18 @@ function plugin(opts, globalOpts){
/**
* Sort the collections.
*/
keys.forEach(function(key) {
debug('sorting collection: %s', key);
var settings = opts[key];
var sort = settings.sortBy || (globalOpts.sortBy || 'date');
var sort = settings.sortBy || 'date';
var col = metadata[key];
if ('function' == typeof sort) {
col.sort(sort);
} else {
col.sort(function(a, b) {
a = a[sort];
b = b[sort];
a = (a.collection[key]) ? (a.collection[key][sort] || a[sort]) : a[sort];
b = (b.collection[key]) ? (b.collection[key][sort] || b[sort]) : b[sort];
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return 1;
@ -125,7 +122,9 @@ function plugin(opts, globalOpts){
var col = metadata[key];
col.metadata = (typeof settings.metadata === 'string') ?
loadMetadata(settings.metadata) :
settings.metadata;
(settings.metadata || {});
Object.assign(col.metadata, globalOpts);
});
/**
@ -152,8 +151,12 @@ function normalize(options){
for (var key in options) {
var val = options[key];
if ('string' == typeof val) options[key] = { pattern: val };
if (val instanceof Array) options[key] = { pattern: val };
if ('string' == typeof val) options[key] = {
pattern: val
};
if (val instanceof Array) options[key] = {
pattern: val
};
}
return options;
@ -177,7 +180,7 @@ function matcher(cols){
}
matchers[key] = {
match: function(file) {
return multimatch(file, opts.pattern)
return multimatch(file, opts.pattern);
}
};
});
@ -187,10 +190,23 @@ function matcher(cols){
if (data.collection) {
var collection = data.collection;
if (!Array.isArray(collection)) {
collection = [collection];
let tmpCollection = {};
if (Array.isArray(collection)) {
for (let collItem of collection) {
tmpCollection[collItem] = {};
}
collection.forEach(function(key){
} else if (collection.constructor === String)
tmpCollection[collection] = {};
else if (collection instanceof Object)
tmpCollection = collection;
else {
collection = {};
}
collection = data.collection = tmpCollection;
Object.keys(collection).forEach(function(key) {
matches.push(key);
if (key && keys.indexOf(key) < 0) {
@ -206,7 +222,6 @@ function matcher(cols){
}
}
data.collection = unique(matches);
return data.collection;
return data.collection || {};
};
}