-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(image): postAsset to prepend post's relative path #159
Conversation
if (data.path && this.config.post_asset_folder && this.config.marked.prependRoot && this.config.marked.postAsset) { | ||
const Post = this.model('Post'); | ||
const source = relative(this.source_dir, data.path); | ||
const post = Post.findOne({ source }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may support Page in future.
- perf: utilise String.replace() * 2000% faster than path.relative()
Ready for review, tested |
May support prepend to path in future, similar function to |
Is the performance being impacted? |
I suspect it will, hence this feature is disabled by default. i'm benchmarking 500 posts + 100 asset images for each post. |
I scaled down my test to 100 posts with 100 asset images in each post, my machine couldn't handle 500 posts. Significant slowdown was due to It's now 3x slower. I also added a note about the processing time. |
lib/renderer.js
Outdated
if (!/^(\/|\\)/.test(href) && postId) { | ||
const PostAsset = hexo.model('PostAsset'); | ||
// slug requires platform-specific path | ||
const asset = PostAsset.findOne({ post: postId, slug: href.replace(/\/|\\/g, sep) }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using findOne
, is it possible to use findById
directly?
https://github.com/hexojs/hexo/blob/master/lib/plugins/processor/post.js
Also, each post should only find PostAsset
for once (not for every image). A cache should be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { Cache } = require('hexo-util');
const postAssetCache = new Cache();
// ...
const asset = postAssetCache.apply(postId, () => PostAsset.findOne({ post: postId, slug: href.replace(/\/|\\/g, sep) }));
And see if there is any performance improvement.
It could also be:
const { Cache } = require('hexo-util');
let postAssetCache;
// ...
// Only init cache when prependRoot is enabled
if (!relative_link && prependRoot) {
postAssetCache = postAssetCache || new Cache();
}
const asset = postAssetCache.apply(postId, () => PostAsset.findOne({ post: postId, slug: href.replace(/\/|\\/g, sep) }));
I tried and it does work.
in my test, all 10,000 asset images have unique path, so Alternatively, it is possible to get rid of (relevant path is any path that does not start with a forward/backward slash) This also means, with We could offer two modes: # _config.yml
marked:
postAsset: 'accurate|fast' |
Edit: removing |
@curbengh There are still some issue:
I would rather bring up the feature based on |
marked:
external_link:
enable: false
exclude: []
nofollow: false |
It should be no slower than querying warehouse. Also, every renderer that Hexo is supported will be benefited. |
filter still requires querying warehouse to skip non-post-asset, The benchmark showed there's hardly any different when enabling this feature, the slowdown actually comes from |
Fixes hexojs/hexo#4454 cc @v-tawe
How to use:
Based on
asset_img
tag plugin.