diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index 710e764586..aee53391df 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -13,6 +13,9 @@ module.exports = { root: '/', permalink: ':year/:month/:day/:title/', permalink_defaults: {}, + pretty_urls: { + trailing_index: true + }, // Directory source_dir: 'source', public_dir: 'public', diff --git a/lib/models/category.js b/lib/models/category.js index a025b5316a..916ae03731 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.pretty_urls.trailing_index === false) 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..5f5d95dcb0 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.pretty_urls.trailing_index === false) 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..78c645ceae 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.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 22491f1fd9..02fe8918b2 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.pretty_urls.trailing_index === false) partial_url = partial_url.replace(/index\.html$/, ''); + return `${ctx.config.url}/${partial_url}`; }); Tag.virtual('posts').get(function() { diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index 80dbb7f634..bfb8f0b562 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -104,6 +104,17 @@ describe('Category', () => { return Category.removeById(data._id); })); + it('permalink - trailing_index', () => { + hexo.config.pretty_urls.trailing_index = false; + return Category.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 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..57778084f4 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -60,6 +60,18 @@ describe('Page', () => { return Page.removeById(data._id); })); + 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); + }); + }); + 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..913e8aef03 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -121,6 +121,18 @@ describe('Post', () => { }); }); + 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); + }); + }); + 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..eb91266711 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -89,6 +89,17 @@ describe('Tag', () => { return Tag.removeById(data._id); })); + 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); + }); + }); + it('posts - virtual', () => Post.insert([ {source: 'foo.md', slug: 'foo'}, {source: 'bar.md', slug: 'bar'},