Merge pull request #24 from davidknezic/master

Allow collections through metadata alone
This commit is contained in:
Dominic Barnes 2015-02-06 20:43:32 -08:00
commit 7dc8a7a580
7 changed files with 35 additions and 1 deletions

View file

@ -63,6 +63,7 @@ My article contents...
```
All of the files with a matching `collection` will be added to an array that is exposed as a key of the same name on the global Metalsmith `metadata`.
You can omit passing any options to the plugin when matching based on a `collection` property.
### Collection Metadata

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) < 0){
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) < 0)
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();
});
});
});