Skip to content

Commit

Permalink
Merge 66c708f into 57c2106
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Nov 4, 2019
2 parents 57c2106 + 66c708f commit 3237ecf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/template.js
Expand Up @@ -3,6 +3,7 @@
const { join } = require('path');
const { readFileSync } = require('fs');
let sitemapTmpl;
const { encodeURL } = require('hexo-util');

module.exports = function(config) {
if (sitemapTmpl) return sitemapTmpl;
Expand All @@ -14,7 +15,7 @@ module.exports = function(config) {
});

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

const sitemapSrc = config.sitemap.template || join(__dirname, '../sitemap.xml');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -29,7 +29,7 @@
],
"license": "MIT",
"dependencies": {
"hexo-util": "^1.3.0",
"hexo-util": "^1.4.0",
"micromatch": "^4.0.2",
"nunjucks": "^3.1.6"
},
Expand Down
60 changes: 60 additions & 0 deletions test/index.js
Expand Up @@ -3,6 +3,7 @@
require('chai').should();
const Hexo = require('hexo');
const cheerio = require('cheerio');
const { encodeURL } = require('hexo-util');

describe('Sitemap generator', () => {
const hexo = new Hexo(__dirname, {silent: true});
Expand Down Expand Up @@ -158,3 +159,62 @@ describe('Rel-Sitemap', () => {
result.should.eql(expected);
});
});

describe('IDN', () => {
it('Default', () => {
const hexo = new Hexo(__dirname, {silent: true});
hexo.config.sitemap = {
path: 'sitemap.xml'
};
const Post = hexo.model('Post');
const generator = require('../lib/generator').bind(hexo);

hexo.config.url = 'http://fôo.com/bár';
const parsedUrl = encodeURL(hexo.config.url);

return hexo.init().then(() => {
return Post.insert({
source: 'foo', slug: 'foo', updated: 1e8
});
}).then(data => {
const locals = hexo.locals.toObject();

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

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

return Post.removeById(data._id);
});
});

it('Encoded', () => {
const hexo = new Hexo(__dirname, {silent: true});
hexo.config.sitemap = {
path: 'sitemap.xml'
};
const Post = hexo.model('Post');
const generator = require('../lib/generator').bind(hexo);

hexo.config.url = 'http://foo.com/b%C3%A1r';

return hexo.init().then(() => {
return Post.insert({
source: 'foo', slug: 'foo', updated: 1e8
});
}).then(data => {
const locals = hexo.locals.toObject();

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

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

return Post.removeById(data._id);
});
});
});

0 comments on commit 3237ecf

Please sign in to comment.