Skip to content

Commit

Permalink
fix(post-asset): strip /\.html?$/ extensions on permalink (#2881)
Browse files Browse the repository at this point in the history
Fixes #2134
Closes #2551
Closes #2504
  • Loading branch information
JLHwung authored and NoahDragon committed Dec 4, 2017
1 parent 08a9b5f commit 284c1b9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/models/post_asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Schema = require('warehouse').Schema;
var pathFn = require('path');
const _ = require('lodash');

module.exports = function(ctx) {
var PostAsset = new Schema({
Expand All @@ -19,7 +20,8 @@ module.exports = function(ctx) {

// PostAsset.path is file path relative to `public_dir`
// no need to urlescape, #1562
return pathFn.join(post.path, this.slug);
// strip /\.html?$/ extensions on permalink, #2134
return pathFn.join(_.replace(post.path, /\.html?$/, ''), this.slug);
});

PostAsset.virtual('source').get(function() {
Expand Down
42 changes: 42 additions & 0 deletions test/scripts/models/post_asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ describe('PostAsset', () => {
return PostAsset.removeById(data._id);
}));

it('path - virtual - when permalink is .html', () => {
hexo.config.permalink = ':year/:month/:day/:title.html';
return PostAsset.insert({
_id: 'source/_posts/test/foo.html',
slug: 'foo.htm',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
});

it('path - virtual - when permalink is .htm', () => {
hexo.config.permalink = ':year/:month/:day/:title.htm';
return PostAsset.insert({
_id: 'source/_posts/test/foo.htm',
slug: 'foo.htm',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
});

it('path - virtual - when permalink contains .htm not in the end', () => {
hexo.config.permalink = ':year/:month/:day/:title/.htm-foo/';
return PostAsset.insert({
_id: 'source/_posts/test/foo.html',
slug: 'foo.html',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path + '.htm-foo/', data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
});

it('source - virtual', () => PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
Expand Down

0 comments on commit 284c1b9

Please sign in to comment.