From 284c1b93824348a44884a7354214c050672e74bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 5 Dec 2017 06:22:22 +0800 Subject: [PATCH] fix(post-asset): strip /\.html?$/ extensions on permalink (#2881) Fixes #2134 Closes #2551 Closes #2504 --- lib/models/post_asset.js | 4 ++- test/scripts/models/post_asset.js | 42 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/models/post_asset.js b/lib/models/post_asset.js index dd87dcd468..9c556aa018 100644 --- a/lib/models/post_asset.js +++ b/lib/models/post_asset.js @@ -2,6 +2,7 @@ var Schema = require('warehouse').Schema; var pathFn = require('path'); +const _ = require('lodash'); module.exports = function(ctx) { var PostAsset = new Schema({ @@ -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() { diff --git a/test/scripts/models/post_asset.js b/test/scripts/models/post_asset.js index dcb88e743b..0df94a28c2 100644 --- a/test/scripts/models/post_asset.js +++ b/test/scripts/models/post_asset.js @@ -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',