Skip to content

How to get the absolute image path from relative image path in contentLoaded function of plugin-content-blog #10467

Answered by slorber
jiashengguo asked this question in Q&A
Discussion options

You must be logged in to vote

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:

import {useBlogPost} from '@docusaurus/plugin-content-blog/client';


const {assets, metadata} = useBlogPost();
const image = assets.image ?? frontMatter.image;

Note the assets thing is not really elegant, I'd like to find a better solution in the futu…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@slorber
Comment options

@jiashengguo
Comment options

@slorber
Comment options

Answer selected by jiashengguo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants