Skip to content

Commit 284c1b9

Browse files
JLHwungNoahDragon
authored andcommitted
fix(post-asset): strip /\.html?$/ extensions on permalink (#2881)
Fixes #2134 Closes #2551 Closes #2504
1 parent 08a9b5f commit 284c1b9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/models/post_asset.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var Schema = require('warehouse').Schema;
44
var pathFn = require('path');
5+
const _ = require('lodash');
56

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

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

2527
PostAsset.virtual('source').get(function() {

test/scripts/models/post_asset.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,48 @@ describe('PostAsset', () => {
5656
return PostAsset.removeById(data._id);
5757
}));
5858

59+
it('path - virtual - when permalink is .html', () => {
60+
hexo.config.permalink = ':year/:month/:day/:title.html';
61+
return PostAsset.insert({
62+
_id: 'source/_posts/test/foo.html',
63+
slug: 'foo.htm',
64+
post: post._id
65+
}).then(data => {
66+
data.path.should.eql(pathFn.join(post.path, data.slug));
67+
return PostAsset.removeById(data._id);
68+
}).finally(() => {
69+
hexo.config.permalink = ':year/:month/:day/:title';
70+
});
71+
});
72+
73+
it('path - virtual - when permalink is .htm', () => {
74+
hexo.config.permalink = ':year/:month/:day/:title.htm';
75+
return PostAsset.insert({
76+
_id: 'source/_posts/test/foo.htm',
77+
slug: 'foo.htm',
78+
post: post._id
79+
}).then(data => {
80+
data.path.should.eql(pathFn.join(post.path, data.slug));
81+
return PostAsset.removeById(data._id);
82+
}).finally(() => {
83+
hexo.config.permalink = ':year/:month/:day/:title';
84+
});
85+
});
86+
87+
it('path - virtual - when permalink contains .htm not in the end', () => {
88+
hexo.config.permalink = ':year/:month/:day/:title/.htm-foo/';
89+
return PostAsset.insert({
90+
_id: 'source/_posts/test/foo.html',
91+
slug: 'foo.html',
92+
post: post._id
93+
}).then(data => {
94+
data.path.should.eql(pathFn.join(post.path + '.htm-foo/', data.slug));
95+
return PostAsset.removeById(data._id);
96+
}).finally(() => {
97+
hexo.config.permalink = ':year/:month/:day/:title';
98+
});
99+
});
100+
59101
it('source - virtual', () => PostAsset.insert({
60102
_id: 'source/_posts/test/foo.jpg',
61103
slug: 'foo.jpg',

0 commit comments

Comments
 (0)