Skip to content

Commit

Permalink
Merge pull request #102 from sehajyang/patch-1
Browse files Browse the repository at this point in the history
feat: add feed icon in rss2.xml
  • Loading branch information
curbengh authored Nov 5, 2019
2 parents d169992 + 417898d commit d168026
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
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>
{% 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
60 changes: 59 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,64 @@ 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);
});

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

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

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

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

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

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

$('rss>channel>image').length.should.eql(0);
});
});

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

0 comments on commit d168026

Please sign in to comment.