mirror of
https://github.com/vale981/metalsmith-collections
synced 2025-03-04 17:01:41 -05:00
Allow Objects...
This commit is contained in:
parent
db2bbbdbc2
commit
bac79a2a4d
1 changed files with 54 additions and 39 deletions
93
lib/index.js
93
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');
|
||||||
|
@ -20,26 +19,25 @@ module.exports = plugin;
|
||||||
* @return {Function}
|
* @return {Function}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function plugin(opts, globalOpts){
|
function plugin(opts, globalOpts) {
|
||||||
opts = normalize(opts);
|
opts = normalize(opts);
|
||||||
var keys = Object.keys(opts);
|
var keys = Object.keys(opts);
|
||||||
var match = matcher(opts);
|
var match = matcher(opts);
|
||||||
|
|
||||||
return function(files, metalsmith, done){
|
return function(files, metalsmith, done) {
|
||||||
var metadata = metalsmith.metadata();
|
var metadata = metalsmith.metadata();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the files in each collection.
|
* Find the files in each collection.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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) {
|
||||||
|
if (key && keys.indexOf(key) < 0) {
|
||||||
match(file, data).forEach(function(key){
|
|
||||||
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] || [];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -61,8 +59,8 @@ function plugin(opts, globalOpts){
|
||||||
* Merge global options with collection options
|
* Merge global options with collection options
|
||||||
*/
|
*/
|
||||||
|
|
||||||
keys.forEach(function(key){
|
keys.forEach(function(key) {
|
||||||
for(var gKey in globalOpts){
|
for (var gKey in globalOpts) {
|
||||||
opts[key][gKey] = opts[key][gKey] || globalOpts[gKey];
|
opts[key][gKey] = opts[key][gKey] || globalOpts[gKey];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -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;
|
||||||
|
@ -99,19 +96,19 @@ function plugin(opts, globalOpts){
|
||||||
* Add `next` and `previous` references and apply the `limit` option
|
* Add `next` and `previous` references and apply the `limit` option
|
||||||
*/
|
*/
|
||||||
|
|
||||||
keys.forEach(function(key){
|
keys.forEach(function(key) {
|
||||||
debug('referencing collection: %s', key);
|
debug('referencing collection: %s', key);
|
||||||
var settings = opts[key];
|
var settings = opts[key];
|
||||||
var col = metadata[key];
|
var col = metadata[key];
|
||||||
var last = col.length - 1;
|
var last = col.length - 1;
|
||||||
if (opts[key].limit && opts[key].limit < col.length) {
|
if (opts[key].limit && opts[key].limit < col.length) {
|
||||||
col = metadata[key] = col.slice(0, opts[key].limit);
|
col = metadata[key] = col.slice(0, opts[key].limit);
|
||||||
last = opts[key].limit - 1;
|
last = opts[key].limit - 1;
|
||||||
}
|
}
|
||||||
if (settings.refer === false) return;
|
if (settings.refer === false) return;
|
||||||
col.forEach(function(file, i){
|
col.forEach(function(file, i) {
|
||||||
if (0 != i) file.previous = col[i-1];
|
if (0 != i) file.previous = col[i - 1];
|
||||||
if (last != i) file.next = col[i+1];
|
if (last != i) file.next = col[i + 1];
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -119,13 +116,15 @@ function plugin(opts, globalOpts){
|
||||||
* Add collection metadata
|
* Add collection metadata
|
||||||
*/
|
*/
|
||||||
|
|
||||||
keys.forEach(function(key){
|
keys.forEach(function(key) {
|
||||||
debug('adding metadata: %s', key);
|
debug('adding metadata: %s', key);
|
||||||
var settings = opts[key];
|
var settings = opts[key];
|
||||||
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -133,7 +132,7 @@ function plugin(opts, globalOpts){
|
||||||
*/
|
*/
|
||||||
|
|
||||||
metadata.collections = {};
|
metadata.collections = {};
|
||||||
keys.forEach(function(key){
|
keys.forEach(function(key) {
|
||||||
return metadata.collections[key] = metadata[key];
|
return metadata.collections[key] = metadata[key];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -147,13 +146,17 @@ function plugin(opts, globalOpts){
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function normalize(options){
|
function normalize(options) {
|
||||||
options = options || {};
|
options = 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;
|
||||||
|
@ -166,31 +169,44 @@ function normalize(options){
|
||||||
* @return {Function}
|
* @return {Function}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function matcher(cols){
|
function matcher(cols) {
|
||||||
var keys = Object.keys(cols);
|
var keys = Object.keys(cols);
|
||||||
var matchers = {};
|
var matchers = {};
|
||||||
|
|
||||||
keys.forEach(function(key){
|
keys.forEach(function(key) {
|
||||||
var opts = cols[key];
|
var opts = cols[key];
|
||||||
if (!opts.pattern) {
|
if (!opts.pattern) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
matchers[key] = {
|
matchers[key] = {
|
||||||
match: function(file) {
|
match: function(file) {
|
||||||
return multimatch(file, opts.pattern)
|
return multimatch(file, opts.pattern);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return function(file, data){
|
return function(file, data) {
|
||||||
var matches = [];
|
var matches = [];
|
||||||
|
|
||||||
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] = {};
|
||||||
|
}
|
||||||
|
} else if (collection.constructor === String)
|
||||||
|
tmpCollection[collection] = {};
|
||||||
|
else if (collection instanceof Object)
|
||||||
|
tmpCollection = collection;
|
||||||
|
else {
|
||||||
|
collection = {};
|
||||||
}
|
}
|
||||||
collection.forEach(function(key){
|
|
||||||
|
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) {
|
||||||
|
@ -199,14 +215,13 @@ function matcher(cols){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var key in matchers){
|
for (var key in matchers) {
|
||||||
var m = matchers[key];
|
var m = matchers[key];
|
||||||
if (m.match(file).length) {
|
if (m.match(file).length) {
|
||||||
matches.push(key);
|
matches.push(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data.collection = unique(matches);
|
return data.collection || {};
|
||||||
return data.collection;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue