Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can configure this plugin in `_config.yml`.

``` yaml
sitemap:
path:
path:
- sitemap.xml
- sitemap.txt
template: ./sitemap_template.xml
Expand All @@ -35,9 +35,10 @@ sitemap:
- **path** - Sitemap path. (Default: sitemap.xml)
- **template** - Custom template path. This file will be used to generate sitemap.xml (See [default xml template](/sitemap.xml))
- **template_txt** - Custom template path. This file will be used to generate sitemap.txt (See [default txt template](/sitemap.txt))
- **rel** - Add [`rel-sitemap`](http://microformats.org/wiki/rel-sitemap) to the site's header. (Default: `false`)
- **tags** - Add site's tags
- **categories** - Add site's categories
- **rel** - Add [`rel-sitemap`](http://microformats.org/wiki/rel-sitemap) to the site's header. (Default: `false`)
- **relPath** - (optional) Specify [`rel-sitemap`](http://microformats.org/wiki/rel-sitemap) path. Unset it to ues the first `.xml` file in `path`.

## Exclude Posts/Pages

Expand Down
8 changes: 5 additions & 3 deletions lib/rel.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

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

function relSitemapInject(data) {
const { path, rel } = this.config.sitemap;
const { path, rel, relPath } = this.config.sitemap;
const rPath = relPath ? relPath : path.filter(p => { return extname(p) === '.xml'; })[0];

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

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

return data.replace(/<head>(?!<\/head>).+?<\/head>/s, str => str.replace('</head>', `${relSitemap}</head>`));
}
Expand Down
43 changes: 40 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,19 @@ it('No posts', async () => {
describe('Rel-Sitemap', () => {
const hexo = new Hexo();
hexo.config.sitemap = {
path: 'sitemap.xml',
path: ['sitemap.xml', 'sitemap.txt'],
rel: true
};
const relSitemap = require('../lib/rel').bind(hexo);
const relPath = hexo.config.sitemap.path.filter(p => { return extname(p) === '.xml'; })[0];

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);
$('link[rel="sitemap"]').attr('href').should.eql(hexo.config.root + relPath);

result.should.eql('<head><link><link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml"></head>');
});
Expand All @@ -237,7 +238,7 @@ describe('Rel-Sitemap', () => {
const result = relSitemap(content);

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

result.should.eql('<head><link><link rel="sitemap" type="application/xml" title="Sitemap" href="/root/sitemap.xml"></head>');
hexo.config.root = '/';
Expand Down Expand Up @@ -291,6 +292,42 @@ describe('Rel-Sitemap', () => {
+ '<head><link></head>';
result.should.eql(expected);
});

it('specify relPath', () => {
hexo.config.sitemap.relPath = 'sitemap_specify.xml';
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.relPath);

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

it('multiple xml', () => {
hexo.config.sitemap.path = ['sitemap2.xml', 'sitemap.xml', 'sitemap.txt'];
const relPath = hexo.config.sitemap.path.filter(p => { return extname(p) === '.xml'; })[0];

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 + relPath);

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

it('no xml', () => {
hexo.config.sitemap.path = ['sitemap2.txt', 'sitemap.txt'];
const content = '<head><link></head>';
const result = relSitemap(content);

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

describe('IDN', () => {
Expand Down