1
1
'use strict' ;
2
2
3
3
var pagination = require ( 'hexo-pagination' ) ;
4
+ var _pick = require ( 'lodash.pick' ) ;
4
5
5
- module . exports = function ( cfg , site ) {
6
+ module . exports = function ( cfg , site ) {
6
7
7
- var restful = cfg . hasOwnProperty ( 'restful' ) ? cfg . restful : {
8
+ var restful = cfg . hasOwnProperty ( 'restful' ) ? cfg . restful :
9
+ {
8
10
site : true ,
9
11
posts_size : 10 ,
10
12
posts_props : {
@@ -25,19 +27,19 @@ module.exports = function(cfg, site) {
25
27
post : true
26
28
} ,
27
29
28
- posts = site . posts . sort ( '-date' ) . filter ( function ( post ) {
30
+ posts = site . posts . sort ( '-date' ) . filter ( function ( post ) {
29
31
return post . published ;
30
32
} ) ,
31
33
32
- posts_props = ( function ( ) {
34
+ posts_props = ( function ( ) {
33
35
var props = restful . posts_props ;
34
36
35
- return function ( name , val ) {
37
+ return function ( name , val ) {
36
38
return props [ name ] ? ( typeof val === 'function' ? val ( ) : val ) : null ;
37
39
}
38
40
} ) ( ) ,
39
41
40
- postMap = function ( post ) {
42
+ postMap = function ( post ) {
41
43
return {
42
44
title : posts_props ( 'title' , post . title ) ,
43
45
slug : posts_props ( 'slug' , post . slug ) ,
@@ -49,16 +51,16 @@ module.exports = function(cfg, site) {
49
51
keywords : posts_props ( 'keywords' , cfg . keywords ) ,
50
52
content : posts_props ( 'content' , post . content ) ,
51
53
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 ) {
54
56
return {
55
57
name : cat . name ,
56
58
path : 'api/categories/' + cat . name + '.json'
57
59
} ;
58
60
} ) ;
59
61
} ) ,
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 ) {
62
64
return {
63
65
name : tag . name ,
64
66
path : 'api/tags/' + tag . name + '.json'
@@ -68,8 +70,8 @@ module.exports = function(cfg, site) {
68
70
} ;
69
71
} ,
70
72
71
- cateReduce = function ( cates , name ) {
72
- return cates . reduce ( function ( result , item ) {
73
+ cateReduce = function ( cates , name ) {
74
+ return cates . reduce ( function ( result , item ) {
73
75
if ( ! item . length ) return result ;
74
76
75
77
return result . concat ( pagination ( item . path , posts , {
@@ -84,14 +86,14 @@ module.exports = function(cfg, site) {
84
86
} , [ ] ) ;
85
87
} ,
86
88
87
- catesMap = function ( item ) {
89
+ catesMap = function ( item ) {
88
90
return {
89
91
name : item . data . name ,
90
92
path : item . data . path
91
93
} ;
92
94
} ,
93
95
94
- cateMap = function ( item ) {
96
+ cateMap = function ( item ) {
95
97
var itemData = item . data ;
96
98
return {
97
99
path : itemData . path ,
@@ -108,7 +110,7 @@ module.exports = function(cfg, site) {
108
110
if ( restful . site ) {
109
111
apiData . push ( {
110
112
path : 'api/site.json' ,
111
- data : JSON . stringify ( cfg )
113
+ data : JSON . stringify ( restful . site instanceof Array ? _pick ( cfg , restful . site ) : cfg )
112
114
} ) ;
113
115
}
114
116
@@ -179,7 +181,7 @@ module.exports = function(cfg, site) {
179
181
}
180
182
181
183
if ( restful . post ) {
182
- apiData = apiData . concat ( posts . map ( function ( post ) {
184
+ apiData = apiData . concat ( posts . map ( function ( post ) {
183
185
var path = 'api/articles/' + post . slug + '.json' ;
184
186
return {
185
187
path : path ,
@@ -193,13 +195,13 @@ module.exports = function(cfg, site) {
193
195
excerpt : post . excerpt ,
194
196
keywords : cfg . keyword ,
195
197
content : post . content ,
196
- categories : post . categories . map ( function ( cat ) {
198
+ categories : post . categories . map ( function ( cat ) {
197
199
return {
198
200
name : cat . name ,
199
201
path : 'api/categories/' + cat . name + '.json'
200
202
} ;
201
203
} ) ,
202
- tags : post . tags . map ( function ( tag ) {
204
+ tags : post . tags . map ( function ( tag ) {
203
205
return {
204
206
name : tag . name ,
205
207
path : 'api/tags/' + tag . name + '.json'
0 commit comments