From 0287767bd980ac3d778ffa7edeb174df1fd9f896 Mon Sep 17 00:00:00 2001 From: Damien Pobel Date: Wed, 21 May 2014 08:37:57 +0200 Subject: [PATCH] Added the ability to limit the size of a collection --- Readme.md | 14 +++++++++++ lib/index.js | 6 ++++- test/fixtures/limit/build/one.md | 2 ++ test/fixtures/limit/build/three.md | 2 ++ test/fixtures/limit/build/two.md | 2 ++ test/fixtures/limit/src/one.md | 6 +++++ test/fixtures/limit/src/three.md | 6 +++++ test/fixtures/limit/src/two.md | 6 +++++ test/index.js | 40 ++++++++++++++++++++++++++++++ 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/limit/build/one.md create mode 100644 test/fixtures/limit/build/three.md create mode 100644 test/fixtures/limit/build/two.md create mode 100644 test/fixtures/limit/src/one.md create mode 100644 test/fixtures/limit/src/three.md create mode 100644 test/fixtures/limit/src/two.md diff --git a/Readme.md b/Readme.md index 1aca96f..455bb5c 100644 --- a/Readme.md +++ b/Readme.md @@ -6,6 +6,7 @@ A [Metalsmith](https://github.com/segmentio/metalsmith) plugin that lets you gro - can match files by `collection` metadata - can match files by pattern + - can limit the number of files in a collection - adds collections to global metadata - adds `next` and `previous` references to each file in the collection @@ -94,6 +95,19 @@ metalsmith.use(collections({ })); ``` +On each collection definition, it's possible to add a `limit` option so that the +collection length is not higher than the given limit: + +```js +metalsmith.use(collections({ + lastArticles: { + sortBy: 'date', + limit: 10 + } +})); +``` + + ## CLI Usage All of the same options apply, just add them to the `"plugins"` key in your `metalsmith.json` configuration: diff --git a/lib/index.js b/lib/index.js index 7f75bde..2a9f0d5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -83,7 +83,7 @@ function plugin(opts){ }); /** - * Add `next` and `previous` references. + * Add `next` and `previous` references and apply the `limit` option */ keys.forEach(function(key){ @@ -91,6 +91,10 @@ function plugin(opts){ var settings = opts[key]; var col = metadata[key]; var last = col.length - 1; + if (opts[key].limit && opts[key].limit < col.length) { + col = metadata[key] = col.slice(0, opts[key].limit); + last = opts[key].limit - 1; + } if (settings.refer === false) return; col.forEach(function(file, i){ if (0 != i) file.previous = col[i-1]; diff --git a/test/fixtures/limit/build/one.md b/test/fixtures/limit/build/one.md new file mode 100644 index 0000000..d888cd7 --- /dev/null +++ b/test/fixtures/limit/build/one.md @@ -0,0 +1,2 @@ + +one \ No newline at end of file diff --git a/test/fixtures/limit/build/three.md b/test/fixtures/limit/build/three.md new file mode 100644 index 0000000..b57895e --- /dev/null +++ b/test/fixtures/limit/build/three.md @@ -0,0 +1,2 @@ + +three \ No newline at end of file diff --git a/test/fixtures/limit/build/two.md b/test/fixtures/limit/build/two.md new file mode 100644 index 0000000..f12fcfe --- /dev/null +++ b/test/fixtures/limit/build/two.md @@ -0,0 +1,2 @@ + +two \ No newline at end of file diff --git a/test/fixtures/limit/src/one.md b/test/fixtures/limit/src/one.md new file mode 100644 index 0000000..d37b9bf --- /dev/null +++ b/test/fixtures/limit/src/one.md @@ -0,0 +1,6 @@ +--- +collection: articles +title: Alpha +--- + +one \ No newline at end of file diff --git a/test/fixtures/limit/src/three.md b/test/fixtures/limit/src/three.md new file mode 100644 index 0000000..bf34f25 --- /dev/null +++ b/test/fixtures/limit/src/three.md @@ -0,0 +1,6 @@ +--- +collection: articles +title: Gamma +--- + +three \ No newline at end of file diff --git a/test/fixtures/limit/src/two.md b/test/fixtures/limit/src/two.md new file mode 100644 index 0000000..7a944e4 --- /dev/null +++ b/test/fixtures/limit/src/two.md @@ -0,0 +1,6 @@ +--- +collection: articles +title: Beta +--- + +two \ No newline at end of file diff --git a/test/index.js b/test/index.js index c4f3adc..b7a0715 100644 --- a/test/index.js +++ b/test/index.js @@ -111,6 +111,46 @@ describe('metalsmith-collections', function(){ }); }); + it('should accept a "limit" option', function (done){ + var metalsmith = Metalsmith('test/fixtures/limit'), + limit = 2; + metalsmith + .use(collections({ + articles: { + limit: limit, + sortBy: 'title', + } + })) + .build(function(err){ + if (err) return done(err); + var articles = metalsmith.metadata().articles; + assert.equal(limit, articles.length); + assert.equal('Alpha', articles[0].title); + assert.equal('Beta', articles[1].title); + done(); + }); + }); + + it('should accept a "limit" higher than the collection length', function(done){ + var metalsmith = Metalsmith('test/fixtures/limit'); + metalsmith + .use(collections({ + articles: { + sortBy: 'title', + limit: 25 + } + })) + .build(function(err){ + if (err) return done(err); + var articles = metalsmith.metadata().articles; + assert.equal(3, articles.length); + assert.equal('Alpha', articles[0].title); + assert.equal('Beta', articles[1].title); + assert.equal('Gamma', articles[2].title); + done(); + }); + }); + it('should add next and previous references', function(done){ var metalsmith = Metalsmith('test/fixtures/references'); metalsmith