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 1/6] 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" }, From 4553c6bfe96f46773461f708c926e80d5155a621 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:40:13 +0100 Subject: [PATCH 2/6] test: insert rel-sitemap tag --- test/index.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/index.js b/test/index.js index 316788f..a65668e 100644 --- a/test/index.js +++ b/test/index.js @@ -68,3 +68,84 @@ describe('Sitemap generator', () => { }); }); }); + +describe('Rel-Sitemap', () => { + const hexo = new Hexo(); + const autoDiscovery = require('../lib/rel').bind(hexo); + hexo.config.sitemap = { + path: 'sitemap.xml', + rel: true + }; + + it('default', () => { + const content = ''; + const result = autoDiscovery(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(''); + }); + + it('prepend root', () => { + hexo.config.root = '/root/'; + const content = ''; + const result = autoDiscovery(content); + + const $ = cheerio.load(result); + $('link[rel="sitemap"]').attr('href').should.eql(hexo.config.root + hexo.config.sitemap.path); + + result.should.eql(''); + hexo.config.root = '/'; + }); + + it('disable autodiscovery', () => { + hexo.config.sitemap.autodiscovery = false; + const content = ''; + const result = autoDiscovery(content); + + const resultType = typeof result; + resultType.should.eql('undefined'); + hexo.config.sitemap.autodiscovery = true; + }); + + it('no duplicate tag', () => { + const content = '' + + ''; + const result = autoDiscovery(content); + + const resultType = typeof result; + resultType.should.eql('undefined'); + }); + + it('ignore empty head tag', () => { + const content = '' + + '' + + ''; + const result = autoDiscovery(content); + + const $ = cheerio.load(result); + $('link[rel="sitemap"]').length.should.eql(1); + + const expected = '' + + '' + + ''; + result.should.eql(expected); + }); + + it('apply to first non-empty head tag only', () => { + const content = '' + + '' + + ''; + const result = autoDiscovery(content); + + const $ = cheerio.load(result); + $('link[rel="sitemap"]').length.should.eql(1); + + const expected = '' + + '' + + ''; + result.should.eql(expected); + }); +}); From 0d8788ce1aa46090a419c4615d46047e00fe1ed1 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:40:41 +0100 Subject: [PATCH 3/6] docs: insert rel-sitemap tag --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 97ff4e5..6eafe1b 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,12 @@ You can configure this plugin in `_config.yml`. sitemap: path: sitemap.xml template: ./sitemap_template.xml + rel: true ``` - **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: `true`) ## Excluding Posts From 110eba699a1e905f0a3dc9ca9a062b0241f945a5 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:41:30 +0100 Subject: [PATCH 4/6] fix: config option --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7524966..76f54fd 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,6 @@ if (!extname(config.path)) { hexo.extend.generator.register('sitemap', require('./lib/generator')); -if (config.sitemap.rel === true) { +if (config.rel === true) { hexo.extend.filter.register('after_render:html', require('./lib/rel')); } From 2df5f9a35723caadf196c657462ec49b8e43e0a6 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:52:59 +0100 Subject: [PATCH 5/6] fix: config naming and syntax --- lib/rel.js | 2 +- test/index.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/rel.js b/lib/rel.js index ea5b4fa..9ed7085 100644 --- a/lib/rel.js +++ b/lib/rel.js @@ -7,7 +7,7 @@ function relSitemapInject(data) { if (!rel || data.match(/rel=['|"]?sitemap['|"]?/i)) return; - const relSitemap = ``; + const relSitemap = ``; return data.replace(/(?!<\/head>).+?<\/head>/, (str) => str.replace('', `${relSitemap}`)); } diff --git a/test/index.js b/test/index.js index a65668e..2c44b08 100644 --- a/test/index.js +++ b/test/index.js @@ -71,15 +71,15 @@ describe('Sitemap generator', () => { describe('Rel-Sitemap', () => { const hexo = new Hexo(); - const autoDiscovery = require('../lib/rel').bind(hexo); hexo.config.sitemap = { path: 'sitemap.xml', rel: true }; + const relSitemap = require('../lib/rel').bind(hexo); it('default', () => { const content = ''; - const result = autoDiscovery(content); + const result = relSitemap(content); const $ = cheerio.load(result); $('link[rel="sitemap"]').length.should.eql(1); @@ -91,7 +91,7 @@ describe('Rel-Sitemap', () => { it('prepend root', () => { hexo.config.root = '/root/'; const content = ''; - const result = autoDiscovery(content); + const result = relSitemap(content); const $ = cheerio.load(result); $('link[rel="sitemap"]').attr('href').should.eql(hexo.config.root + hexo.config.sitemap.path); @@ -100,20 +100,20 @@ describe('Rel-Sitemap', () => { hexo.config.root = '/'; }); - it('disable autodiscovery', () => { - hexo.config.sitemap.autodiscovery = false; + it('disable', () => { + hexo.config.sitemap.rel = false; const content = ''; - const result = autoDiscovery(content); + const result = relSitemap(content); const resultType = typeof result; resultType.should.eql('undefined'); - hexo.config.sitemap.autodiscovery = true; + hexo.config.sitemap.rel = true; }); it('no duplicate tag', () => { const content = '' + ''; - const result = autoDiscovery(content); + const result = relSitemap(content); const resultType = typeof result; resultType.should.eql('undefined'); @@ -123,7 +123,7 @@ describe('Rel-Sitemap', () => { const content = '' + '' + ''; - const result = autoDiscovery(content); + const result = relSitemap(content); const $ = cheerio.load(result); $('link[rel="sitemap"]').length.should.eql(1); @@ -138,7 +138,7 @@ describe('Rel-Sitemap', () => { const content = '' + '' + ''; - const result = autoDiscovery(content); + const result = relSitemap(content); const $ = cheerio.load(result); $('link[rel="sitemap"]').length.should.eql(1); From 39ef767dd9270ef5d792eeae4b970a118594765a Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 24 Sep 2019 11:26:24 +0100 Subject: [PATCH 6/6] fix: disable rel-sitemap by default --- README.md | 4 ++-- index.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6eafe1b..ed0dfc9 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,12 @@ You can configure this plugin in `_config.yml`. sitemap: path: sitemap.xml template: ./sitemap_template.xml - rel: true + 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: `true`) +- **rel** - Add [`rel-sitemap`](http://microformats.org/wiki/rel-sitemap) to the site's header. (Default: `false`) ## Excluding Posts diff --git a/index.js b/index.js index 76f54fd..087189a 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const { extname } = require('path'); const config = hexo.config.sitemap = Object.assign({ path: 'sitemap.xml', - rel: true + rel: false }, hexo.config.sitemap); if (!extname(config.path)) {