From de2e3657709bcae2e6f7d9433b03023ba9db68b9 Mon Sep 17 00:00:00 2001 From: simonkimi Date: Wed, 24 Jul 2024 10:13:58 +0800 Subject: [PATCH 1/3] Add post_extensions to distinguish post --- lib/hexo/default_config.ts | 1 + lib/plugins/processor/post.ts | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/hexo/default_config.ts b/lib/hexo/default_config.ts index 390b8f32f5..e9331ececa 100644 --- a/lib/hexo/default_config.ts +++ b/lib/hexo/default_config.ts @@ -36,6 +36,7 @@ export = { filename_case: 0, render_drafts: false, post_asset_folder: false, + post_extensions: [], relative_link: false, future: true, syntax_highlighter: 'highlight.js', diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index 856fff5ba2..26590d3faf 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -50,7 +50,11 @@ export = (ctx: Hexo) => { // if post_asset_folder is set, restrict renderable files to default file extension if (result.renderable && ctx.config.post_asset_folder) { - result.renderable = (extname(ctx.config.new_post_name) === extname(path)); + if (!Array.isArray(ctx.config.post_extensions) || ctx.config.post_extensions.length === 0) { + result.renderable = (extname(ctx.config.new_post_name) === extname(path)); + } else { + result.renderable = ctx.config.post_extensions.includes(extname(path).slice(1)); + } } return result; From 01ec2c370b246f955b6933ce23b1d17af39c2440 Mon Sep 17 00:00:00 2001 From: simonkimi Date: Wed, 31 Jul 2024 23:19:17 +0800 Subject: [PATCH 2/3] add test --- test/scripts/processors/post.ts | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/scripts/processors/post.ts b/test/scripts/processors/post.ts index e3f1ff54c0..8b95806a50 100644 --- a/test/scripts/processors/post.ts +++ b/test/scripts/processors/post.ts @@ -1359,4 +1359,42 @@ describe('post', () => { PostAsset.removeById(id) ]); }); + + it('post - post_extensions', async () => { + function fooRenderer() { + return ''; + } + + hexo.extend.renderer.register('md', 'html', fooRenderer, true); + hexo.extend.renderer.register('adoc', 'html', fooRenderer, true); + hexo.extend.renderer.register('ejs', 'html', fooRenderer, true); + + hexo.config.post_asset_folder = true; + hexo.config.new_post_name = ':title.md'; + + hexo.config.post_extensions = null; + pattern.match('_posts/foo.md').should.have.property('renderable', true); + pattern.match('_posts/foo.adoc').should.have.property('renderable', false); + pattern.match('_posts/foo.ejs').should.have.property('renderable', false); + + hexo.config.post_extensions = []; + pattern.match('_posts/foo.md').should.have.property('renderable', true); + pattern.match('_posts/foo.adoc').should.have.property('renderable', false); + pattern.match('_posts/foo.ejs').should.have.property('renderable', false); + + hexo.config.post_extensions = ['adoc']; + pattern.match('_posts/foo.md').should.have.property('renderable', false); + pattern.match('_posts/foo.adoc').should.have.property('renderable', true); + pattern.match('_posts/foo.ejs').should.have.property('renderable', false); + + hexo.config.post_extensions = ['md', 'adoc']; + pattern.match('_posts/foo.md').should.have.property('renderable', true); + pattern.match('_posts/foo.adoc').should.have.property('renderable', true); + pattern.match('_posts/foo.ejs').should.have.property('renderable', false); + + hexo.config.post_extensions = ['md', 'adoc', 'ejs']; + pattern.match('_posts/foo.md').should.have.property('renderable', true); + pattern.match('_posts/foo.adoc').should.have.property('renderable', true); + pattern.match('_posts/foo.ejs').should.have.property('renderable', true); + }); }); From d15fcd9a7bd12c46bd6a8ec77f867d177cb0c848 Mon Sep 17 00:00:00 2001 From: simonkimi Date: Sun, 18 Aug 2024 15:28:54 +0800 Subject: [PATCH 3/3] update comments --- lib/plugins/processor/post.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index 26590d3faf..83880ee39a 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -48,7 +48,7 @@ export = (ctx: Hexo) => { // checks only if there is a renderer for the file type or if is included in skip_render result.renderable = ctx.render.isRenderable(path) && !isMatch(path, ctx.config.skip_render); - // if post_asset_folder is set, restrict renderable files to default file extension + // if post_asset_folder is set, restrict renderable files to post_extensions or the default file extension if (result.renderable && ctx.config.post_asset_folder) { if (!Array.isArray(ctx.config.post_extensions) || ctx.config.post_extensions.length === 0) { result.renderable = (extname(ctx.config.new_post_name) === extname(path));