mirror of
https://github.com/vale981/metalsmith-collections
synced 2025-03-05 09:21:39 -05:00
Merge pull request #13 from dpobel/limit_option
Added the ability to limit the size of a collection
This commit is contained in:
commit
e51f6080e1
9 changed files with 83 additions and 1 deletions
14
Readme.md
14
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 `collection` metadata
|
||||||
- can match files by pattern
|
- can match files by pattern
|
||||||
|
- can limit the number of files in a collection
|
||||||
- adds collections to global metadata
|
- adds collections to global metadata
|
||||||
- adds `next` and `previous` references to each file in the collection
|
- 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
|
## CLI Usage
|
||||||
|
|
||||||
All of the same options apply, just add them to the `"plugins"` key in your `metalsmith.json` configuration:
|
All of the same options apply, just add them to the `"plugins"` key in your `metalsmith.json` configuration:
|
||||||
|
|
|
@ -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){
|
keys.forEach(function(key){
|
||||||
|
@ -91,6 +91,10 @@ function plugin(opts){
|
||||||
var settings = opts[key];
|
var settings = opts[key];
|
||||||
var col = metadata[key];
|
var col = metadata[key];
|
||||||
var last = col.length - 1;
|
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;
|
if (settings.refer === false) return;
|
||||||
col.forEach(function(file, i){
|
col.forEach(function(file, i){
|
||||||
if (0 != i) file.previous = col[i-1];
|
if (0 != i) file.previous = col[i-1];
|
||||||
|
|
2
test/fixtures/limit/build/one.md
vendored
Normal file
2
test/fixtures/limit/build/one.md
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
one
|
2
test/fixtures/limit/build/three.md
vendored
Normal file
2
test/fixtures/limit/build/three.md
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
three
|
2
test/fixtures/limit/build/two.md
vendored
Normal file
2
test/fixtures/limit/build/two.md
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
two
|
6
test/fixtures/limit/src/one.md
vendored
Normal file
6
test/fixtures/limit/src/one.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
collection: articles
|
||||||
|
title: Alpha
|
||||||
|
---
|
||||||
|
|
||||||
|
one
|
6
test/fixtures/limit/src/three.md
vendored
Normal file
6
test/fixtures/limit/src/three.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
collection: articles
|
||||||
|
title: Gamma
|
||||||
|
---
|
||||||
|
|
||||||
|
three
|
6
test/fixtures/limit/src/two.md
vendored
Normal file
6
test/fixtures/limit/src/two.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
collection: articles
|
||||||
|
title: Beta
|
||||||
|
---
|
||||||
|
|
||||||
|
two
|
|
@ -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){
|
it('should add next and previous references', function(done){
|
||||||
var metalsmith = Metalsmith('test/fixtures/references');
|
var metalsmith = Metalsmith('test/fixtures/references');
|
||||||
metalsmith
|
metalsmith
|
||||||
|
|
Loading…
Add table
Reference in a new issue