Skip to content

Commit

Permalink
Merge pull request #3917 from curbengh/pretty_urls-util
Browse files Browse the repository at this point in the history
feat(permalink): pretty_urls.trailing_html
  • Loading branch information
SukkaW authored Dec 10, 2019
2 parents 219fded + e6955d3 commit 140ed36
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
permalink: ':year/:month/:day/:title/',
permalink_defaults: {},
pretty_urls: {
trailing_index: true
trailing_index: true,
trailing_html: true
},
// Directory
source_dir: 'source',
Expand Down
5 changes: 1 addition & 4 deletions lib/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ module.exports = ctx => {
});

Category.virtual('permalink').get(function() {
const { config } = ctx;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
return full_url_for.call(ctx, this.path);
});

Category.virtual('posts').get(function() {
Expand Down
5 changes: 1 addition & 4 deletions lib/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ module.exports = ctx => {
});

Page.virtual('permalink').get(function() {
const { config } = ctx;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
return full_url_for.call(ctx, this.path);
});

Page.virtual('full_source').get(function() {
Expand Down
5 changes: 1 addition & 4 deletions lib/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ module.exports = ctx => {
});

Post.virtual('permalink').get(function() {
const { config } = ctx;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
return full_url_for.call(ctx, this.path);
});

Post.virtual('full_source').get(function() {
Expand Down
5 changes: 1 addition & 4 deletions lib/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ module.exports = ctx => {
});

Tag.virtual('permalink').get(function() {
const { config } = ctx;
let url = full_url_for.call(ctx, this.path);
if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, '');
return url;
return full_url_for.call(ctx, this.path);
});

Tag.virtual('posts').get(function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"hexo-fs": "^2.0.0",
"hexo-i18n": "^1.0.0",
"hexo-log": "^1.0.0",
"hexo-util": "^1.6.1",
"hexo-util": "^1.7.0",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"micromatch": "^4.0.2",
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 @@ -116,6 +116,17 @@ describe('Category', () => {
});
});

it('permalink - trailing_html', () => {
hexo.config.pretty_urls.trailing_html = false;
return Category.insert({
name: 'foo'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, ''));
hexo.config.pretty_urls.trailing_html = true;
return Category.removeById(data._id);
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
return Category.insert({
Expand Down
24 changes: 24 additions & 0 deletions test/scripts/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ describe('Page', () => {
});
});

it('permalink - trailing_html', () => {
hexo.config.pretty_urls.trailing_html = false;
return Page.insert({
source: 'foo.md',
path: 'bar/foo.html'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, ''));
hexo.config.pretty_urls.trailing_html = true;
return Page.removeById(data._id);
});
});

it('permalink - trailing_html - index.html', () => {
hexo.config.pretty_urls.trailing_html = false;
return Page.insert({
source: 'foo.md',
path: 'bar/index.html'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path);
hexo.config.pretty_urls.trailing_html = true;
return Page.removeById(data._id);
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
const path = 'bár';
Expand Down
26 changes: 25 additions & 1 deletion test/scripts/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,38 @@ describe('Post', () => {
hexo.config.pretty_urls.trailing_index = false;
return Post.insert({
source: 'foo.md',
slug: 'bar'
slug: '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 Post.removeById(data._id);
});
});

it('permalink - trailing_html', () => {
hexo.config.pretty_urls.trailing_html = false;
return Post.insert({
source: 'foo.md',
slug: 'bar/foo.html'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, ''));
hexo.config.pretty_urls.trailing_html = true;
return Post.removeById(data._id);
});
});

it('permalink - trailing_html - index.html', () => {
hexo.config.pretty_urls.trailing_html = false;
return Post.insert({
source: 'foo.md',
slug: 'bar/index.html'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path);
hexo.config.pretty_urls.trailing_html = 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 @@ -101,6 +101,17 @@ describe('Tag', () => {
});
});

it('permalink - trailing_html', () => {
hexo.config.pretty_urls.trailing_html = false;
return Tag.insert({
name: 'foo'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, ''));
hexo.config.pretty_urls.trailing_html = true;
return Tag.removeById(data._id);
});
});

it('permalink - should be encoded', () => {
hexo.config.url = 'http://fôo.com';
return Tag.insert({
Expand Down

0 comments on commit 140ed36

Please sign in to comment.