Skip to content

Commit

Permalink
Merge pull request #71 from curbengh/rel-sitemap
Browse files Browse the repository at this point in the history
feat: insert rel-sitemap tag
  • Loading branch information
curbengh authored Sep 27, 2019
2 parents a61334c + 39ef767 commit 6e54068
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ You can configure this plugin in `_config.yml`.
sitemap:
path: sitemap.xml
template: ./sitemap_template.xml
rel: false
```
- **path** - Sitemap path. (Default: sitemap.xml)
- **template** - Custom template path. This file will be used to generate sitemap.xml (See [default template](/sitemap.xml))
- **rel** - Add [`rel-sitemap`](http://microformats.org/wiki/rel-sitemap) to the site's header. (Default: `false`)

## Excluding Posts

Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
const { extname } = require('path');

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

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

hexo.extend.generator.register('sitemap', require('./lib/generator'));

if (config.rel === true) {
hexo.extend.filter.register('after_render:html', require('./lib/rel'));
}
15 changes: 15 additions & 0 deletions lib/rel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const { url_for } = require('hexo-util');

function relSitemapInject(data) {
const { path, rel } = this.config.sitemap;

if (!rel || data.match(/rel=['|"]?sitemap['|"]?/i)) return;

const relSitemap = `<link rel="sitemap" type="application/xml" title="Sitemap" href="${url_for.call(this, path)}">`;

return data.replace(/<head>(?!<\/head>).+?<\/head>/, (str) => str.replace('</head>', `${relSitemap}</head>`));
}

module.exports = relSitemapInject;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"license": "MIT",
"dependencies": {
"hexo-util": "^1.3.0",
"micromatch": "^4.0.2",
"nunjucks": "^3.1.6"
},
Expand Down
81 changes: 81 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,84 @@ describe('Sitemap generator', () => {
});
});
});

describe('Rel-Sitemap', () => {
const hexo = new Hexo();
hexo.config.sitemap = {
path: 'sitemap.xml',
rel: true
};
const relSitemap = require('../lib/rel').bind(hexo);

it('default', () => {
const content = '<head><link></head>';
const result = relSitemap(content);

const $ = cheerio.load(result);
$('link[rel="sitemap"]').length.should.eql(1);
$('link[rel="sitemap"]').attr('href').should.eql(hexo.config.root + hexo.config.sitemap.path);

result.should.eql('<head><link><link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml"></head>');
});

it('prepend root', () => {
hexo.config.root = '/root/';
const content = '<head><link></head>';
const result = relSitemap(content);

const $ = cheerio.load(result);
$('link[rel="sitemap"]').attr('href').should.eql(hexo.config.root + hexo.config.sitemap.path);

result.should.eql('<head><link><link rel="sitemap" type="application/xml" title="Sitemap" href="/root/sitemap.xml"></head>');
hexo.config.root = '/';
});

it('disable', () => {
hexo.config.sitemap.rel = false;
const content = '<head><link></head>';
const result = relSitemap(content);

const resultType = typeof result;
resultType.should.eql('undefined');
hexo.config.sitemap.rel = true;
});

it('no duplicate tag', () => {
const content = '<head><link>'
+ '<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml"></head>';
const result = relSitemap(content);

const resultType = typeof result;
resultType.should.eql('undefined');
});

it('ignore empty head tag', () => {
const content = '<head></head>'
+ '<head><link></head>'
+ '<head></head>';
const result = relSitemap(content);

const $ = cheerio.load(result);
$('link[rel="sitemap"]').length.should.eql(1);

const expected = '<head></head>'
+ '<head><link><link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml"></head>'
+ '<head></head>';
result.should.eql(expected);
});

it('apply to first non-empty head tag only', () => {
const content = '<head></head>'
+ '<head><link></head>'
+ '<head><link></head>';
const result = relSitemap(content);

const $ = cheerio.load(result);
$('link[rel="sitemap"]').length.should.eql(1);

const expected = '<head></head>'
+ '<head><link><link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml"></head>'
+ '<head><link></head>';
result.should.eql(expected);
});
});

0 comments on commit 6e54068

Please sign in to comment.