Skip to content

Commit

Permalink
fix: permalink should be overwritten when post_asset_folder is true (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon authored Jan 12, 2024
1 parent 75ea6ce commit 6a91fb6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/plugins/filter/post_permalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ let permalink;

function postPermalinkFilter(this: Hexo, data) {
const { config } = this;
const { id, _id, slug, title, date, __permalink } = data;
const { id, _id, slug, title, date } = data;
let { __permalink } = data;
const { post_asset_folder } = config;

if (__permalink) {
if (post_asset_folder && !__permalink.endsWith('/') && !__permalink.endsWith('.html')) {
__permalink += '/';
}
if (!__permalink.startsWith('/')) return `/${__permalink}`;
return __permalink;
}
Expand Down Expand Up @@ -62,7 +67,11 @@ function postPermalinkFilter(this: Hexo, data) {
}
}

return permalink.stringify(meta);
const permalink_stringify = permalink.stringify(meta);
if (post_asset_folder && !permalink_stringify.endsWith('/') && !permalink_stringify.endsWith('.html')) {
return `${permalink_stringify}/`;
}
return permalink_stringify;
}

export = postPermalinkFilter;
48 changes: 48 additions & 0 deletions test/scripts/filters/post_permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,52 @@ describe('post_permalink', () => {

await Promise.all(posts.map(post => Post.removeById(post._id)));
});

it('permalink - should end with / or .html - 1', async () => {
hexo.config.post_asset_folder = true;
hexo.config.permalink = ':year/:month/:day/:title';

const post = await Post.insert({
source: 'foo.md',
slug: 'foo',
date: moment('2014-01-02')
});

postPermalink(post).should.eql('2014/01/02/foo/');

Post.removeById(post._id);
hexo.config.post_asset_folder = false;
});


it('permalink - should end with / or .html - 2', async () => {
hexo.config.post_asset_folder = true;

const posts = await Post.insert([{
source: 'my-new-post.md',
slug: 'hexo/permalink-test',
__permalink: 'hexo/permalink-test',
title: 'Permalink Test',
date: moment('2014-01-02')
}, {
source: 'another-new-post.md',
slug: '/hexo-hexo/permalink-test-2',
__permalink: '/hexo-hexo/permalink-test-2/',
title: 'Permalink Test',
date: moment('2014-01-02')
}, {
source: 'another-another-new-post.md',
slug: '/hexo-hexo/permalink-test-3',
__permalink: '/hexo-hexo/permalink-test-3.html',
title: 'Permalink Test',
date: moment('2014-01-02')
}]);

postPermalink(posts[0]).should.eql('/hexo/permalink-test/');
postPermalink(posts[1]).should.eql('/hexo-hexo/permalink-test-2/');
postPermalink(posts[2]).should.eql('/hexo-hexo/permalink-test-3.html');

await Promise.all(posts.map(post => Post.removeById(post._id)));
hexo.config.post_asset_folder = false;
});
});

0 comments on commit 6a91fb6

Please sign in to comment.