-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
79 lines (66 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
var isValid = require('is-valid-app');
var extend = require('extend-shallow');
var PluginError = require('plugin-error');
var through = require('through2');
/**
* Add `prev`/`next` paging information to the context for files
* in the current stream.
*
* @name plugin
* @param {Object} [options]
* @param {String|Array} [options.sortBy]
* @return {Stream} Returns a [vinyl][] stream
* @api public
*/
module.exports = function(config) {
return function(app) {
if (!isValid(app, 'assemble-pager')) return;
/**
* Add `prev`/`next` paging information to the context for files
* in the current stream.
*
* @name .pager
* @param {Object} [options]
* @param {String|Array} [options.sortBy]
* @return {Stream} Returns a [vinyl][] stream
* @api public
*/
app.define('pager', function(options) {
var opts = extend({}, config, options);
var list = new app.List({pager: true});
return through.obj(function(file, enc, next) {
if (file.isNull()) {
next(null, file);
return;
}
if (!file.isView) {
next(new PluginError('assemble-pager', 'expected an assemble view', {showStack: true}));
return;
}
if (file.data.pager === false) {
next(null, file);
return;
}
if (opts.collection && file.options.collection !== opts.collection) {
next(null, file);
return;
}
list.addItem(file);
next();
}, function(cb) {
if (opts.sortBy) {
list = list.sortBy(opts.sortBy);
}
for (var i = 0; i < list.items.length; i++) {
this.push(list.items[i]);
}
cb();
});
});
};
};
/**
* Expose helpers
*/
module.exports.helpers = require('./lib/helpers');