-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (112 loc) · 4.22 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
/*
* Assemble Plugin: assemble-contrib-pagination
* https://github.com/assemble/assemble-contrib-pagination
* Assemble is the 100% JavaScript static site generator for Node.js, Grunt.js, and Yeoman.
*
* Copyright (c) 2014 Brian Woodward, Jon Schlinkert, contributors
* Licensed under the MIT license.
*/
// node_modules
var matter = require('gray-matter');
var file = require('fs-utils');
var _ = require('lodash');
// local libs
var utils = require('./lib/utils');
var Page = require('./lib/page');
var Collection = require('./lib/collection');
var options = {
stage: 'options:post:configuration'
};
module.exports = function(params, done) {
'use strict';
var grunt = params.grunt;
grunt.verbose.subhead('Running:'.bold, '"assemble-contrib-pagination"');
grunt.verbose.writeln('Stage: '.bold, '"' + params.stage + '"\n');
var opts = params.assemble.options.pagination;
grunt.verbose.writeln('Options: '.bold, require('util').inspect(opts));
/**
* accepted options
*
* options: {
* pagination: {
* template: 'src/templates/blog/index.hbs',
* dest: 'blog/',
* structure: 'page:num/index:ext', // permalinks structure
* per_page: 10,
* src: 'src/templates/*.hbs'
* }
* }
*/
if (opts) {
// extend the options with some defaults
opts = _.extend({
dest: '.',
structure: ':num/index:ext',
per_page: 10
}, opts);
// setup a default template if none is specified
var template = [
'<a href="{{page.pagination.previous_page_path}}">Previous</a> | <a href="{{page.pagination.next_page_path}}">Next</a>',
'{{#each page.pagination.items}}',
'<div>{{this.metadata.src}}</div>',
'<div>{{this.metadata.summary}}</div>',
'{{/each}}'
].join('\n');
// read in the template if one is specified
grunt.verbose.writeln('Template: '.bold, opts.template);
if (opts.template && opts.template.length > 0) {
template = file.readFileSync(opts.template);
}
// Expand given filepaths of the items/pages/posts/etc...
grunt.verbose.writeln('Source: '.bold, opts.src);
var items = utils.loadItems(opts.src || '');
// collection is a convenience for getting items out later
var collection = new Collection({items: items});
// setup the state
var state = {
// item info
currentItemIndex: 0,
totalItems: items.length,
// page info
perPage: opts.per_page,
currentPageIndex: 1,
prevPageIndex: 1
};
state.totalPages = Math.round(state.totalItems / state.perPage);
state.nextPageIndex = Math.min(2, state.totalPages);
// generate each list page based on how many items there are
var pages = {};
do {
// create a new page from the template with the items in the context
var currentPagePath = utils.generatePagePath(opts.dest, state.currentPageIndex);
var page = new Page({
filename: currentPagePath,
content: template,
context: {
pagination: {
page: state.currentPageIndex,
per_page: state.perPage,
items: collection.getItems(state.currentItemIndex, state.perPage),
total_items: state.totalItems,
total_pages: state.totalPages,
previous_page: state.prevPageIndex,
previous_page_path: utils.relative(currentPagePath, utils.generatePagePath(opts.dest, state.prevPageIndex)),
next_page: state.nextPageIndex,
next_page_path: utils.relative(currentPagePath, utils.generatePagePath(opts.dest, state.nextPageIndex))
}
}
});
pages[page.filename] = page;
// update state information
state.currentPageIndex++;
state.prevPageIndex = state.currentPageIndex - 1;
state.nextPageIndex = state.currentPageIndex + (state.currentPageIndex === state.totalPages ? 0 : 1);
state.currentItemIndex += state.perPage;
} while (state.currentPageIndex <= state.totalPages);
// add the new pages to the assemble.options.pages collection
grunt.verbose.writeln('Pages: '.bold, require('util').inspect(pages));
params.assemble.options.pages = _.extend({}, (params.assemble.options.pages || {}), pages);
}
done();
};
module.exports.options = options;