Markdown files are converting to html OK on my nextjs site.
However, images referenced with paths like  aren't rendering and show broken images in the browser. When I inspect the source code, I see html like:
<img src="./images/teapot.jpg" alt="a teapot">
Instead of the more complex img tag that nextjs usually generates:
<img alt="teapot" loading="lazy" width="510" height="510" decoding="async" data-nimg="1" class="avatar" srcset="/_next/image?url=%2F_next%2Fstatic%2F..., /_next/image?url=%2F_next%2Fstatic%2Fmedia%2F..." src="/_next/image?url=%2F_next%2Fstatic%2Fmedia%2F..." style="color: transparent;">
This is the basic shape of my mdx definition in contentlayer.config.js:
mdx: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
[
rehypePrettyCode,
{
theme: ...,
onVisitLine(node) {...},
onVisitHighlightedLine(node) {...},
onVisitHighlightedWord(node) {...},
},
],
[
rehypeAutolinkHeadings,
{...},
],
],
},
What do I need to do to get images to show? I am not sure where the problem lies... is it remark, rehype, contentlayer or nextjs itself? Thanks.
When using a relative path for your image, the path must start with a
/, so use, rather than.Make sure that your
imagesfolder exists in the rootpublicdirectory of your NextJS projectRemark converts markdown to html. It knows nothing about React or NextJS's
Imagecomponent. You can tell it what to do with certain elements. I usereact-remarkwithin a React component and this is the additional Rehype options required to get it using the NextImagecomponent:Alternatively, it's also possible to apply a blanket conversion to all
mdconversions by using @next/mdx and creating amdx-components.jsat the root of your project (tested on Next v 13) and specifying custom components there. You will need to setup the @next/mdx library, but here is the relevant solution to your particular issue (link to docs):