-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (45 loc) · 1.68 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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
var through2 = require('through2'),
chunk = require('lodash.chunk'),
path = require('path');
function makePage(originalFile, page, pageNumber) {
// Apparently you have to pass true for this to deep-clone.
// The docs are wrong. :/
var newFile = originalFile.clone(true);
newFile.data = newFile.data || {};
newFile.data.posts = page;
newFile.data.page = pageNumber;
if (pageNumber === 1) return newFile;
var filePath = path.parse(newFile.path);
filePath.dir = path.join(filePath.dir, 'page', pageNumber.toString());
newFile.path = path.format(filePath);
return newFile;
}
module.exports = function() {
return through2.obj(function(file, enc, callback) {
var pageFiles = [],
pages = [];
pages = chunk(file.data.posts, 5);
pageFiles = pages.map(function(page, pageNumber) {
return makePage(file, page, pageNumber + 1);
});
// Per-index page counts
pageFiles.forEach(function(file) {
file.data.pageCount = pageFiles.length;
this.push(file);
}, this);
callback();
});
};