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