diff --git a/.eslintrc b/.eslintrc index 91288aa..5aedf6d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,7 @@ { "extends": "hexo", - "root": true -} \ No newline at end of file + "root": true, + "parserOptions": { + "ecmaVersion": 2018 + } +} diff --git a/index.js b/index.js index a6f3c12..e5a641f 100644 --- a/index.js +++ b/index.js @@ -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'; } diff --git a/lib/generator.js b/lib/generator.js index f7a1d24..75a3c36 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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, diff --git a/test/index.js b/test/index.js index 316788f..91da7de 100644 --- a/test/index.js +++ b/test/index.js @@ -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(() => { @@ -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; }); });