Skip to content

Commit

Permalink
refactor: no unused argument, destructure path (#69)
Browse files Browse the repository at this point in the history
* refactor(test): no unused argument

* style(test): chai should

* refactor: destructure path module

* refactor: destructure fs module
  • Loading branch information
curbengh authored and tomap committed Sep 11, 2019
1 parent 6b74b3a commit c5f7c55
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* global hexo */
'use strict';

const pathFn = require('path');
const { extname } = require('path');

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

if (!pathFn.extname(config.path)) {
if (!extname(config.path)) {
config.path += '.xml';
}

Expand Down
8 changes: 4 additions & 4 deletions lib/template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const pathFn = require('path');
const fs = require('fs');
const { join } = require('path');
const { readFileSync } = require('fs');
let sitemapTmpl;

module.exports = function(config) {
Expand All @@ -17,8 +17,8 @@ module.exports = function(config) {
return encodeURI(str);
});

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

return sitemapTmpl;
};
10 changes: 5 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();
const Hexo = require('hexo');
const cheerio = require('cheerio');

Expand All @@ -22,7 +22,7 @@ describe('Sitemap generator', () => {
{source: 'bar', slug: 'bar', updated: 1e8 + 1},
{source: 'baz', slug: 'baz', updated: 1e8 - 1}
]).then(data => {
posts = Post.sort('-updated');
posts = data.sort((a, b) => b.updated - a.updated);
locals = hexo.locals.toObject();
});
});
Expand All @@ -34,14 +34,14 @@ describe('Sitemap generator', () => {
result.path.should.eql('sitemap.xml');
result.data.should.eql(sitemapTmpl.render({
config: hexo.config,
posts: posts.toArray()
posts: posts
}));

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

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

Expand Down

0 comments on commit c5f7c55

Please sign in to comment.