Allow collections through metadata alone

This commit is contained in:
David Knezic 2014-10-13 12:55:10 +02:00
parent 0f70d87931
commit 86509b0ad3
6 changed files with 34 additions and 1 deletions

View file

@ -36,6 +36,11 @@ function plugin(opts){
debug('checking file: %s', file);
var data = files[file];
match(file, data).forEach(function(key){
if (key && ~~keys.indexOf(key)){
opts[key] = {};
keys.push(key);
}
metadata[key] = metadata[key] || [];
metadata[key].push(data);
});
@ -158,7 +163,10 @@ function matcher(cols){
var collection = data.collection;
if (!Array.isArray(collection)) collection = [collection];
collection.forEach(function(key){
if (key && ~keys.indexOf(key)) matches.push(key);
matches.push(key);
if (key && ~~keys.indexOf(key))
debug('adding new collection through metadata: %s', key);
});
}
else {

3
test/fixtures/noconfig/src/four.md vendored Normal file
View file

@ -0,0 +1,3 @@
---
collection: books
---

3
test/fixtures/noconfig/src/one.md vendored Normal file
View file

@ -0,0 +1,3 @@
---
collection: books
---

3
test/fixtures/noconfig/src/three.md vendored Normal file
View file

@ -0,0 +1,3 @@
---
collection: movies
---

1
test/fixtures/noconfig/src/two.md vendored Normal file
View file

@ -0,0 +1 @@
Nothing

View file

@ -211,4 +211,19 @@ describe('metalsmith-collections', function(){
});
});
it('should allow collections through metadata alone', function (done) {
var metalsmith = Metalsmith('test/fixtures/noconfig');
metalsmith
.use(collections({ movies: {} }))
.build(function(err){
if (err) return done(err);
var m = metalsmith.metadata();
assert.equal(2, m.books.length);
assert.equal(1, m.movies.length);
assert.equal(m.collections.books, m.books);
assert.equal(m.collections.movies, m.movies);
done();
});
});
});