Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(permalink): add pretty_urls option to remove index.html from url #3691

Merged
merged 6 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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