mirror of
https://github.com/vale981/metalsmith-collections
synced 2025-03-05 09:21:39 -05:00
add sort by function
This commit is contained in:
parent
6ed0b022b2
commit
d1806abe0c
8 changed files with 58 additions and 11 deletions
|
@ -41,7 +41,7 @@ function plugin(opts){
|
|||
});
|
||||
|
||||
/**
|
||||
* Sort the collection.
|
||||
* Sort the collections.
|
||||
*/
|
||||
|
||||
keys.forEach(function(key){
|
||||
|
@ -49,6 +49,10 @@ function plugin(opts){
|
|||
var settings = opts[key];
|
||||
var sort = settings.sortBy || 'date';
|
||||
var col = metadata[key];
|
||||
|
||||
if ('function' == typeof sort) {
|
||||
col.sort(sort);
|
||||
} else {
|
||||
col.sort(function(a, b){
|
||||
a = a[sort];
|
||||
b = b[sort];
|
||||
|
@ -59,6 +63,8 @@ function plugin(opts){
|
|||
if (a > b) return 1;
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
if (settings.reverse) col.reverse();
|
||||
});
|
||||
|
||||
|
|
1
test/fixtures/sort-function/build/one.md
vendored
Normal file
1
test/fixtures/sort-function/build/one.md
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
one
|
1
test/fixtures/sort-function/build/three.md
vendored
Normal file
1
test/fixtures/sort-function/build/three.md
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
three
|
1
test/fixtures/sort-function/build/two.md
vendored
Normal file
1
test/fixtures/sort-function/build/two.md
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
two
|
6
test/fixtures/sort-function/src/one.md
vendored
Normal file
6
test/fixtures/sort-function/src/one.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
collection: articles
|
||||
title: Alpha
|
||||
---
|
||||
|
||||
one
|
6
test/fixtures/sort-function/src/three.md
vendored
Normal file
6
test/fixtures/sort-function/src/three.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
collection: articles
|
||||
title: Gamma
|
||||
---
|
||||
|
||||
three
|
6
test/fixtures/sort-function/src/two.md
vendored
Normal file
6
test/fixtures/sort-function/src/two.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
collection: articles
|
||||
title: Beta
|
||||
---
|
||||
|
||||
two
|
|
@ -72,6 +72,26 @@ describe('metalsmith-collections', function(){
|
|||
});
|
||||
});
|
||||
|
||||
it('should accept a "sortBy" function', function(done){
|
||||
var metalsmith = Metalsmith('test/fixtures/sort');
|
||||
metalsmith
|
||||
.use(collections({ articles: { sortBy: sort }}))
|
||||
.build(function(err){
|
||||
if (err) return done(err);
|
||||
var articles = metalsmith.metadata().articles;
|
||||
assert.equal('Gamma', articles[0].title);
|
||||
assert.equal('Beta', articles[1].title);
|
||||
assert.equal('Alpha', articles[2].title);
|
||||
done();
|
||||
});
|
||||
|
||||
function sort(a, b){
|
||||
a = a.title.slice(1);
|
||||
b = b.title.slice(1);
|
||||
return a > b ? 1 : -1;
|
||||
}
|
||||
});
|
||||
|
||||
it('should accept a "reverse" option', function(done){
|
||||
var metalsmith = Metalsmith('test/fixtures/sort');
|
||||
metalsmith
|
||||
|
|
Loading…
Add table
Reference in a new issue