/
index.js
194 lines (175 loc) · 5.45 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
var utils = require('./lib/utils');
/**
* Add middleware to gather collections from frontmatter and
* provide helpers for working with collections, collection groups, and
* views in collection groups.
*
* ```js
* app.use(collections());
* ```
* @name collections
* @param {Object} `config` Configuration object for setting up frontmatter collections.
* @param {Array} `config.exts` Array of extensions to use when adding `preRender` middleware. Defaults to `['md', 'hbs', 'html']`.
* @param {Object} `config.collections` Optional collections to look for in view frontmatter. Default collections are `categories` and `tags`.
* @param {Object} `config.collections.${key}` Individual collection configuration.
* @param {String} `config.collections.${key}.inflection` Singular version of collection key: e.g. `categories: { inflection: 'category' }`
* @param {String} `config.collections.${key}.sortOrder` Default sort direction of views in each collection. Defaults to `asc`.
* @api public
*/
module.exports = function(config) {
return function assembleCollections(app) {
if (!isValidInstance(this)) {
return;
}
var List = this.List;
if (!List) {
throw new Error('Unable to find a "List" constructor on "app".');
}
/**
* 1. Add middleware to gather collection information from front-matter (data)
* 2. Add helpers to retrieve sorted/grouped collections in templates
* 3. ? Add data for accessing collections in templates
*/
var opts = utils.merge({
exts: ['md', 'hbs', 'html'],
collections: {
categories: {
inflection: 'category',
sortOrder: 'asc'
},
tags: {
inflection: 'tag',
sortOrder: 'asc'
}
}
}, this.options, config);
var collections = {};
var collectionKeys = Object.keys(opts.collections);
var self = this;
/**
* `preRender` middleware for building up a collection using the built-in `groupBy` method
* on view collections. This middleware controlled by the passed in `regex` property on the
* plugin configuration.
*/
this.preRender(utils.extRegex(opts.exts), function(file, next) {
var collection = self[file.options.collection];
if (!collection) {
return next();
}
collectionKeys.forEach(function(key) {
if (collections.hasOwnProperty(key)) {
return;
}
var collectionOpts = opts.collections[key];
if (typeof collectionOpts === 'string') {
collectionOpts = { inflection: collectionOpts };
}
var props = [`data.${key}`, `data.${collectionOpts.inflection}`];
var group = collection.groupBy(function(item) {
return props.reduce(function(acc, prop) {
var val = utils.get(item, prop);
if (typeof val !== 'undefined') {
acc = acc.concat(val);
}
return acc;
}, []);
});
collections[key] = group;
});
next();
});
/**
* Helper to iterate or return an array of frontmatter collection keys.
*
* ```handlebars
* {{! use as a block helper }}
* {{#collections}}
* {{this.name}}
* {{/collections}}
*
* {{! use as a subexpression }}
* {{#each (collections)}}
* {{this.name}}
* {{/each}}
* ```
*
* @name collections
* @api public
*/
this.helper('collections', function(options) {
if (options.fn) {
return Object.keys(collections).map(function(key) {
return options.fn({ name: key });
}).join('\n');
}
return Object.keys(collections);
});
/**
* Helper to return the collection instance or iterate over each collection item
* in the collection.
*
* ```handlebars
* {{! use as a block helper }}
* {{#collection "categories"}}
* <span>{{category}}</span>
* <ul>
* {{#each items}}
* <li>{{name}}</li>
* {{/each}}
* </ul>
* {{/collection}}
*
* {{! use as a subexpression }}
* {{#each (collection "categories")}}
* <span>{{category}}</span>
* <ul>
* {{#each items}}
* <li>{{name}}</li>
* {{/each}}
* </ul>
* {{/each}}
* ```
*
* @name collection
* @api public
*/
this.helper('collection', function(name, options) {
var collection = collections[name];
if (typeof collection === 'undefined') {
return options.fn ? options.fn() : '';
}
var sortBy;
if (options.hash && typeof options.hash.sortBy === 'string') {
sortBy = options.hash.sortBy;
}
var collectionOpts = opts.collections[name];
if (options.fn) {
// TODO: need a better way of just getting the collection items (e.g. categories, tags)
return Object.keys(collection).map(function(key) {
var list = collection.get(key);
if (sortBy) {
list = list.sortBy(sortBy);
}
var ctx = {
name: key,
inflection: collectionOpts.inflection
};
ctx[collectionOpts.inflection] = key;
ctx.items = list.items;
return options.fn(ctx);
}).join('\n');
}
return collection;
});
};
};
function isValidInstance(app) {
if (app.isRegistered('assemble-collections')) {
return false;
}
if (app.isApp) {
return true;
}
return false;
}