From 04283538ef4c5672072b2ee7d4fbce14576c890f Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Mon, 1 May 2023 19:09:11 +0800 Subject: [PATCH 1/3] fix(rel): fix bug causes by sitemap.txt --- lib/rel.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/rel.js b/lib/rel.js index 0ca10dc..719fd29 100644 --- a/lib/rel.js +++ b/lib/rel.js @@ -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 = ``; + const relSitemap = ``; return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', `${relSitemap}`)); } From 2f68d257ae844cdac449c5c5152ce95a1257a491 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Mon, 1 May 2023 19:09:26 +0800 Subject: [PATCH 2/3] test(rel): add test code. --- test/index.js | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/test/index.js b/test/index.js index 8df1f5d..363f25d 100644 --- a/test/index.js +++ b/test/index.js @@ -215,10 +215,11 @@ 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 = ''; @@ -226,7 +227,7 @@ describe('Rel-Sitemap', () => { 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(''); }); @@ -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(''); hexo.config.root = '/'; @@ -291,6 +292,42 @@ describe('Rel-Sitemap', () => { + ''; result.should.eql(expected); }); + + it('specify relPath', () => { + hexo.config.sitemap.relPath = 'sitemap_specify.xml'; + const content = ''; + 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(''); + 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 = ''; + 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(``); + }); + + it('no xml', () => { + hexo.config.sitemap.path = ['sitemap2.txt', 'sitemap.txt']; + const content = ''; + const result = relSitemap(content); + + const resultType = typeof result; + resultType.should.eql('undefined'); + }); }); describe('IDN', () => { From bedf54e04283bab02ab565b43fe1ad831bace474 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sat, 6 May 2023 19:54:50 +0800 Subject: [PATCH 3/3] doc: update `rel` related doc. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e938f26..b9eb689 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ You can configure this plugin in `_config.yml`. ``` yaml sitemap: - path: + path: - sitemap.xml - sitemap.txt template: ./sitemap_template.xml @@ -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