From b03094d717d4d0b34ef1d9c0333999127ddc9225 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 26 Aug 2019 16:44:11 +0930 Subject: [PATCH 1/6] feat(permalink): add canonical_url option to remove index.html from url --- lib/models/category.js | 5 ++++- lib/models/page.js | 5 ++++- lib/models/post.js | 1 + lib/models/tag.js | 5 ++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/models/category.js b/lib/models/category.js index a025b5316a..7b7c15bf3d 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -38,7 +38,10 @@ module.exports = ctx => { }); Category.virtual('permalink').get(function() { - return `${ctx.config.url}/${this.path}`; + const { config } = ctx; + let partial_url = this.path; + if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + return `${ctx.config.url}/${partial_url}`; }); Category.virtual('posts').get(function() { diff --git a/lib/models/page.js b/lib/models/page.js index f946abb25d..2168961d8f 100644 --- a/lib/models/page.js +++ b/lib/models/page.js @@ -32,7 +32,10 @@ module.exports = ctx => { }); Page.virtual('permalink').get(function() { - return `${ctx.config.url}/${this.path}`; + const { config } = ctx; + let partial_url = this.path; + if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + return `${ctx.config.url}/${partial_url}`; }); Page.virtual('full_source').get(function() { diff --git a/lib/models/post.js b/lib/models/post.js index 27fc275a0d..dbcdaaaa4d 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -54,6 +54,7 @@ module.exports = ctx => { const { config } = ctx; let partial_url = self.url_for(this.path); if (config.relative_link) partial_url = `/${partial_url}`; + if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); return config.url + partial_url.replace(config.root, '/'); }); diff --git a/lib/models/tag.js b/lib/models/tag.js index 22491f1fd9..9924ea5a54 100644 --- a/lib/models/tag.js +++ b/lib/models/tag.js @@ -29,7 +29,10 @@ module.exports = ctx => { }); Tag.virtual('permalink').get(function() { - return `${ctx.config.url}/${this.path}`; + const { config } = ctx; + let partial_url = this.path; + if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + return `${ctx.config.url}/${partial_url}`; }); Tag.virtual('posts').get(function() { From 56617655cf00a9849fefba61711440b0f074bd5f Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 26 Aug 2019 16:48:27 +0930 Subject: [PATCH 2/6] test(permalink): url should not ends with index.html when canonical_url --- test/scripts/models/category.js | 10 ++++++++++ test/scripts/models/page.js | 11 +++++++++++ test/scripts/models/post.js | 11 +++++++++++ test/scripts/models/tag.js | 10 ++++++++++ 4 files changed, 42 insertions(+) diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index 80dbb7f634..602ca43dd7 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -104,6 +104,16 @@ describe('Category', () => { return Category.removeById(data._id); })); + it('permalink - canonical_url', () => { + hexo.config.canonical_url = true; + return Category.insert({ + name: 'foo' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + return Category.removeById(data._id); + }); + }); + it('posts - virtual', () => Post.insert([ {source: 'foo.md', slug: 'foo'}, {source: 'bar.md', slug: 'bar'}, diff --git a/test/scripts/models/page.js b/test/scripts/models/page.js index a4efe34781..3657fd7325 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -60,6 +60,17 @@ describe('Page', () => { return Page.removeById(data._id); })); + it('permalink - canonical_url', () => { + hexo.config.canonical_url = true; + return Page.insert({ + source: 'foo.md', + path: 'bar/index.html' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + return Page.removeById(data._id); + }); + }); + it('full_source - virtual', () => Page.insert({ source: 'foo', path: 'bar' diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index 0b05b83014..c71fdb067f 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -121,6 +121,17 @@ describe('Post', () => { }); }); + it('permalink - canonical_url', () => { + hexo.config.canonical_url = true; + return Post.insert({ + source: 'foo.md', + slug: 'bar' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + return Post.removeById(data._id); + }); + }); + it('full_source - virtual', () => Post.insert({ source: 'foo.md', slug: 'bar' diff --git a/test/scripts/models/tag.js b/test/scripts/models/tag.js index 691a4085fb..6aced68b4a 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -89,6 +89,16 @@ describe('Tag', () => { return Tag.removeById(data._id); })); + it('permalink - canonical_url', () => { + hexo.config.canonical_url = true; + return Tag.insert({ + name: 'foo' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + return Tag.removeById(data._id); + }); + }); + it('posts - virtual', () => Post.insert([ {source: 'foo.md', slug: 'foo'}, {source: 'bar.md', slug: 'bar'}, From 1e9c405e27c051d05e5e84da0e6c3577f735973f Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 26 Aug 2019 17:02:16 +0930 Subject: [PATCH 3/6] feat(canonical_url): default to false --- lib/hexo/default_config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 710e764586..672842d8c1 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -13,6 +13,7 @@ module.exports = { root: '/', permalink: ':year/:month/:day/:title/', permalink_defaults: {}, + canonical_url: false, // Directory source_dir: 'source', public_dir: 'public', From 3de151d53854f0121be6cd92f8419a6599a3dddd Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 30 Aug 2019 10:41:43 +0100 Subject: [PATCH 4/6] fix(permalink): use config.trailing_url.trailing_index --- lib/hexo/default_config.js | 4 +++- lib/models/category.js | 2 +- lib/models/page.js | 2 +- lib/models/post.js | 2 +- lib/models/tag.js | 2 +- test/scripts/models/category.js | 4 ++-- test/scripts/models/page.js | 2 +- test/scripts/models/post.js | 2 +- test/scripts/models/tag.js | 2 +- 9 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 672842d8c1..4d785be0d8 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -13,7 +13,9 @@ module.exports = { root: '/', permalink: ':year/:month/:day/:title/', permalink_defaults: {}, - canonical_url: false, + trailing_url: { + trailing_index: true + }, // Directory source_dir: 'source', public_dir: 'public', diff --git a/lib/models/category.js b/lib/models/category.js index 7b7c15bf3d..6c29342e01 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -40,7 +40,7 @@ module.exports = ctx => { Category.virtual('permalink').get(function() { const { config } = ctx; let partial_url = this.path; - if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return `${ctx.config.url}/${partial_url}`; }); diff --git a/lib/models/page.js b/lib/models/page.js index 2168961d8f..d30fbbfa4b 100644 --- a/lib/models/page.js +++ b/lib/models/page.js @@ -34,7 +34,7 @@ module.exports = ctx => { Page.virtual('permalink').get(function() { const { config } = ctx; let partial_url = this.path; - if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return `${ctx.config.url}/${partial_url}`; }); diff --git a/lib/models/post.js b/lib/models/post.js index dbcdaaaa4d..8a7058fd4e 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -54,7 +54,7 @@ module.exports = ctx => { const { config } = ctx; let partial_url = self.url_for(this.path); if (config.relative_link) partial_url = `/${partial_url}`; - if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return config.url + partial_url.replace(config.root, '/'); }); diff --git a/lib/models/tag.js b/lib/models/tag.js index 9924ea5a54..d1f0dfdfbb 100644 --- a/lib/models/tag.js +++ b/lib/models/tag.js @@ -31,7 +31,7 @@ module.exports = ctx => { Tag.virtual('permalink').get(function() { const { config } = ctx; let partial_url = this.path; - if (config.canonical_url) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return `${ctx.config.url}/${partial_url}`; }); diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index 602ca43dd7..6d03468ac7 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -104,8 +104,8 @@ describe('Category', () => { return Category.removeById(data._id); })); - it('permalink - canonical_url', () => { - hexo.config.canonical_url = true; + it('permalink - trailing_index', () => { + hexo.config.trailing_url.trailing_index = false; return Category.insert({ name: 'foo' }).then(data => { diff --git a/test/scripts/models/page.js b/test/scripts/models/page.js index 3657fd7325..728e08d8f5 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -61,7 +61,7 @@ describe('Page', () => { })); it('permalink - canonical_url', () => { - hexo.config.canonical_url = true; + hexo.config.trailing_url.trailing_index = false; return Page.insert({ source: 'foo.md', path: 'bar/index.html' diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index c71fdb067f..c5b0f3ff00 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -122,7 +122,7 @@ describe('Post', () => { }); it('permalink - canonical_url', () => { - hexo.config.canonical_url = true; + hexo.config.trailing_url.trailing_index = false; return Post.insert({ source: 'foo.md', slug: 'bar' diff --git a/test/scripts/models/tag.js b/test/scripts/models/tag.js index 6aced68b4a..e110b8ac74 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -90,7 +90,7 @@ describe('Tag', () => { })); it('permalink - canonical_url', () => { - hexo.config.canonical_url = true; + hexo.config.trailing_url.trailing_index = false; return Tag.insert({ name: 'foo' }).then(data => { From 3f03a31479e5456a8da561be456849ebdb5f8038 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 15 Sep 2019 07:36:11 +0100 Subject: [PATCH 5/6] fix: use config.pretty_urls option --- lib/hexo/default_config.js | 2 +- lib/models/category.js | 2 +- lib/models/page.js | 2 +- lib/models/post.js | 2 +- lib/models/tag.js | 2 +- test/scripts/models/category.js | 2 +- test/scripts/models/page.js | 2 +- test/scripts/models/post.js | 2 +- test/scripts/models/tag.js | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 4d785be0d8..aee53391df 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -13,7 +13,7 @@ module.exports = { root: '/', permalink: ':year/:month/:day/:title/', permalink_defaults: {}, - trailing_url: { + pretty_urls: { trailing_index: true }, // Directory diff --git a/lib/models/category.js b/lib/models/category.js index 6c29342e01..916ae03731 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -40,7 +40,7 @@ module.exports = ctx => { Category.virtual('permalink').get(function() { const { config } = ctx; let partial_url = this.path; - if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return `${ctx.config.url}/${partial_url}`; }); diff --git a/lib/models/page.js b/lib/models/page.js index d30fbbfa4b..5f5d95dcb0 100644 --- a/lib/models/page.js +++ b/lib/models/page.js @@ -34,7 +34,7 @@ module.exports = ctx => { Page.virtual('permalink').get(function() { const { config } = ctx; let partial_url = this.path; - if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return `${ctx.config.url}/${partial_url}`; }); diff --git a/lib/models/post.js b/lib/models/post.js index 8a7058fd4e..78c645ceae 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -54,7 +54,7 @@ module.exports = ctx => { const { config } = ctx; let partial_url = self.url_for(this.path); if (config.relative_link) partial_url = `/${partial_url}`; - if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return config.url + partial_url.replace(config.root, '/'); }); diff --git a/lib/models/tag.js b/lib/models/tag.js index d1f0dfdfbb..02fe8918b2 100644 --- a/lib/models/tag.js +++ b/lib/models/tag.js @@ -31,7 +31,7 @@ module.exports = ctx => { Tag.virtual('permalink').get(function() { const { config } = ctx; let partial_url = this.path; - if (config.trailing_url.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); + if (config.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); return `${ctx.config.url}/${partial_url}`; }); diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index 6d03468ac7..b4b4401c04 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -105,7 +105,7 @@ describe('Category', () => { })); it('permalink - trailing_index', () => { - hexo.config.trailing_url.trailing_index = false; + hexo.config.pretty_urls.trailing_index = false; return Category.insert({ name: 'foo' }).then(data => { diff --git a/test/scripts/models/page.js b/test/scripts/models/page.js index 728e08d8f5..22ce23dd59 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -61,7 +61,7 @@ describe('Page', () => { })); it('permalink - canonical_url', () => { - hexo.config.trailing_url.trailing_index = false; + hexo.config.pretty_urls.trailing_index = false; return Page.insert({ source: 'foo.md', path: 'bar/index.html' diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index c5b0f3ff00..f719ccceff 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -122,7 +122,7 @@ describe('Post', () => { }); it('permalink - canonical_url', () => { - hexo.config.trailing_url.trailing_index = false; + hexo.config.pretty_urls.trailing_index = false; return Post.insert({ source: 'foo.md', slug: 'bar' diff --git a/test/scripts/models/tag.js b/test/scripts/models/tag.js index e110b8ac74..c32b9ee2e2 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -90,7 +90,7 @@ describe('Tag', () => { })); it('permalink - canonical_url', () => { - hexo.config.trailing_url.trailing_index = false; + hexo.config.pretty_urls.trailing_index = false; return Tag.insert({ name: 'foo' }).then(data => { From 8c0ca000a279ebe61c150a8a60078b332de08442 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 15 Sep 2019 07:41:22 +0100 Subject: [PATCH 6/6] test: reset config and update test name --- test/scripts/models/category.js | 1 + test/scripts/models/page.js | 3 ++- test/scripts/models/post.js | 3 ++- test/scripts/models/tag.js | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index b4b4401c04..bfb8f0b562 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -110,6 +110,7 @@ describe('Category', () => { name: 'foo' }).then(data => { data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + hexo.config.pretty_urls.trailing_index = true; return Category.removeById(data._id); }); }); diff --git a/test/scripts/models/page.js b/test/scripts/models/page.js index 22ce23dd59..57778084f4 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -60,13 +60,14 @@ describe('Page', () => { return Page.removeById(data._id); })); - it('permalink - canonical_url', () => { + it('permalink - trailing_index', () => { hexo.config.pretty_urls.trailing_index = false; return Page.insert({ source: 'foo.md', path: 'bar/index.html' }).then(data => { data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + hexo.config.pretty_urls.trailing_index = true; return Page.removeById(data._id); }); }); diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index f719ccceff..913e8aef03 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -121,13 +121,14 @@ describe('Post', () => { }); }); - it('permalink - canonical_url', () => { + it('permalink - trailing_index', () => { hexo.config.pretty_urls.trailing_index = false; return Post.insert({ source: 'foo.md', slug: 'bar' }).then(data => { data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + hexo.config.pretty_urls.trailing_index = true; return Post.removeById(data._id); }); }); diff --git a/test/scripts/models/tag.js b/test/scripts/models/tag.js index c32b9ee2e2..eb91266711 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -89,12 +89,13 @@ describe('Tag', () => { return Tag.removeById(data._id); })); - it('permalink - canonical_url', () => { + it('permalink - trailing_index', () => { hexo.config.pretty_urls.trailing_index = false; return Tag.insert({ name: 'foo' }).then(data => { data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); + hexo.config.pretty_urls.trailing_index = true; return Tag.removeById(data._id); }); });