Skip to content

Commit

Permalink
Merge pull request #3691 from curbengh/canonical
Browse files Browse the repository at this point in the history
feat(permalink): add pretty_urls option to remove index.html from url & permalink
  • Loading branch information
curbengh authored Sep 18, 2019
2 parents 19ac152 + 8c0ca00 commit ab90487
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion lib/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion lib/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions lib/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
});

Expand Down
5 changes: 4 additions & 1 deletion lib/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions test/scripts/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down
12 changes: 12 additions & 0 deletions test/scripts/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions test/scripts/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions test/scripts/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down

0 comments on commit ab90487

Please sign in to comment.