forked from rknightuk/hexo-generator-json-feed-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var util = require('hexo-util');
hexo.extend.generator.register('json-feed', hexo_generator_json_feed);
function hexo_generator_json_feed(site) {
var config, posts, rss, feedPath, siteAuthor;
generateTags = function(post) {
return post.categories.length ? post.categories.map(function (cat) {
return cat.name;
}).join(',') : post.tags.map(function (tag) {
return tag.name;
}).join(',')
}
config = hexo.config.hasOwnProperty('jsonFeed') ? hexo.config.jsonFeed : {};
feedPath = config.path ? config.path + '.json' : 'feed.json';
posts = site.posts.sort('-date').filter(function (post) {
return post.published;
});
if (config.limit) posts = posts.limit(config.limit);
siteAuthor = {
name: hexo.config.author,
url: hexo.config.url
}
posts = posts.map(function (post) {
return {
id: post.permalink,
url: post.permalink,
external_url: post.link,
title: post.title,
link: post.permalink,
summary: post.excerpt ? post.excerpt : '',
image: post.image,
banner_image: post.image,
content_html: post.content,
date_published: post.date.toDate().toISOString(),
date_modified: post.updated.toDate().toISOString(),
author: post.author ? {
"name": post.author
} : siteAuthor,
tags: generateTags(post)
};
});
feedContent = {
version: 'https://jsonfeed.org/version/1',
title: hexo.config.title,
description: hexo.config.description,
home_page_url: hexo.config.url,
feed_url: hexo.config.url + '/' + feedPath,
language: hexo.config.language,
author: siteAuthor,
items: posts
};
return {
path: feedPath,
data: JSON.stringify(feedContent)
};
}