-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
148 lines (123 loc) · 3.67 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
var path = require('path'),
_ = require('lodash'),
url = require('url');
var baseUrl = '/api';
hexo.extend.generator.register('api', apiGenerator);
// Utilities
function listName(apiPage) {
if (apiPage === 1) {
return 'list.json';
}
else {
return 'list-' + apiPage + '.json';
}
}
function listPath(apiPage) {
return path.resolve(baseUrl, 'list', listName(apiPage));
}
// Name JSON file recent.json
function recentPath(apiPage) {
return path.resolve(baseUrl, 'list', 'recent.json');
}
function listUrl(apiPage) {
return url.resolve(hexo.config.url, listPath(apiPage));
}
function postPath(post) {
return path.resolve(baseUrl, 'posts/' + post._id + '.json');
}
function postUrl(post) {
return url.resolve(hexo.config.url, 'api/posts/' + post._id + '.json');
}
function startIdx(apiPage, postsPerPage) {
return (apiPage - 1) * postsPerPage;
}
function endIdx(apiPage, postsPerPage) {
return apiPage * postsPerPage;
}
function makePost(post, inList) {
var result = _.pick(post, 'title', 'permalink');
result.date = post.date._i;
if (inList) {
//post in list
result.url = postUrl(post);
}
else {
// Set up new variables
var postRaw = post.raw,
postBody;
// Split post at front matter seperator '---'
postRaw = postRaw.split('---');
// Set body post to second part of array, first part being only front matter
postBody = postRaw[1];
// Incase user has used the front matter trigger anywhere else in the post
// recombine the rest of postRaw with postBody, still ignoring the front matter
if (postRaw.length > 1) {
for (i = 2; i < postRaw.length; i++) {
// We have to append the seperator '---' before recombining since it will be
// removed in the split
postBody += '---' + postRaw[i]
}
}
//post detail
result.raw = postBody;
}
return result;
}
function generateList(allPosts) {
// list generation
// get all posts
var postsPerPage = hexo.config.api_posts_per_page || 10,
apiPage = 1, allRoutes = [],
curPosts;
while (startIdx(apiPage, postsPerPage) < allPosts.length) {
curPosts = _.filter(allPosts, function(post, idx) {
// pagination
return startIdx(apiPage, postsPerPage) <= idx && endIdx(apiPage, postsPerPage) > idx;
});
result = {
'prev': null,
'next': null
};
result.posts = _.map(curPosts, function(post) {
return makePost(post, true);
});
// check for prev page
if (apiPage > 1) {
result.prev = listUrl(apiPage - 1);
}
// check for next page
if (startIdx(apiPage + 1, postsPerPage) < allPosts.length) {
result.next = listUrl(apiPage + 1);
}
allRoutes.push({ path: listPath(apiPage), data: JSON.stringify(result) });
apiPage ++;
}
return allRoutes;
}
function generatePosts(allPosts) {
return _.collect(allPosts, function(post) {
var result = makePost(post, false);
return { path: postPath(post), data: JSON.stringify(result)};
});
}
function generateRecent(allPosts) {
var postsPerPage = hexo.config['api_posts_per_page'] || 10,
recentPosts = [];
// Loop thru the first 'postsPerPage' posts, in reverse order
for (var i = allPosts.length; i-- > (allPosts.length - postsPerPage); ) {
recentPosts.push(allPosts[i])
}
// Add posts into object
result = {};
result.posts = _.map(recentPosts, function(post) {
return makePost(post, true);
});
return { path: recentPath(), data: JSON.stringify(result) };
}
function apiGenerator(locals) {
allPosts = locals.posts.toArray();
return _.flatten([
generateRecent(allPosts), // Create API file for most recent posts
generateList(allPosts),
generatePosts(allPosts)]);
}