Skip to content

Commit 2ae45d4

Browse files
authoredApr 2, 2017
Merge pull request #6 from changkun/master
Optimize [excerpt] and add [cover/covers/pages] option
2 parents 8ff45c4 + f4cd8c2 commit 2ae45d4

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed
 

‎README.md

+18
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ restful:
2828
comments: true
2929
path: true
3030
excerpt: false
31+
cover: true
3132
content: false
3233
keywords: false
3334
categories: true
3435
tags: true
3536
categories: true # 分类数据
3637
tags: true # 标签数据
3738
post: true # 文章数据
39+
pages: false # 额外的 Hexo 页面数据, 如 About
3840
```
3941
4042
## Document
@@ -152,3 +154,19 @@ GET /api/articles/:Slug.json
152154
###### Response
153155

154156
[/api/articles/javascript-advanced-functions.json](http://www.imys.net/api/articles/javascript-advanced-functions.json)
157+
158+
### Get Implecit Pages
159+
160+
获取来自主题的 Hexo 隐式页面内容,如 About 等。因隐式页面(除 About 等导航栏入口页外)一般在 Hexo 不提供直接访问入口,调用此 API 的开发者需要了解其完整路径,此接口默认关闭。
161+
162+
例如:
163+
164+
###### Request
165+
166+
```
167+
GET /api/pages/about.json
168+
```
169+
170+
###### Response
171+
172+
格式类似于于 Get Post By Slug。

‎lib/generator.js

+48-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
var pagination = require('hexo-pagination');
44
var _pick = require('lodash.pick');
55

6+
function filterHTMLTags(str) {
7+
return str ? str
8+
.replace(/\<(?!img|br).*?\>/g, "")
9+
.replace(/\r?\n|\r/g, '')
10+
.replace(/<img(.*)>/g, ' [Figure] ') : null
11+
}
12+
function fetchCovers(str) {
13+
var temp,
14+
imgURLs = [],
15+
rex = /<img[^>]+src="?([^"\s]+)"(.*)>/g;
16+
while ( temp = rex.exec( str ) ) {
17+
imgURLs.push( temp[1] );
18+
}
19+
return imgURLs.length > 0 ? imgURLs : null;
20+
}
21+
function fetchCover(str) {
22+
var covers = fetchCovers(str)
23+
return covers ? covers[0] : null;
24+
}
25+
626
module.exports = function (cfg, site) {
727

828
var restful = cfg.hasOwnProperty('restful') ? cfg.restful :
@@ -15,6 +35,7 @@ module.exports = function (cfg, site) {
1535
date: true,
1636
updated: true,
1737
comments: true,
38+
cover: true,
1839
path: true,
1940
raw: false,
2041
excerpt: false,
@@ -24,7 +45,8 @@ module.exports = function (cfg, site) {
2445
},
2546
categories: true,
2647
tags: true,
27-
post: true
48+
post: true,
49+
pages: false,
2850
},
2951

3052
posts = site.posts.sort('-date').filter(function (post) {
@@ -47,8 +69,9 @@ module.exports = function (cfg, site) {
4769
updated: posts_props('updated', post.updated),
4870
comments: posts_props('comments', post.comments),
4971
path: posts_props('path', 'api/articles/' + post.slug + '.json'),
50-
excerpt: posts_props('excerpt', post.excerpt),
72+
excerpt: posts_props('excerpt', filterHTMLTags(post.excerpt)),
5173
keywords: posts_props('keywords', cfg.keywords),
74+
cover: posts_props('cover', fetchCover(post.content)),
5275
content: posts_props('content', post.content),
5376
raw: posts_props('raw', post.raw),
5477
categories: posts_props('categories', function () {
@@ -193,7 +216,8 @@ module.exports = function (cfg, site) {
193216
updated: post.updated,
194217
comments: post.comments,
195218
path: path,
196-
excerpt: post.excerpt,
219+
excerpt: filterHTMLTags(post.excerpt),
220+
covers: fetchCovers(post.content),
197221
keywords: cfg.keyword,
198222
content: post.content,
199223
categories: post.categories.map(function (cat) {
@@ -213,5 +237,26 @@ module.exports = function (cfg, site) {
213237
}));
214238
}
215239

240+
if (restful.pages) {
241+
apiData = apiData.concat(site.pages.data.map(function (page) {
242+
var safe_title = page.title.replace(/[^a-z0-9]/gi, '-').toLowerCase()
243+
var path = 'api/pages/' + safe_title + '.json';
Has conversations. Original line has conversations.
244+
245+
return {
246+
path: path,
247+
data: JSON.stringify({
248+
title: page.title,
249+
date: page.date,
250+
updated: page.updated,
251+
comments: page.comments,
252+
path: path,
253+
covers: fetchCovers(page.content),
254+
excerpt: filterHTMLTags(page.excerpt),
255+
content: page.content
256+
})
257+
};
258+
}));
259+
}
260+
216261
return apiData;
217262
};

0 commit comments

Comments
 (0)
Please sign in to comment.