Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoviclefevre committed May 18, 2015
1 parent c4940ab commit 6fb6ce5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ Generate SEO-friendly sitemap.

Inspired by XML Sitemap in Yoast Wordpress SEO Plugin (https://yoast.com).

## Install

``` bash
$ npm install hexo-generator-seo-friendly-sitemap --save
```

## Options

You can configure this plugin in `_config.yml`.

``` yaml
sitemap:
path: sitemap.xml
```

## License

MIT © [Ludovic LEFEVRE](http://www.ludoviclefevre.fr)
Expand Down
12 changes: 10 additions & 2 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

var path = require('path'),
ejs = require('ejs'),
_ = require('lodash'),
Promise = require('bluebird'),
fs = Promise.promisifyAll(require('fs')),

googleFriendlySitemap = function (locals) {
'use strict';

var config = this.config,
viewPath = path.join(__dirname, '../views/'),
Expand All @@ -31,6 +32,10 @@ var path = require('path'),

filePaths = [],

isPluginEnabled = function (config) {
return (config.sitemap && config.sitemap.path);
},

addPosts = function () {
if (locals.posts.length === 0) {
return;
Expand Down Expand Up @@ -159,7 +164,7 @@ var path = require('path'),
filePaths.push(
{
template: 'index-sitemap.ejs',
filename: 'index-sitemap.xml',
filename: config.sitemap.path,
items: indexSitemapItems
});
},
Expand All @@ -177,6 +182,9 @@ var path = require('path'),
);
};

if (!isPluginEnabled(config)) {
return Promise.resolve([]);
}
addPosts();
addPages();
addCategories();
Expand Down
2 changes: 1 addition & 1 deletion test/allSitemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ it('should generate all sitemap files if posts, pages, categories and tags are d
checkAssertions = function (result) {
result.should.be.a('array');

var indexSitemap = _.find(result, {path: 'index-sitemap.xml'}),
var indexSitemap = _.find(result, {path: 'sitemap.xml'}),
postSitemap = _.find(result, {path: 'post-sitemap.xml'}),
pageSitemap = _.find(result, {path: 'page-sitemap.xml'}),
categorySitemap = _.find(result, {path: 'category-sitemap.xml'}),
Expand Down
20 changes: 20 additions & 0 deletions test/disabledPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var Hexo = require('hexo'),
path = require('path');

describe('Hexo', function () {
it('should not generate sitemap files if plugin is not enabled in _config.yml.', function () {
var hexo = new Hexo(__dirname, {silent: true}),
generator = require(path.join(__dirname, '../lib/generator')).bind(hexo),
locals,
checkAssertions = function (result) {
return result.should.be.empty;
};

locals = hexo.locals.toObject();

return generator(locals)
.then(checkAssertions);
});
});

0 comments on commit 6fb6ce5

Please sign in to comment.