Skip to content

Commit

Permalink
fix: remove index.html from end position
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Sep 10, 2019
1 parent c5f7c55 commit aed9a3d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
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
}
}
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
/* global hexo */
'use strict';

<<<<<<< HEAD
const { extname } = require('path');
=======
const pathFn = require('path');
>>>>>>> fix: remove index.html from end position

const config = hexo.config.sitemap = Object.assign({
path: 'sitemap.xml'
}, hexo.config.sitemap);

<<<<<<< HEAD
if (!extname(config.path)) {
=======
if (!pathFn.extname(config.path)) {
>>>>>>> fix: remove index.html from end position
config.path += '.xml';
}

Expand Down
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

0 comments on commit aed9a3d

Please sign in to comment.