Skip to content

Commit

Permalink
Merge pull request #100 from curbengh/atom-rss2
Browse files Browse the repository at this point in the history
feat: support output in both atom and rss2
  • Loading branch information
curbengh committed Nov 9, 2019
2 parents d168026 + 7f41f0f commit 273eb73
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 75 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,22 @@ feed:
autodiscovery: true
```

- **type** - Feed type. (atom/rss2)
- **path** - Feed path. (Default: atom.xml/rss2.xml)
- **type** - Feed type. `atom` or `rss2`. Specify `['atom', 'rss2']` to output both types. (Default: `atom`)
* Example:
``` yaml
feed:
# Generate atom feed
type: atom

# Generate both atom and rss2 feeds
type:
- atom
- rss2
path:
- atom.xml
- rss2.xml
```
- **path** - Feed path. When both types are specified, path must follow the order of type value. (Default: atom.xml/rss2.xml)
- **limit** - Maximum number of posts in the feed (Use `0` or `false` to show all posts)
- **hub** - URL of the PubSubHubbub hubs (Leave it empty if you don't use it)
- **content** - (optional) set to 'true' to include the contents of the entire post in the feed.
Expand Down
52 changes: 39 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,52 @@ const config = hexo.config.feed = Object.assign({
autodiscovery: true
}, hexo.config.feed);

const type = config.type.toLowerCase();
let type = config.type;
let path = config.path;
const feedFn = require('./lib/generator');

// Check feed type
if (type !== 'atom' && type !== 'rss2') {
config.type = 'atom';
} else {
config.type = type;
if (typeof type === 'string') type = [type];

if (!type || !Array.isArray(type)) {
type = ['atom'];
}

if (Array.isArray(type) && type.length > 2) {
type = type.slice(0, 2);
}

type = type.map((str, i) => {
str = str.toLowerCase();
if (str !== 'atom' && str !== 'rss2') {
if (i === 0) str = 'atom';
else str = 'rss2';
}
return str;
});

if (!path || typeof path === 'string' || !Array.isArray(path)) {
path = type.map(str => str.concat('.xml'));
}

// Set default feed path
if (!config.path) {
config.path = config.type + '.xml';
if (Array.isArray(path) && path.length > 2) {
path = path.slice(0, 2);
}

// Add extension name if don't have
if (!extname(config.path)) {
config.path += '.xml';
path = path.map(str => {
if (!extname(str)) return str.concat('.xml');
return str;
});

config.type = type;
config.path = path;

for (const feedType of type) {
hexo.extend.generator.register(feedType, locals => {
return feedFn.call(hexo, locals, feedType, path[type.indexOf(feedType)]);
});
}

hexo.extend.generator.register('feed', require('./lib/generator'));
if (typeof config.autodiscovery === 'undefined') config.autodiscovery = true;

if (config.autodiscovery === true) {
hexo.extend.filter.register('after_render:html', require('./lib/autodiscovery'));
Expand Down
12 changes: 8 additions & 4 deletions lib/autodiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const { url_for } = require('hexo-util');
function autodiscoveryInject(data) {
const { config } = this;
const { feed } = config;
const type = feed.type.replace(/2$/, '');
const type = feed.type;
const path = feed.path;
let autodiscoveryTag = '';

if (!feed.autodiscovery
|| data.match(/type=['|"]?application\/(atom|rss)\+xml['|"]?/i)) return;
if (data.match(/type=['|"]?application\/(atom|rss)\+xml['|"]?/i)) return;

const autodiscoveryTag = `<link rel="alternate" href="${url_for.call(this, feed.path)}" title="${config.title}" type="application/${type}+xml">`;
type.forEach((feedType, i) => {
autodiscoveryTag += `<link rel="alternate" href="${url_for.call(this, path[i])}" `
+ `title="${config.title}" type="application/${feedType.replace(/2$/, '')}+xml">\n`;
});

return data.replace(/<head>(?!<\/head>).+?<\/head>/s, (str) => str.replace('</head>', `${autodiscoveryTag}</head>`));
}
Expand Down
8 changes: 4 additions & 4 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env);
const rss2TmplSrc = join(__dirname, '../rss2.xml');
const rss2Tmpl = nunjucks.compile(readFileSync(rss2TmplSrc, 'utf8'), env);

module.exports = function(locals) {
module.exports = function(locals, type, path) {
const config = this.config;
const feedConfig = config.feed;
const template = feedConfig.type === 'rss2' ? rss2Tmpl : atomTmpl;
const template = type === 'atom' ? atomTmpl : rss2Tmpl;

let posts = locals.posts.sort(feedConfig.order_by || '-date');
posts = posts.filter(post => {
Expand All @@ -43,11 +43,11 @@ module.exports = function(locals) {
url,
icon,
posts,
feed_url: config.root + feedConfig.path
feed_url: config.root + path
});

return {
path: feedConfig.path,
path,
data: xml
};
};
Loading

0 comments on commit 273eb73

Please sign in to comment.