From 4ae4f9254170740f2f59043952c6516ae92cfe88 Mon Sep 17 00:00:00 2001 From: yscoder <634206017@qq.com> Date: Thu, 22 Dec 2016 21:26:17 +0800 Subject: [PATCH] update site config, #1 --- README.md | 8 +++++--- lib/generator.js | 38 ++++++++++++++++++++------------------ package.json | 5 +++-- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 215eb1e..2930195 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Generate restful json data for Hexo plugins. ## Install -``` +```bash npm install hexo-generator-restful --save ``` @@ -14,8 +14,10 @@ npm install hexo-generator-restful --save 以下为默认配置,属性值为 `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: # 文章列表项的需要生成的属性 @@ -111,7 +113,7 @@ GET /api/tags/:TagName.json ### Get All Categories -获取所有文章标签,如果文章无标签则不生成。 +获取所有文章分类,如果文章无分类则不生成。 ###### Request diff --git a/lib/generator.js b/lib/generator.js index 6833cea..2754b0d 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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: { @@ -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), @@ -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' @@ -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, { @@ -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, @@ -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) }); } @@ -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, @@ -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' diff --git a/package.json b/package.json index 3a3176a..0521d22 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -19,6 +19,7 @@ "author": "yusen", "license": "MIT", "dependencies": { - "hexo-pagination": "0.0.2" + "hexo-pagination": "0.0.2", + "lodash.pick": "^4.4.0" } }