From ee4ddb98b7dfcd49f297335cfdf2222a21049f4c Mon Sep 17 00:00:00 2001 From: Aaron Boushley Date: Thu, 6 Nov 2014 10:30:52 -0800 Subject: [PATCH] Allow front matter and pattern collections. Adjust the matching code to allow collections to be defined in the front matter and by pattern. Currently if you define collection in the front matter no patterns will match that file. This aides in building something like a tagging system on a blog. All posts can be matched with something like `'*.md'` or `'blog/**/*'` and then tags can be added in the individual posts `collection` front matter. --- lib/index.js | 9 ++++----- test/index.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7f75bde..9fed3ef 100644 --- a/lib/index.js +++ b/lib/index.js @@ -171,11 +171,10 @@ function matcher(cols){ debug('adding new collection through metadata: %s', key); }); } - else { - for (var key in matchers){ - var m = matchers[key]; - if (m && m.match(file)) matches.push(key); - } + + for (var key in matchers){ + var m = matchers[key]; + if (m && m.match(file)) matches.push(key); } data.collection = unique(matches); diff --git a/test/index.js b/test/index.js index c4f3adc..914e0b5 100644 --- a/test/index.js +++ b/test/index.js @@ -243,4 +243,22 @@ describe('metalsmith-collections', function(){ }); }); + it('should allow collections by pattern and front matter', function (done) { + var metalsmith = Metalsmith('test/fixtures/multi'); + metalsmith + .use(collections({ articles: {}, posts: {}, drafts: {}, blog: '*.md' })) + .build(function(err){ + if (err) return done(err); + var m = metalsmith.metadata(); + assert.equal(3, m.blog.length); + assert.equal(2, m.articles.length); + assert.equal(1, m.drafts.length); + assert.equal(1, m.posts.length); + assert.equal(m.collections.blog, m.blog); + assert.equal(m.collections.articles, m.articles); + assert.equal(m.collections.drafts, m.drafts); + assert.equal(m.collections.posts, m.posts); + done(); + }); + }); });