Skip to content

Commit 4ae4f92

Browse files
committed
update site config, #1
1 parent 75a1b64 commit 4ae4f92

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ Generate restful json data for Hexo plugins.
66

77
## Install
88

9-
```
9+
```bash
1010
npm install hexo-generator-restful --save
1111
```
1212

1313
## Config
1414

1515
以下为默认配置,属性值为 `false` 表示不生成。
1616

17-
```
17+
```yml
1818
restful:
19+
# site 可配置为数组选择性生成某些属性
20+
# site: ['title', 'subtitle', 'description', 'author', 'since', email', 'favicon', 'avatar']
1921
site: true # hexo.config mix theme.config
2022
posts_size: 10 # 文章列表分页,0 表示不分页
2123
posts_props: # 文章列表项的需要生成的属性
@@ -111,7 +113,7 @@ GET /api/tags/:TagName.json
111113

112114
### Get All Categories
113115

114-
获取所有文章标签,如果文章无标签则不生成
116+
获取所有文章分类,如果文章无分类则不生成
115117

116118
###### Request
117119

lib/generator.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

33
var pagination = require('hexo-pagination');
4+
var _pick = require('lodash.pick');
45

5-
module.exports = function(cfg, site) {
6+
module.exports = function (cfg, site) {
67

7-
var restful = cfg.hasOwnProperty('restful') ? cfg.restful : {
8+
var restful = cfg.hasOwnProperty('restful') ? cfg.restful :
9+
{
810
site: true,
911
posts_size: 10,
1012
posts_props: {
@@ -25,19 +27,19 @@ module.exports = function(cfg, site) {
2527
post: true
2628
},
2729

28-
posts = site.posts.sort('-date').filter(function(post) {
30+
posts = site.posts.sort('-date').filter(function (post) {
2931
return post.published;
3032
}),
3133

32-
posts_props = (function() {
34+
posts_props = (function () {
3335
var props = restful.posts_props;
3436

35-
return function(name, val) {
37+
return function (name, val) {
3638
return props[name] ? (typeof val === 'function' ? val() : val) : null;
3739
}
3840
})(),
3941

40-
postMap = function(post) {
42+
postMap = function (post) {
4143
return {
4244
title: posts_props('title', post.title),
4345
slug: posts_props('slug', post.slug),
@@ -49,16 +51,16 @@ module.exports = function(cfg, site) {
4951
keywords: posts_props('keywords', cfg.keywords),
5052
content: posts_props('content', post.content),
5153
raw: posts_props('raw', post.raw),
52-
categories: posts_props('categories', function() {
53-
return post.categories.map(function(cat) {
54+
categories: posts_props('categories', function () {
55+
return post.categories.map(function (cat) {
5456
return {
5557
name: cat.name,
5658
path: 'api/categories/' + cat.name + '.json'
5759
};
5860
});
5961
}),
60-
tags: posts_props('tags', function() {
61-
return post.tags.map(function(tag) {
62+
tags: posts_props('tags', function () {
63+
return post.tags.map(function (tag) {
6264
return {
6365
name: tag.name,
6466
path: 'api/tags/' + tag.name + '.json'
@@ -68,8 +70,8 @@ module.exports = function(cfg, site) {
6870
};
6971
},
7072

71-
cateReduce = function(cates, name) {
72-
return cates.reduce(function(result, item) {
73+
cateReduce = function (cates, name) {
74+
return cates.reduce(function (result, item) {
7375
if (!item.length) return result;
7476

7577
return result.concat(pagination(item.path, posts, {
@@ -84,14 +86,14 @@ module.exports = function(cfg, site) {
8486
}, []);
8587
},
8688

87-
catesMap = function(item) {
89+
catesMap = function (item) {
8890
return {
8991
name: item.data.name,
9092
path: item.data.path
9193
};
9294
},
9395

94-
cateMap = function(item) {
96+
cateMap = function (item) {
9597
var itemData = item.data;
9698
return {
9799
path: itemData.path,
@@ -108,7 +110,7 @@ module.exports = function(cfg, site) {
108110
if (restful.site) {
109111
apiData.push({
110112
path: 'api/site.json',
111-
data: JSON.stringify(cfg)
113+
data: JSON.stringify(restful.site instanceof Array ? _pick(cfg, restful.site) : cfg)
112114
});
113115
}
114116

@@ -179,7 +181,7 @@ module.exports = function(cfg, site) {
179181
}
180182

181183
if (restful.post) {
182-
apiData = apiData.concat(posts.map(function(post) {
184+
apiData = apiData.concat(posts.map(function (post) {
183185
var path = 'api/articles/' + post.slug + '.json';
184186
return {
185187
path: path,
@@ -193,13 +195,13 @@ module.exports = function(cfg, site) {
193195
excerpt: post.excerpt,
194196
keywords: cfg.keyword,
195197
content: post.content,
196-
categories: post.categories.map(function(cat) {
198+
categories: post.categories.map(function (cat) {
197199
return {
198200
name: cat.name,
199201
path: 'api/categories/' + cat.name + '.json'
200202
};
201203
}),
202-
tags: post.tags.map(function(tag) {
204+
tags: post.tags.map(function (tag) {
203205
return {
204206
name: tag.name,
205207
path: 'api/tags/' + tag.name + '.json'

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hexo-generator-restful",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Generate restful json data for Hexo plugins.",
55
"main": "index.js",
66
"scripts": {
@@ -19,6 +19,7 @@
1919
"author": "yusen",
2020
"license": "MIT",
2121
"dependencies": {
22-
"hexo-pagination": "0.0.2"
22+
"hexo-pagination": "0.0.2",
23+
"lodash.pick": "^4.4.0"
2324
}
2425
}

0 commit comments

Comments
 (0)