Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : add feed icon to rss2.xml #102

Merged
merged 10 commits into from
Nov 5, 2019
8 changes: 7 additions & 1 deletion atom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="https://www.w3.org/2005/Atom">
<title>{{ config.title }}</title>
{% if icon %}<icon>{{ icon }}</icon>{% endif %}
{% if icon %}
<image>
<url>{{ icon }}</url>
<title>{{ config.title }}</title>
<link>{{ url }}</link>
</image>
{% endif %}
sehajyang marked this conversation as resolved.
Show resolved Hide resolved
{% if config.subtitle %}<subtitle>{{ config.subtitle }}</subtitle>{% endif %}
<link href="{{ feed_url | uriencode }}" rel="self"/>
{% if config.feed.hub %}<link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
Expand Down
12 changes: 6 additions & 6 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const nunjucks = require('nunjucks');
const env = new nunjucks.Environment();
const { join } = require('path');
const { readFileSync } = require('fs');
const { encodeURL, gravatar } = require('hexo-util');
const { encodeURL, gravatar, full_url_for } = require('hexo-util');

env.addFilter('uriencode', str => {
return encodeURL(str);
Expand Down Expand Up @@ -35,14 +35,14 @@ module.exports = function(locals) {
if (url[url.length - 1] !== '/') url += '/';

let icon = '';
if (feedConfig.icon) icon = url + encodeURI(feedConfig.icon);
if (feedConfig.icon) icon = full_url_for.call(this, feedConfig.icon);
else if (config.email) icon = gravatar(config.email);

const xml = template.render({
config: config,
url: url,
icon: icon,
posts: posts,
config,
url,
icon,
posts,
feed_url: config.root + feedConfig.path
});

Expand Down
7 changes: 7 additions & 0 deletions rss2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
<channel>
<title>{{ config.title }}</title>
<link>{{ url }}</link>
{% if icon %}
<image>
<url>{{ icon }}</url>
<title>{{ config.title }}</title>
<link>{{ url }}</link>
</image>
sehajyang marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
<atom:link href="{{ feed_url | uriencode }}" rel="self" type="application/rss+xml"/>
{% if config.feed.hub %}<atom:link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
<description>{{ config.description }}</description>
Expand Down
31 changes: 30 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const env = new nunjucks.Environment();
const { join } = require('path');
const { readFileSync } = require('fs');
const cheerio = require('cheerio');
const { encodeURL } = require('hexo-util');
const { encodeURL, full_url_for } = require('hexo-util');

env.addFilter('uriencode', str => {
return encodeURI(str);
Expand Down Expand Up @@ -235,6 +235,35 @@ describe('Feed generator', () => {
};
checkURL('http://localhost/', '/', 'item:nth-of-type(3)>enclosure');
});

it('Icon (atom)', () => {
hexo.config.url = 'http://example.com';
hexo.config.root = '/';

hexo.config.feed = {
type: 'atom',
path: 'atom.xml',
icon: 'icon.svg'
};

const result = generator(locals);
const $ = cheerio.load(result.data);

$('feed>icon').text().should.eql(full_url_for.call(hexo, hexo.config.feed.icon));
});

it('Icon (atom) - no icon', () => {
hexo.config.feed = {
type: 'atom',
path: 'atom.xml',
icon: undefined
};

const result = generator(locals);
const $ = cheerio.load(result.data);

$('feed>icon').length.should.eql(0);
});
});

describe('Autodiscovery', () => {
Expand Down