mirror of
https://github.com/vale981/metalsmith-collections
synced 2025-03-05 09:21:39 -05:00
Allow Objects...
This commit is contained in:
parent
db2bbbdbc2
commit
bac79a2a4d
1 changed files with 54 additions and 39 deletions
51
lib/index.js
51
lib/index.js
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
var debug = require('debug')('metalsmith-collections');
|
var debug = require('debug')('metalsmith-collections');
|
||||||
var extend = require('extend');
|
var extend = require('extend');
|
||||||
var multimatch = require('multimatch');
|
var multimatch = require('multimatch');
|
||||||
|
@ -35,10 +34,9 @@ function plugin(opts, globalOpts){
|
||||||
Object.keys(files).forEach(function(file) {
|
Object.keys(files).forEach(function(file) {
|
||||||
debug('checking file: %s', file);
|
debug('checking file: %s', file);
|
||||||
var data = files[file];
|
var data = files[file];
|
||||||
|
data.path = file;
|
||||||
|
|
||||||
data.path = file
|
Object.keys(match(file, data)).forEach(function(key) {
|
||||||
|
|
||||||
match(file, data).forEach(function(key){
|
|
||||||
if (key && keys.indexOf(key) < 0) {
|
if (key && keys.indexOf(key) < 0) {
|
||||||
opts[key] = {};
|
opts[key] = {};
|
||||||
keys.push(key);
|
keys.push(key);
|
||||||
|
@ -53,7 +51,7 @@ function plugin(opts, globalOpts){
|
||||||
* Ensure that a default empty collection exists.
|
* Ensure that a default empty collection exists.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
keys.forEach(function(key) {
|
Object.keys(keys).forEach(function(key) {
|
||||||
metadata[key] = metadata[key] || [];
|
metadata[key] = metadata[key] || [];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -70,19 +68,18 @@ function plugin(opts, globalOpts){
|
||||||
/**
|
/**
|
||||||
* Sort the collections.
|
* Sort the collections.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
keys.forEach(function(key) {
|
keys.forEach(function(key) {
|
||||||
debug('sorting collection: %s', key);
|
debug('sorting collection: %s', key);
|
||||||
var settings = opts[key];
|
var settings = opts[key];
|
||||||
var sort = settings.sortBy || (globalOpts.sortBy || 'date');
|
var sort = settings.sortBy || 'date';
|
||||||
var col = metadata[key];
|
var col = metadata[key];
|
||||||
|
|
||||||
if ('function' == typeof sort) {
|
if ('function' == typeof sort) {
|
||||||
col.sort(sort);
|
col.sort(sort);
|
||||||
} else {
|
} else {
|
||||||
col.sort(function(a, b) {
|
col.sort(function(a, b) {
|
||||||
a = a[sort];
|
a = (a.collection[key]) ? (a.collection[key][sort] || a[sort]) : a[sort];
|
||||||
b = b[sort];
|
b = (b.collection[key]) ? (b.collection[key][sort] || b[sort]) : b[sort];
|
||||||
if (!a && !b) return 0;
|
if (!a && !b) return 0;
|
||||||
if (!a) return -1;
|
if (!a) return -1;
|
||||||
if (!b) return 1;
|
if (!b) return 1;
|
||||||
|
@ -125,7 +122,9 @@ function plugin(opts, globalOpts){
|
||||||
var col = metadata[key];
|
var col = metadata[key];
|
||||||
col.metadata = (typeof settings.metadata === 'string') ?
|
col.metadata = (typeof settings.metadata === 'string') ?
|
||||||
loadMetadata(settings.metadata) :
|
loadMetadata(settings.metadata) :
|
||||||
settings.metadata;
|
(settings.metadata || {});
|
||||||
|
|
||||||
|
Object.assign(col.metadata, globalOpts);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -152,8 +151,12 @@ function normalize(options){
|
||||||
|
|
||||||
for (var key in options) {
|
for (var key in options) {
|
||||||
var val = options[key];
|
var val = options[key];
|
||||||
if ('string' == typeof val) options[key] = { pattern: val };
|
if ('string' == typeof val) options[key] = {
|
||||||
if (val instanceof Array) options[key] = { pattern: val };
|
pattern: val
|
||||||
|
};
|
||||||
|
if (val instanceof Array) options[key] = {
|
||||||
|
pattern: val
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
|
@ -177,7 +180,7 @@ function matcher(cols){
|
||||||
}
|
}
|
||||||
matchers[key] = {
|
matchers[key] = {
|
||||||
match: function(file) {
|
match: function(file) {
|
||||||
return multimatch(file, opts.pattern)
|
return multimatch(file, opts.pattern);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -187,10 +190,23 @@ function matcher(cols){
|
||||||
|
|
||||||
if (data.collection) {
|
if (data.collection) {
|
||||||
var collection = data.collection;
|
var collection = data.collection;
|
||||||
if (!Array.isArray(collection)) {
|
let tmpCollection = {};
|
||||||
collection = [collection];
|
|
||||||
|
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);
|
matches.push(key);
|
||||||
|
|
||||||
if (key && keys.indexOf(key) < 0) {
|
if (key && keys.indexOf(key) < 0) {
|
||||||
|
@ -206,7 +222,6 @@ function matcher(cols){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data.collection = unique(matches);
|
return data.collection || {};
|
||||||
return data.collection;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue