Skip to content

Commit

Permalink
Merge pull request #93 from curbengh/idn
Browse files Browse the repository at this point in the history
fix(IDN): uriencode root value
  • Loading branch information
curbengh authored Oct 18, 2019
2 parents a513323 + f3052d0 commit 4ea5b4c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
18 changes: 9 additions & 9 deletions atom.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<feed xmlns="https://www.w3.org/2005/Atom">
<title>{{ config.title }}</title>
{% if icon %}<icon>{{ icon }}</icon>{% endif %}
{% 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 %}
<link href="{{ url }}"/>
<link href="{{ url | uriencode }}"/>
<updated>{{ posts.first().updated.toISOString() }}</updated>
<id>{{ url }}</id>
<id>{{ url | uriencode }}</id>
{% if config.author %}
<author>
<name>{{ config.author }}</name>
{% if config.email %}<email>{{ config.email }}</email>{% endif %}
</author>
{% endif %}
<generator uri="http://hexo.io/">Hexo</generator>
<generator uri="https://hexo.io/">Hexo</generator>
{% for post in posts.toArray() %}
<entry>
<title>{{ post.title }}</title>
<link href="{{ url }}{{ post.path | uriencode }}"/>
<id>{{ url }}{{ post.path }}</id>
<link href="{{ post.permalink | uriencode }}"/>
<id>{{ post.permalink | uriencode }}</id>
<published>{{ post.date.toISOString() }}</published>
<updated>{{ post.updated.toISOString() }}</updated>
{% if config.feed.content and post.content %}
Expand Down Expand Up @@ -47,13 +47,13 @@
{% endif %}
</summary>
{% if post.image %}
<content src="{{ url }}{{ post.image | uriencode }}" type="image" />
<content src="{{ url + post.image | uriencode }}" type="image" />
{% endif %}
{% for category in post.categories.toArray() %}
<category term="{{ category.name }}" scheme="{{ url }}{{ category.path | uriencode }}"/>
<category term="{{ category.name }}" scheme="{{ url + category.path | uriencode }}"/>
{% endfor %}
{% for tag in post.tags.toArray() %}
<category term="{{ tag.name }}" scheme="{{ url }}{{ tag.path | uriencode }}"/>
<category term="{{ tag.name }}" scheme="{{ url + tag.path | uriencode }}"/>
{% endfor %}
</entry>
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const nunjucks = require('nunjucks');
const env = new nunjucks.Environment();
const { join } = require('path');
const { readFileSync } = require('fs');
const { gravatar } = require('hexo-util');
const { encodeURL, gravatar } = require('hexo-util');

env.addFilter('uriencode', str => {
return encodeURI(str);
return encodeURL(str);
});

env.addFilter('noControlChars', str => {
Expand Down
8 changes: 4 additions & 4 deletions rss2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
{% for post in posts.toArray() %}
<item>
<title>{{ post.title }}</title>
<link>{{ url }}{{ post.path | uriencode }}</link>
<guid>{{ url }}{{ post.path | uriencode }}</guid>
<link>{{ post.permalink | uriencode }}</link>
<guid>{{ post.permalink | uriencode }}</guid>
<pubDate>{{ post.date.toDate().toUTCString() }}</pubDate>
<description>
{% if post.description %}
Expand All @@ -38,12 +38,12 @@
{% endif %}
</description>
{% if post.image %}
<enclosure url="{{ url + post.image }}" type="image" />
<enclosure url="{{ url + post.image | uriencode }}" type="image" />
{% endif %}
{% if config.feed.content and post.content %}
<content:encoded><![CDATA[{{ post.content | noControlChars | safe }}]]></content:encoded>
{% endif %}
{% if post.comments %}<comments>{{ url }}{{ post.path | uriencode }}#disqus_thread</comments>{% endif %}
{% if post.comments %}<comments>{{ post.permalink | uriencode }}#disqus_thread</comments>{% endif %}
</item>
{% endfor %}
</channel>
Expand Down
15 changes: 8 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const env = new nunjucks.Environment();
const { join } = require('path');
const { readFileSync } = require('fs');
const cheerio = require('cheerio');
const { encodeURL } = require('hexo-util');

env.addFilter('uriencode', str => {
return encodeURI(str);
Expand Down Expand Up @@ -148,7 +149,6 @@ describe('Feed generator', () => {
const $ = cheerio.load(result.data);

$('feed>id').text().should.eql(valid);
$('feed>entry>link').attr('href').should.eql(valid);
};

checkURL('http://localhost/', '/', 'http://localhost/');
Expand All @@ -170,20 +170,21 @@ describe('Feed generator', () => {
path: 'atom.xml'
};

const checkURL = function(url, root, valid) {
const checkURL = function(url, root) {
hexo.config.url = url;
hexo.config.root = root;

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

$('feed>id').text().should.eql(valid);
$('feed>entry>link').attr('href').should.eql(valid);
if (url[url.length - 1] !== '/') url += '/';
const punyIDN = encodeURL(url);
$('feed>id').text().should.eql(punyIDN);
};
const IDN = 'http://gôg.com/';
checkURL(IDN, '/', IDN);

checkURL(IDN, 'blo g/', IDN);
checkURL('http://gôg.com/', '/');

checkURL('http://gôg.com/bár', '/bár/');
});

it('Root encoding', () => {
Expand Down

0 comments on commit 4ea5b4c

Please sign in to comment.