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

WIP fix: do not encode IDN domain #66

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const micromatch = require('micromatch');
const template = require('./template');
const url = require('url');

module.exports = function(locals) {
const config = this.config;
Expand All @@ -22,7 +23,15 @@ module.exports = function(locals) {
})
.sort(function(a, b) {
return b.updated - a.updated;
});
})
.map(post => ({
...post,
permalink: url.format({
protocol: url.parse(post.permalink).protocol,
hostname: url.parse(post.permalink).hostname,
pathname: encodeURI(url.parse(post.permalink).pathname)
})
}));

const xml = template(config).render({
config: config,
Expand Down
4 changes: 0 additions & 4 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ module.exports = function(config) {
watch: false
});

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

const sitemapSrc = config.sitemap.template || pathFn.join(__dirname, '../sitemap.xml');
sitemapTmpl = nunjucks.compile(fs.readFileSync(sitemapSrc, 'utf8'), env);

Expand Down
2 changes: 1 addition & 1 deletion sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in posts %}
<url>
<loc>{{ post.permalink | uriencode }}</loc>
<loc>{{ post.permalink }}</loc>
{% if post.updated %}
<lastmod>{{ post.updated.toISOString() }}</lastmod>
{% elif post.date %}
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const should = require('chai').should(); // eslint-disable-line
const Hexo = require('hexo');
const cheerio = require('cheerio');
const urlFn = require('url');

describe('Sitemap generator', () => {
const hexo = new Hexo(__dirname, {silent: true});
Expand Down Expand Up @@ -67,4 +68,20 @@ describe('Sitemap generator', () => {
result.should.be.ok;
});
});

it('IDN handling', () => {
hexo.config.url = 'http://fôo.com/bár';
Copy link
Contributor Author

@curbengh curbengh Aug 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this option can be applied, but the following result still show "http://yoursite.com/".

This PR does work in a test blog, but somehow not in a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's related to describe() it(). But I don't know how to proceed.

Copy link
Contributor Author

@curbengh curbengh Aug 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also related to how urlObject is referenced/dereferenced.

const parsedUrl = urlFn.format({
protocol: urlFn.parse(hexo.config.url).protocol,
hostname: urlFn.parse(hexo.config.url).hostname,
pathname: encodeURI(urlFn.parse(hexo.config.url).pathname)
});

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

$('url').each((index, element) => {
$(element).children('loc').text().startsWith(parsedUrl).should.be.true;
});
});
});