From a4e79d1f21c51c84a71bb834324ba430f7ad183d Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Fri, 19 Apr 2024 02:10:34 +0900 Subject: [PATCH] perf(processor/post): improve processing speed if `config.post_asset_folder` is enabled --- lib/plugins/processor/post.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/plugins/processor/post.ts b/lib/plugins/processor/post.ts index 5d1367eea0..3040a0b43a 100644 --- a/lib/plugins/processor/post.ts +++ b/lib/plugins/processor/post.ts @@ -5,6 +5,7 @@ import { extname, join } from 'path'; import { stat, listDir } from 'hexo-fs'; import { slugize, Pattern, Permalink } from 'hexo-util'; import { magenta } from 'picocolors'; +import { posix } from 'path'; import type { _File } from '../../box'; import type Hexo from '../../hexo'; import type { Stats } from 'fs'; @@ -278,16 +279,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) {