-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (129 loc) · 4.59 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
/*
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 path = require('path');
if (!Array.prototype.includes) require('es7-array.prototype.includes');
var through2 = require('through2');
var handleOffset = require('stratic-handle-offset');
function sortChronological(a, b) {
if (a.data.time.epoch === b.data.time.epoch) {
return 0;
}
return a.data.time.epoch < b.data.time.epoch ? 1 : -1;
}
module.exports = function(template, options) {
if (typeof template !== 'string') throw new Error('template not specified');
var files = [];
var templateFile;
// For each file that comes through, add it to an array
var stream = through2.obj(function(file, enc, callback) {
// Check if the file is the template file
if (file.relative === template) {
templateFile = file;
} else {
files.push(file);
}
callback();
},
// Wait until all files have come through, then attach the non-template files
function(callback) {
if (!templateFile) throw new Error('template not found in stream');
templateFile.data = templateFile.data || {};
// Find years
var years = [];
files.forEach(function(post) {
var year = handleOffset(post.data.time).year();
if (!years.includes(year)) years.push(year);
});
years.sort();
// Find months
// Map keys are years that posts have been authored in, value is array of months in that year
var months = new Map();
files.forEach(function(post) {
var date = handleOffset(post.data.time);
var year = date.year();
var month = date.month();
if (!months.get(year)) months.set(year, []);
if (!months.get(year).includes(month)) months.get(year).push(month);
});
months.forEach(function(year) {
year.sort();
});
// Find categories
var categories = [];
files.forEach(function(post) {
post.data.categories.forEach(function(category) {
if (!categories.includes(category)) categories.push(category);
});
});
categories.sort();
// Output main index
var mainIndexFile = templateFile.clone();
mainIndexFile.data.posts = files;
mainIndexFile.data.posts.sort(sortChronological);
mainIndexFile.data.indexType = 'main';
mainIndexFile.data.includedYears = years;
mainIndexFile.data.includedMonths = months;
mainIndexFile.data.includedCategories = categories;
this.push(mainIndexFile);
// Output years
years.forEach(function(year) {
var file = templateFile.clone();
file.data.posts = files.filter(function(post) {
return year === handleOffset(post.data.time).year();
});
file.data.posts.sort(sortChronological);
var yearStr = year.toString();
file.path = path.join(file.base, yearStr, file.relative);
file.data.indexType = 'year';
file.data.includedMonths = months.get(year);
file.data.year = year;
this.push(file);
}, this);
// Output months
months.forEach(function(monthsInYear, year) {
monthsInYear.forEach(function(month) {
var file = templateFile.clone();
file.data.posts = files.filter(function(post) {
var date = handleOffset(post.data.time);
var postYear = date.year();
var postMonth = date.month();
return year === postYear && month === postMonth;
});
file.data.posts.sort(sortChronological);
var yearStr = year.toString();
var monthStr = month + 1;
monthStr = monthStr < 10 ? '0' + monthStr.toString() : monthStr.toString();
file.path = path.join(file.base, yearStr, monthStr, file.relative);
file.data.indexType = 'month';
file.data.year = year;
file.data.month = month;
this.push(file);
}, this);
}, this);
// Output categories
categories.forEach(function(category) {
var file = templateFile.clone();
file.data.posts = files.filter(function(post) {
return post.data.categories.includes(category);
});
file.data.posts.sort(sortChronological);
file.path = path.join(file.base, 'category', category, file.relative);
file.data.indexType = 'category';
file.data.category = category;
this.push(file);
}, this);
callback();
});
return stream;
};