Skip to content

Commit b8a4029

Browse files
committed
update file path
1 parent 8f8fd41 commit b8a4029

File tree

2 files changed

+214
-210
lines changed

2 files changed

+214
-210
lines changed

index.js

+2-210
Original file line numberDiff line numberDiff line change
@@ -1,215 +1,7 @@
11
'use strict';
22

3-
var pagination = require('hexo-pagination');
4-
3+
var generator = require('./lib/generator');
54

65
hexo.extend.generator.register('restful', function(site) {
7-
8-
var cfg = Object.assign(hexo.config, hexo.theme.config),
9-
10-
restful = cfg.hasOwnProperty('restful') ? cfg.restful : {
11-
site: true,
12-
posts_size: 10,
13-
posts_props: {
14-
title: true,
15-
slug: true,
16-
date: true,
17-
updated: true,
18-
comments: true,
19-
path: true,
20-
excerpt: false,
21-
content: false,
22-
categories: true,
23-
tags: true
24-
},
25-
categories: true,
26-
tags: true,
27-
post: true
28-
},
29-
30-
posts = site.posts.sort('-date').filter(function(post) {
31-
return post.published;
32-
}),
33-
34-
posts_props = (function() {
35-
var props = restful.posts_props;
36-
37-
return function(name, val) {
38-
return props[name] ? (typeof val === 'function' ? val() : val) : null;
39-
}
40-
})(),
41-
42-
postMap = function(post) {
43-
return {
44-
title: posts_props('title', post.title),
45-
slug: posts_props('slug', post.slug),
46-
date: posts_props('date', post.date),
47-
updated: posts_props('updated', post.updated),
48-
comments: posts_props('comments', post.comments),
49-
path: posts_props('path', 'api/articles/' + post.slug + '.json'),
50-
excerpt: posts_props('excerpt', post.excerpt),
51-
keywords: posts_props('keywords', cfg.keywords),
52-
content: posts_props('content', post.content),
53-
categories: posts_props('categories', function() {
54-
return post.categories.map(function(cat) {
55-
return {
56-
name: cat.name,
57-
path: 'api/categories/' + cat.name + '.json'
58-
};
59-
});
60-
}),
61-
tags: posts_props('tags', function() {
62-
return post.tags.map(function(tag) {
63-
return {
64-
name: tag.name,
65-
path: 'api/tags/' + tag.name + '.json'
66-
};
67-
});
68-
})
69-
};
70-
},
71-
72-
cateReduce = function(cates, name) {
73-
return cates.reduce(function(result, item) {
74-
if (!item.length) return result;
75-
76-
return result.concat(pagination(item.path, posts, {
77-
perPage: 0,
78-
data: {
79-
name: item.name,
80-
path: 'api/' + name + '/' + item.name + '.json',
81-
postlist: item.posts.map(postMap)
82-
}
83-
84-
}));
85-
}, []);
86-
},
87-
88-
catesMap = function(item) {
89-
return {
90-
name: item.data.name,
91-
path: item.data.path
92-
};
93-
},
94-
95-
cateMap = function(item) {
96-
var itemData = item.data;
97-
return {
98-
path: itemData.path,
99-
data: JSON.stringify({
100-
name: itemData.name,
101-
postlist: itemData.postlist
102-
})
103-
};
104-
},
105-
106-
apiData = [];
107-
108-
109-
if (restful.site) {
110-
apiData.push({
111-
path: 'api/site.json',
112-
data: JSON.stringify(cfg)
113-
});
114-
}
115-
116-
if (restful.categories) {
117-
118-
var cates = cateReduce(site.categories, 'categories');
119-
120-
if (!!cates.length) {
121-
apiData.push({
122-
path: 'api/categories.json',
123-
data: JSON.stringify(cates.map(catesMap))
124-
});
125-
126-
apiData = apiData.concat(cates.map(cateMap));
127-
}
128-
129-
}
130-
131-
if (restful.tags) {
132-
var tags = cateReduce(site.tags, 'tags');
133-
134-
if (tags.length) {
135-
apiData.push({
136-
path: 'api/tags.json',
137-
data: JSON.stringify(tags.map(catesMap))
138-
});
139-
140-
apiData = apiData.concat(tags.map(cateMap));
141-
}
142-
143-
}
144-
145-
var postlist = posts.map(postMap);
146-
147-
if (restful.posts_size > 0) {
148-
149-
var page_posts = [],
150-
i = 0,
151-
len = postlist.length,
152-
ps = restful.posts_size,
153-
pc = Math.ceil(len / ps);
154-
155-
for (; i < len; i += ps) {
156-
page_posts.push({
157-
path: 'api/posts/' + Math.ceil((i + 1) / ps) + '.json',
158-
data: JSON.stringify({
159-
total: len,
160-
pageSize: ps,
161-
pageCount: pc,
162-
data: postlist.slice(i, i + ps)
163-
})
164-
});
165-
}
166-
167-
apiData.push({
168-
path: 'api/posts.json',
169-
data: page_posts[0].data
170-
});
171-
172-
apiData = apiData.concat(page_posts);
173-
174-
} else {
175-
176-
apiData.push({
177-
path: 'api/posts.json',
178-
data: JSON.stringify(postlist)
179-
});
180-
}
181-
182-
if (restful.post) {
183-
apiData = apiData.concat(posts.map(function(post) {
184-
var path = 'api/articles/' + post.slug + '.json';
185-
return {
186-
path: path,
187-
data: JSON.stringify({
188-
title: post.title,
189-
slug: post.slug,
190-
date: post.date,
191-
updated: post.updated,
192-
comments: post.comments,
193-
path: path,
194-
excerpt: post.excerpt,
195-
keywords: cfg.keyword,
196-
content: post.content,
197-
categories: post.categories.map(function(cat) {
198-
return {
199-
name: cat.name,
200-
path: 'api/categories/' + cat.name + '.json'
201-
};
202-
}),
203-
tags: post.tags.map(function(tag) {
204-
return {
205-
name: tag.name,
206-
path: 'api/tags/' + tag.name + '.json'
207-
};
208-
})
209-
})
210-
};
211-
}));
212-
}
213-
214-
return apiData;
6+
return generator(Object.assign(hexo.config, hexo.theme.config), site);
2157
});

0 commit comments

Comments
 (0)