Merge pull request #19 from segmentio/multi-collection

Added the ability to add content to multiple collections
This commit is contained in:
Anthony Short 2014-08-12 14:15:55 -07:00
commit 1668b2a73f
8 changed files with 52 additions and 7 deletions

View file

@ -38,7 +38,6 @@ function plugin(opts){
match(file, data).forEach(function(key){
metadata[key] = metadata[key] || [];
metadata[key].push(data);
data.collection = key;
});
});
@ -154,12 +153,22 @@ function matcher(cols){
return function(file, data){
var matches = [];
var key = data.collection;
if (data.collection) {
var collection = data.collection;
if (!Array.isArray(collection)) collection = [collection];
collection.forEach(function(key){
if (key && ~keys.indexOf(key)) matches.push(key);
for (key in matchers){
});
}
else {
for (var key in matchers){
var m = matchers[key];
if (m && m.match(file)) matches.push(key);
}
return unique(matches);
}
data.collection = unique(matches);
return data.collection;
};
}

2
test/fixtures/multi/build/one.md vendored Normal file
View file

@ -0,0 +1,2 @@
one

1
test/fixtures/multi/build/three.md vendored Normal file
View file

@ -0,0 +1 @@
three

2
test/fixtures/multi/build/two.md vendored Normal file
View file

@ -0,0 +1,2 @@
two

5
test/fixtures/multi/src/one.md vendored Normal file
View file

@ -0,0 +1,5 @@
---
collection: articles
---
one

1
test/fixtures/multi/src/three.md vendored Normal file
View file

@ -0,0 +1 @@
three

8
test/fixtures/multi/src/two.md vendored Normal file
View file

@ -0,0 +1,8 @@
---
collection:
- articles
- posts
- drafts
---
two

View file

@ -194,4 +194,21 @@ describe('metalsmith-collections', function(){
});
});
it('should allow multiple collections', function (done) {
var metalsmith = Metalsmith('test/fixtures/multi');
metalsmith
.use(collections({ articles: {}, posts: {}, drafts: {} }))
.build(function(err){
if (err) return done(err);
var m = metalsmith.metadata();
assert.equal(2, m.articles.length);
assert.equal(1, m.drafts.length);
assert.equal(1, m.posts.length);
assert.equal(m.collections.articles, m.articles);
assert.equal(m.collections.drafts, m.drafts);
assert.equal(m.collections.posts, m.posts);
done();
});
});
});