forked from akveo/blur-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaginator.coffee
More file actions
44 lines (38 loc) · 1.71 KB
/
paginator.coffee
File metadata and controls
44 lines (38 loc) · 1.71 KB
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
module.exports = (env, callback) ->
### Paginator plugin. Defaults can be overridden in config.json
e.g. "paginator": {"perPage": 10} ###
defaults =
template: 'index.jade' # template that renders pages
articles: 'articles' # directory containing contents to paginate
first: 'index.html' # filename/url for first page
filename: 'page/%d/index.html' # filename for rest of pages
perPage: 2 # number of articles per page
groupSort: {}
# assign defaults any option not set in the config file
options = env.config.paginator or {}
for key, value of defaults
options[key] ?= defaults[key]
getArticles = (contents) ->
# helper that returns a list of articles found in *contents*
# note that each article is assumed to have its own directory in the articles directory
articles = contents[options.articles]._.directories.map (item) -> item.index
# skip articles that does not have a template associated
articles = articles.filter (item) -> item.template isnt 'none'
# sort article by date
articles.sort (a, b) -> b.metadata.sort - a.metadata.sort
groupedArticlesObj = articles.reduce (acc, curr) ->
groupName = curr.metadata.group
if not acc[groupName]
acc[groupName] =
groupName: groupName
items: []
acc[groupName].items.push curr
return acc
, {}
groupedArticles = (val for key, val of groupedArticlesObj)
groupedArticles.sort (a, b) -> (options.groupSort[b.groupName] || 0) - (options.groupSort[a.groupName] || 0)
return groupedArticles
# add the article helper to the environment so we can use it later
env.helpers.getGroupedArticles = getArticles
# tell the plugin manager we are done
callback()