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: use canonical url for pages #59

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "hexo",
"root": true
}
"root": true,
"parserOptions": {
"ecmaVersion": 2018
}
}
6 changes: 5 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ module.exports = function(locals) {
})
.sort((a, b) => {
return b.updated - a.updated;
});
})
.map((post) => ({
...post,
permalink: post.permalink.replace(/index\.html$/, '')
}));

const xml = template(config).render({
config: config,
Expand Down
36 changes: 31 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ describe('Sitemap generator', () => {
path: 'sitemap.xml'
};
const Post = hexo.model('Post');
const Page = hexo.model('Page');
const generator = require('../lib/generator').bind(hexo);
const sitemapTmpl = require('../lib/template')(hexo.config);
let posts = {};
let pages = {};
let locals = {};

before(() => {
Expand All @@ -21,27 +23,51 @@ describe('Sitemap generator', () => {
{source: 'foo', slug: 'foo', updated: 1e8},
{source: 'bar', slug: 'bar', updated: 1e8 + 1},
{source: 'baz', slug: 'baz', updated: 1e8 - 1}
]).then(data => {
posts = data.sort((a, b) => b.updated - a.updated);
]).then(post => {
posts = post.sort((a, b) => b.updated - a.updated);
return Page.insert([
{source: 'bio/index.md', path: 'bio/', updated: 1e8 - 3},
{source: 'about/index.md', path: 'about/', updated: 1e8 - 4}
]);
}).then(page => {
pages = page.sort((a, b) => b.updated - a.updated);
locals = hexo.locals.toObject();
});
});
});

// Check the whole sitemap.xml
it('default', () => {
const result = generator(locals);

result.path.should.eql('sitemap.xml');
result.data.should.eql(sitemapTmpl.render({
config: hexo.config,
posts: posts
posts: posts.concat(pages)
}));
});

// Check each element in sitemap.xml
it('Each element', () => {
const result = generator(locals);

const $ = cheerio.load(result.data);
const allPosts = posts.concat(pages);

$('url').each((index, element) => {
$(element).children('loc').text().should.eql(allPosts[index].permalink);
$(element).children('lastmod').text().should.eql(allPosts[index].updated.toISOString());
});
});

// urls should not ends with 'index.html'
it('Canonical url', () => {
const result = generator(locals);

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

$('url').each((index, element) => {
$(element).children('loc').text().should.eql(posts[index].permalink);
$(element).children('lastmod').text().should.eql(posts[index].updated.toISOString());
$(element).children('loc').text().endsWith('index.html').should.be.false;
});
});

Expand Down