-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
60 lines (49 loc) · 1.17 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
'use strict';
var extend = require('extend-shallow');
var isobject = require('isobject');
var slugify = require('markdown-slug');
/**
* Helper for sorting pages before rendering. The `slugify`
* function is used to normalize the names before the comparison
* is done.
*/
module.exports = function(items, order, options) {
if (isobject(order) && order.hash) {
options = order;
order = null;
}
if (!Array.isArray(items)) {
return [];
}
options = options || {};
var opts = extend({}, this.context, this.options, options.hash);
order = order || opts.order;
if (typeof order === 'string') {
order = order.split(',');
}
if (!Array.isArray(order)) {
return items;
}
var len = order.length;
var arr = [];
for (var i = 0; i < len; i++) {
var item = pluckItem(items, slugify(order[i]));
if (item) {
arr.push(item);
}
}
return arr;
};
function pluckItem(items, name) {
var len = items.length;
for (var i = 0; i < len; i++) {
var item = items[i];
if (typeof item === 'string') {
if (slugify(item) === name) return item;
continue;
}
if (slugify(item.stem) === name) {
return item;
}
}
}