Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: insert rel-sitemap tag #71

Merged
merged 6 commits into from
Sep 27, 2019
Merged
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
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 @@ -68,3 +68,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);
});
});