Skip to content

Commit

Permalink
update site config, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yscoder committed Dec 22, 2016
1 parent 75a1b64 commit 4ae4f92
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ Generate restful json data for Hexo plugins.

## Install

```
```bash
npm install hexo-generator-restful --save
```

## Config

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

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

### Get All Categories

获取所有文章标签,如果文章无标签则不生成
获取所有文章分类,如果文章无分类则不生成

###### Request

Expand Down
38 changes: 20 additions & 18 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

var pagination = require('hexo-pagination');
var _pick = require('lodash.pick');

module.exports = function(cfg, site) {
module.exports = function (cfg, site) {

var restful = cfg.hasOwnProperty('restful') ? cfg.restful : {
var restful = cfg.hasOwnProperty('restful') ? cfg.restful :
{
site: true,
posts_size: 10,
posts_props: {
Expand All @@ -25,19 +27,19 @@ module.exports = function(cfg, site) {
post: true
},

posts = site.posts.sort('-date').filter(function(post) {
posts = site.posts.sort('-date').filter(function (post) {
return post.published;
}),

posts_props = (function() {
posts_props = (function () {
var props = restful.posts_props;

return function(name, val) {
return function (name, val) {
return props[name] ? (typeof val === 'function' ? val() : val) : null;
}
})(),

postMap = function(post) {
postMap = function (post) {
return {
title: posts_props('title', post.title),
slug: posts_props('slug', post.slug),
Expand All @@ -49,16 +51,16 @@ module.exports = function(cfg, site) {
keywords: posts_props('keywords', cfg.keywords),
content: posts_props('content', post.content),
raw: posts_props('raw', post.raw),
categories: posts_props('categories', function() {
return post.categories.map(function(cat) {
categories: posts_props('categories', function () {
return post.categories.map(function (cat) {
return {
name: cat.name,
path: 'api/categories/' + cat.name + '.json'
};
});
}),
tags: posts_props('tags', function() {
return post.tags.map(function(tag) {
tags: posts_props('tags', function () {
return post.tags.map(function (tag) {
return {
name: tag.name,
path: 'api/tags/' + tag.name + '.json'
Expand All @@ -68,8 +70,8 @@ module.exports = function(cfg, site) {
};
},

cateReduce = function(cates, name) {
return cates.reduce(function(result, item) {
cateReduce = function (cates, name) {
return cates.reduce(function (result, item) {
if (!item.length) return result;

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

catesMap = function(item) {
catesMap = function (item) {
return {
name: item.data.name,
path: item.data.path
};
},

cateMap = function(item) {
cateMap = function (item) {
var itemData = item.data;
return {
path: itemData.path,
Expand All @@ -108,7 +110,7 @@ module.exports = function(cfg, site) {
if (restful.site) {
apiData.push({
path: 'api/site.json',
data: JSON.stringify(cfg)
data: JSON.stringify(restful.site instanceof Array ? _pick(cfg, restful.site) : cfg)
});
}

Expand Down Expand Up @@ -179,7 +181,7 @@ module.exports = function(cfg, site) {
}

if (restful.post) {
apiData = apiData.concat(posts.map(function(post) {
apiData = apiData.concat(posts.map(function (post) {
var path = 'api/articles/' + post.slug + '.json';
return {
path: path,
Expand All @@ -193,13 +195,13 @@ module.exports = function(cfg, site) {
excerpt: post.excerpt,
keywords: cfg.keyword,
content: post.content,
categories: post.categories.map(function(cat) {
categories: post.categories.map(function (cat) {
return {
name: cat.name,
path: 'api/categories/' + cat.name + '.json'
};
}),
tags: post.tags.map(function(tag) {
tags: post.tags.map(function (tag) {
return {
name: tag.name,
path: 'api/tags/' + tag.name + '.json'
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-generator-restful",
"version": "0.0.3",
"version": "0.1.0",
"description": "Generate restful json data for Hexo plugins.",
"main": "index.js",
"scripts": {
Expand All @@ -19,6 +19,7 @@
"author": "yusen",
"license": "MIT",
"dependencies": {
"hexo-pagination": "0.0.2"
"hexo-pagination": "0.0.2",
"lodash.pick": "^4.4.0"
}
}

0 comments on commit 4ae4f92

Please sign in to comment.