How to get the absolute image path from relative image path in contentLoaded function of plugin-content-blog #10467
-
I would like to display some related posts for each blog post. I managed to do so by exending I could get the image path of the related post from How can I get that absolute path? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Markdown images like We also have a "createAssets" feature (internal, and not really elegant implementation) that does this too for some front matter fields, and the blog post image front matter is one of them. See how we handle this on our own theme code, and try to reuse that: import {useBlogPost} from '@docusaurus/plugin-content-blog/client';
const {assets, metadata} = useBlogPost();
const image = assets.image ?? frontMatter.image; Note the Unfortunately you won't be able to access function blogPostItemsModule(ids: string[]) {
return ids.map((id) => {
return {
content: {
__import: true,
path: getBlogPostById(id).metadata.source,
query: {
truncated: true,
},
},
};
});
}
function createBlogPostsPaginatedRoutes(): RouteConfig[] {
return blogListPaginated.map((paginated) => {
return {
path: paginated.metadata.permalink,
component: blogListComponent,
exact: true,
modules: {
sidebar: sidebarModulePath,
items: blogPostItemsModule(paginated.items), // The important part
},
props: {
metadata: paginated.metadata,
},
};
});
} The route above will receive an array of blog posts on the |
Beta Was this translation helpful? Give feedback.
metadata.frontMatter.image
is what the user provides, unaltered.Markdown images like
![img](./cover.png)
are converted to<img src={require("./cover.png")}/>
by our mdx Webpack loader.We also have a "createAssets" feature (internal, and not really elegant implementation) that does this too for some front matter fields, and the blog post image front matter is one of them.
See how we handle this on our own theme code, and try to reuse that:
Note the
assets
thing is not really elegant, I'd like to find a better solution in the futu…