From 6df36fffe108747ab996f52ba2c0c0aa52db8884 Mon Sep 17 00:00:00 2001 From: Anthony Short Date: Tue, 12 Aug 2014 14:12:39 -0700 Subject: [PATCH] Fixed tests --- lib/index.js | 21 +++++++++++---------- test/fixtures/multi/build/one.md | 2 ++ test/fixtures/multi/build/three.md | 1 + test/fixtures/multi/build/two.md | 2 ++ test/fixtures/multi/src/one.md | 5 +++++ test/fixtures/multi/src/three.md | 1 + test/fixtures/multi/src/two.md | 8 ++++++++ test/index.js | 17 +++++++++++++++++ 8 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/multi/build/one.md create mode 100644 test/fixtures/multi/build/three.md create mode 100644 test/fixtures/multi/build/two.md create mode 100644 test/fixtures/multi/src/one.md create mode 100644 test/fixtures/multi/src/three.md create mode 100644 test/fixtures/multi/src/two.md diff --git a/lib/index.js b/lib/index.js index d95d41c..1b2f08e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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,20 +153,22 @@ function matcher(cols){ return function(file, data){ var matches = []; - var collections = data.collection || []; - if (!Array.isArray(collections)) { - collections = [collections]; + 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); + }); } - - collections.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; }; } diff --git a/test/fixtures/multi/build/one.md b/test/fixtures/multi/build/one.md new file mode 100644 index 0000000..d888cd7 --- /dev/null +++ b/test/fixtures/multi/build/one.md @@ -0,0 +1,2 @@ + +one \ No newline at end of file diff --git a/test/fixtures/multi/build/three.md b/test/fixtures/multi/build/three.md new file mode 100644 index 0000000..1d19714 --- /dev/null +++ b/test/fixtures/multi/build/three.md @@ -0,0 +1 @@ +three \ No newline at end of file diff --git a/test/fixtures/multi/build/two.md b/test/fixtures/multi/build/two.md new file mode 100644 index 0000000..f12fcfe --- /dev/null +++ b/test/fixtures/multi/build/two.md @@ -0,0 +1,2 @@ + +two \ No newline at end of file diff --git a/test/fixtures/multi/src/one.md b/test/fixtures/multi/src/one.md new file mode 100644 index 0000000..d920332 --- /dev/null +++ b/test/fixtures/multi/src/one.md @@ -0,0 +1,5 @@ +--- +collection: articles +--- + +one \ No newline at end of file diff --git a/test/fixtures/multi/src/three.md b/test/fixtures/multi/src/three.md new file mode 100644 index 0000000..1d19714 --- /dev/null +++ b/test/fixtures/multi/src/three.md @@ -0,0 +1 @@ +three \ No newline at end of file diff --git a/test/fixtures/multi/src/two.md b/test/fixtures/multi/src/two.md new file mode 100644 index 0000000..3f80e4c --- /dev/null +++ b/test/fixtures/multi/src/two.md @@ -0,0 +1,8 @@ +--- +collection: + - articles + - posts + - drafts +--- + +two \ No newline at end of file diff --git a/test/index.js b/test/index.js index 531522d..c7432d0 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); + }); + }); + });