Skip to content

Commit

Permalink
Merge 66e45e1 into a4fb0d8
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jun 29, 2019
2 parents a4fb0d8 + 66e45e1 commit 906773a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions index.js
@@ -1,9 +1,9 @@
/* global hexo */
'use strict';

var pathFn = require('path');
const pathFn = require('path');

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

Expand Down
14 changes: 7 additions & 7 deletions lib/generator.js
@@ -1,11 +1,11 @@
'use strict';

var minimatch = require('minimatch');
var template = require('./template');
const minimatch = require('minimatch');
const template = require('./template');

module.exports = function(locals) {
var config = this.config;
var skipRenderList = [
const config = this.config;
let skipRenderList = [
'**/*.js',
'**/*.css'
];
Expand All @@ -16,15 +16,15 @@ module.exports = function(locals) {
skipRenderList.push(config.skip_render);
}

var posts = [].concat(locals.posts.toArray(), locals.pages.toArray())
const posts = [].concat(locals.posts.toArray(), locals.pages.toArray())
.filter(function(post) {
return post.sitemap !== false && !isMatch(post.source, skipRenderList);
})
.sort(function(a, b) {
return b.updated - a.updated;
});

var xml = template(config).render({
const xml = template(config).render({
config: config,
posts: posts
});
Expand All @@ -40,7 +40,7 @@ function isMatch(path, patterns) {
if (!Array.isArray(patterns)) patterns = [patterns];
if (!patterns.length) return false;

for (var i = 0, len = patterns.length; i < len; i++) {
for (let i = 0, len = patterns.length; i < len; i++) {
if (minimatch(path, patterns[i])) return true;
}

Expand Down
14 changes: 7 additions & 7 deletions lib/template.js
@@ -1,23 +1,23 @@
'use strict';

var pathFn = require('path');
var fs = require('fs');
var sitemapTmpl;
const pathFn = require('path');
const fs = require('fs');
let sitemapTmpl;

module.exports = function(config) {
if (sitemapTmpl) return sitemapTmpl;

var nunjucks = require('nunjucks');
var env = new nunjucks.Environment(null, {
const nunjucks = require('nunjucks');
const env = new nunjucks.Environment(null, {
autoescape: false,
watch: false
});

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

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

return sitemapTmpl;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"author": "Tommy Chen <tommy351@gmail.com> (http://zespia.tw)",
"maintainers": [
"Abner Chou <hi@abnerchou.me> (http://abnerchou.me)"
],
],
"license": "MIT",
"dependencies": {
"minimatch": "^3.0.0",
Expand Down
52 changes: 26 additions & 26 deletions test/index.js
@@ -1,69 +1,69 @@
'use strict';

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

describe('Sitemap generator', function() {
var hexo = new Hexo(__dirname, {silent: true});
describe('Sitemap generator', () => {
const hexo = new Hexo(__dirname, {silent: true});
hexo.config.sitemap = {
path: 'sitemap.xml'
};
var Post = hexo.model('Post');
var generator = require('../lib/generator').bind(hexo);
var sitemapTmpl = require('../lib/template')(hexo.config);
var posts,
locals;
const Post = hexo.model('Post');
const generator = require('../lib/generator').bind(hexo);
const sitemapTmpl = require('../lib/template')(hexo.config);
let posts = {};
let locals = {};

before(function() {
return hexo.init().then(function() {
before(() => {
return hexo.init().then(() => {
return Post.insert([
{source: 'foo', slug: 'foo', updated: 1e8},
{source: 'bar', slug: 'bar', updated: 1e8 + 1},
{source: 'baz', slug: 'baz', updated: 1e8 - 1}
]).then(function(data) {
]).then(data => {
posts = Post.sort('-updated');
locals = hexo.locals.toObject();
});
});
});

it('default', function() {
var result = generator(locals);
it('default', () => {
const result = generator(locals);

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

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

$('urlset').find('url').each(function(i) {
$(this).children('loc').text().should.eql(posts.eq(i).permalink);
$(this).children('lastmod').text().should.eql(posts.eq(i).updated.toISOString());
$('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());
});
});

describe('skip_render', function() {
it('array', function() {
describe('skip_render', () => {
it('array', () => {
hexo.config.skip_render = ['foo'];

var result = generator(locals);
const result = generator(locals);
result.data.should.not.contain('foo');
});

it('string', function() {
it('string', () => {
hexo.config.skip_render = 'bar';

var result = generator(locals);
const result = generator(locals);
result.data.should.not.contain('bar');
});

it('off', function() {
it('off', () => {
hexo.config.skip_render = null;

var result = generator(locals);
const result = generator(locals);
result.should.be.ok;
});
});
Expand Down

0 comments on commit 906773a

Please sign in to comment.