From 9385c81f5a50dc2b6947a543776f80105ee229b6 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Fri, 19 Apr 2024 02:29:42 +0900 Subject: [PATCH 1/4] perf(processor/post): improve processing speed when `config.post_asset_folder` is enabled --- lib/plugins/processor/post.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index 5d1367eea0..03fe91060d 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -1,7 +1,7 @@ import { toDate, timezone, isExcludedFile, isTmpFile, isHiddenFile, isMatch } from './common'; import Promise from 'bluebird'; import { parse as yfm } from 'hexo-front-matter'; -import { extname, join } from 'path'; +import { extname, join, posix } from 'path'; import { stat, listDir } from 'hexo-fs'; import { slugize, Pattern, Permalink } from 'hexo-util'; import { magenta } from 'picocolors'; @@ -278,16 +278,18 @@ function processAsset(ctx: Hexo, file: _File) { return; } - // TODO: Better post searching - const post = Post.toArray().find(post => file.source.startsWith(post.asset_dir)); - if (post != null && (post.published || ctx._showDrafts())) { - return PostAsset.save({ - _id: id, - slug: file.source.substring(post.asset_dir.length), - post: post._id, - modified: file.type !== 'skip', - renderable: file.params.renderable - }); + if (Post.length > 0) { + const assetDir = id.slice(0, id.lastIndexOf('/')); + const post = Post.findOne(p => p.asset_dir.endsWith(posix.join(assetDir, '/'))); + if (post != null && (post.published || ctx._showDrafts())) { + return PostAsset.save({ + _id: id, + slug: file.source.substring(post.asset_dir.length), + post: post._id, + modified: file.type !== 'skip', + renderable: file.params.renderable + }); + } } if (doc) { From 44a14de9a51609500bdfaf54e9a39f98ef920b8f Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Fri, 19 Apr 2024 20:39:32 +0900 Subject: [PATCH 2/4] refactor: use `Post.findById` if `postAsset` exists --- lib/plugins/processor/post.ts | 36 ++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index 03fe91060d..cf53f7e86f 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -1,13 +1,14 @@ import { toDate, timezone, isExcludedFile, isTmpFile, isHiddenFile, isMatch } from './common'; import Promise from 'bluebird'; import { parse as yfm } from 'hexo-front-matter'; -import { extname, join, posix } from 'path'; +import { extname, join, posix, sep } from 'path'; import { stat, listDir } from 'hexo-fs'; import { slugize, Pattern, Permalink } from 'hexo-util'; import { magenta } from 'picocolors'; import type { _File } from '../../box'; import type Hexo from '../../hexo'; import type { Stats } from 'fs'; +import { PostSchema } from '../../types'; const postDir = '_posts/'; const draftDir = '_drafts/'; @@ -270,28 +271,37 @@ function processAsset(ctx: Hexo, file: _File) { const id = file.source.substring(ctx.base_dir.length); const doc = PostAsset.findById(id); - if (file.type === 'delete') { + if (file.type === 'delete' || Post.length === 0) { if (doc) { return doc.remove(); } - return; } - if (Post.length > 0) { - const assetDir = id.slice(0, id.lastIndexOf('/')); - const post = Post.findOne(p => p.asset_dir.endsWith(posix.join(assetDir, '/'))); + const savePostAsset = (post: PostSchema) => { + return PostAsset.save({ + _id: id, + slug: file.source.substring(post.asset_dir.length), + post: post._id, + modified: file.type !== 'skip', + renderable: file.params.renderable + }); + }; + + if (doc) { + // `doc.post` is `Post.id`. + const post = Post.findById(doc.post); if (post != null && (post.published || ctx._showDrafts())) { - return PostAsset.save({ - _id: id, - slug: file.source.substring(post.asset_dir.length), - post: post._id, - modified: file.type !== 'skip', - renderable: file.params.renderable - }); + return savePostAsset(post); } } + const assetDir = id.slice(0, id.lastIndexOf(sep)); + const post = Post.findOne(p => p.asset_dir.endsWith(posix.join(assetDir, '/'))); + if (post != null && (post.published || ctx._showDrafts())) { + return savePostAsset(post); + } + if (doc) { return doc.remove(); } From f782736c4cb7e207e2c50711f0e7c86fae663f35 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Fri, 19 Apr 2024 20:43:22 +0900 Subject: [PATCH 3/4] chore: add comment --- lib/plugins/processor/post.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index cf53f7e86f..e5c320d359 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -302,6 +302,7 @@ function processAsset(ctx: Hexo, file: _File) { return savePostAsset(post); } + // NOTE: Probably, unreachable. if (doc) { return doc.remove(); } From 03379e8ce32d8649ddb59545b2ccac199513f203 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Fri, 19 Apr 2024 20:45:18 +0900 Subject: [PATCH 4/4] chore: rename unclear variable name --- lib/plugins/processor/post.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index e5c320d359..2baee39021 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -269,11 +269,11 @@ function processAsset(ctx: Hexo, file: _File) { const PostAsset = ctx.model('PostAsset'); const Post = ctx.model('Post'); const id = file.source.substring(ctx.base_dir.length); - const doc = PostAsset.findById(id); + const postAsset = PostAsset.findById(id); if (file.type === 'delete' || Post.length === 0) { - if (doc) { - return doc.remove(); + if (postAsset) { + return postAsset.remove(); } return; } @@ -288,9 +288,9 @@ function processAsset(ctx: Hexo, file: _File) { }); }; - if (doc) { - // `doc.post` is `Post.id`. - const post = Post.findById(doc.post); + if (postAsset) { + // `postAsset.post` is `Post.id`. + const post = Post.findById(postAsset.post); if (post != null && (post.published || ctx._showDrafts())) { return savePostAsset(post); } @@ -303,7 +303,7 @@ function processAsset(ctx: Hexo, file: _File) { } // NOTE: Probably, unreachable. - if (doc) { - return doc.remove(); + if (postAsset) { + return postAsset.remove(); } }