From 6a91fb67c1bb5b755dd537fbab61309e7ba48528 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Fri, 12 Jan 2024 15:27:18 +0800 Subject: [PATCH] fix: permalink should be overwritten when post_asset_folder is true (#5254) --- lib/plugins/filter/post_permalink.ts | 13 +++++-- test/scripts/filters/post_permalink.js | 48 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/plugins/filter/post_permalink.ts b/lib/plugins/filter/post_permalink.ts index f066421330..5c92f7fc87 100644 --- a/lib/plugins/filter/post_permalink.ts +++ b/lib/plugins/filter/post_permalink.ts @@ -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; } @@ -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; diff --git a/test/scripts/filters/post_permalink.js b/test/scripts/filters/post_permalink.js index e01f22a1be..3643ed752f 100644 --- a/test/scripts/filters/post_permalink.js +++ b/test/scripts/filters/post_permalink.js @@ -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; + }); });