UNPKG

2.09 kBJavaScriptView Raw
1var util = hexo.util,
2 file = util.file,
3 extend = hexo.extend,
4 route = hexo.route,
5 xml = require('jstoxml');
6
7extend.generator.register(function(locals, render, callback){
8 var config = hexo.config;
9
10 var content = [
11 {title: '<![CDATA[' + config.title + ']]>'},
12 {
13 _name: 'link',
14 _attrs: {
15 href: config.url + '/atom.xml',
16 rel: 'self'
17 }
18 },
19 {
20 _name: 'link',
21 _attrs: {
22 href: config.url
23 }
24 },
25 {updated: new Date().toISOString()},
26 {id: config.url + '/'},
27 {author:
28 {
29 name: '<![CDATA[' + config.author + ']]>'
30 }
31 },
32 {
33 _name: 'generator',
34 _attrs: {
35 uri: 'http://zespia.tw/hexo'
36 },
37 _content: 'Hexo'
38 }
39 ];
40
41 if (config.email) content[5].author.email = '<![CDATA[' + config.email + ']]>';
42 if (config.subtitle) content.splice(1, 0, {subtitle: '<![CDATA[' + config.subtitle + ']]>'});
43
44 locals.posts.limit(20).each(function(item){
45 var entry = [
46 {
47 _name: 'title',
48 _attrs: {
49 type: 'html'
50 },
51 _content: '<![CDATA[' + item.title + ']]>'
52 },
53 {
54 _name: 'link',
55 _attrs: {
56 href: item.permalink
57 }
58 },
59 {id: item.permalink},
60 {published: item.date.toDate().toISOString()},
61 {updated: item.updated.toDate().toISOString()},
62 {
63 _name: 'content',
64 _attrs: {
65 type: 'html'
66 },
67 _content: '<![CDATA[' + item.content + ']]>'
68 },
69 ];
70
71 if (item.tags){
72 var tags = [];
73
74 item.tags.forEach(function(tag){
75 tags.push({
76 _name: 'category',
77 _attrs: {
78 scheme: tag.permalink,
79 term: tag.name
80 }
81 });
82 });
83
84 entry = [].concat(entry, tags);
85 }
86
87 content.push({entry: entry});
88 });
89
90 var result = xml.toXML({
91 _name: 'feed',
92 _attrs: {
93 xmlns: 'http://www.w3.org/2005/Atom'
94 },
95 _content: content
96 }, {header: true, indent: ' '});
97
98 route.set('atom.xml', result);
99 callback();
100});
\No newline at end of file