Skip to content

Commit

Permalink
feat: insert rel-sitemap tag
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Sep 24, 2019
1 parent c5f7c55 commit 306a8db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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: true
}, hexo.config.sitemap);

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

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

if (config.sitemap.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

0 comments on commit 306a8db

Please sign in to comment.