From 306a8dbf0ce2ef7ff708ad9d4a6587316b3ca58a Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:39:35 +0100 Subject: [PATCH] feat: insert rel-sitemap tag - http://microformats.org/wiki/rel-sitemap --- index.js | 7 ++++++- lib/rel.js | 15 +++++++++++++++ package.json | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 lib/rel.js diff --git a/index.js b/index.js index a6f3c12..7524966 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,8 @@ 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)) { @@ -12,3 +13,7 @@ if (!extname(config.path)) { } hexo.extend.generator.register('sitemap', require('./lib/generator')); + +if (config.sitemap.rel === true) { + hexo.extend.filter.register('after_render:html', require('./lib/rel')); +} diff --git a/lib/rel.js b/lib/rel.js new file mode 100644 index 0000000..ea5b4fa --- /dev/null +++ b/lib/rel.js @@ -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 = ``; + + return data.replace(/(?!<\/head>).+?<\/head>/, (str) => str.replace('', `${relSitemap}`)); +} + +module.exports = relSitemapInject; diff --git a/package.json b/package.json index 92ec42f..6b13d8b 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ ], "license": "MIT", "dependencies": { + "hexo-util": "^1.3.0", "micromatch": "^4.0.2", "nunjucks": "^3.1.6" },