UNPKG

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