From b9f2f9d33bb5896c90ab8e2c2c7366530e9169b6 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 27 Jul 2023 23:24:47 +0800 Subject: [PATCH] fix: permalink should be overwritten when post_asset_folder is true --- 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 c715e67b5d..753ab02ea2 100644 --- a/lib/plugins/filter/post_permalink.ts +++ b/lib/plugins/filter/post_permalink.ts @@ -4,9 +4,14 @@ let permalink; function postPermalinkFilter(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; } @@ -61,7 +66,11 @@ function postPermalinkFilter(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; + }); });