8

I'm using react-markdown in a web app. When I load an image stored in the same folder as the .md file using ![](image1.jpg) I get the image rendered correctly in the preview in Visual Studio, but not in the browser - instead I get an image error in both the development and production version. I've tried escaping HTML and this doesn't work either. I'm loading a .md page as a component in and all text and formatting works perfectly.

Thanks

5
  • Did you find the solution? Commented Aug 15, 2018 at 14:23
  • No I didn't, I ended up just rendering the images below and outside the markdown component, instead of inline Commented Aug 17, 2018 at 2:00
  • 1
    Ok, thank you for your answer! As you've probably guess, I was experiencing the same problem. The cause of the problem is in fact simple and I found it by asking myself this question: what is the difference between my md file read by the editor and what I see in my browser? In React, your code is compiled: the image is just not found. For example, if you use create-react-app, the basic configuration allow you to import images like this: import myImage from 'path/to/image.png'. In your md file, if you use the image path then the compiler can't find it. Are you using create-react-app? Commented Aug 18, 2018 at 10:29
  • @Sylla thanks and sorry for the late response. Yes I'm using create-react-app. So I import the image as a component in the main .js, then how do I call the component in the .md file? Commented Dec 14, 2018 at 3:24
  • Unfortunately the solution is not that straightforward, if you want to modify the way you use an image, you have to eject. I had found another solution but I can't find the link anymore... To allow server side rendering, I finally chose to use Next.js along with MDX and this is working very well! Commented Dec 27, 2018 at 14:37

3 Answers 3

19

I kind of had the same issue with Strapi and Gatsby, watch this video it might help to fix the image URI to load the image correctly: https://youtu.be/LIrK5KxsUSE?t=351

This is what fixed mine:

 <Reactmarkdown
  source={data.strapiArticle.content}
  transformImageUri={uri =>
    uri.startsWith("http") ? uri : `${process.env.REACT_IMAGE_BASE_URL}${uri}`
  }
/>

.env.development file:

REACT_IMAGE_BASE_URL =http://localhost:1337
Sign up to request clarification or add additional context in comments.

1 Comment

REACT_IMAGE_BASE_URL doesn't work for me. Only if REACT_APP_IMAGE_BASE_URL then it works. And how to add domain name dynamically?
3

Install rehype-raw to your project

npm install rehype-raw

Import rehypeRaw

import rehypeRaw from 'rehype-raw'

Add rehypePlugins props on your component

<ReactMarkdown remarkPlugins={[[remarkGfm,]]} children={readmeContent} rehypePlugins={[rehypeRaw]} />

Comments

1

On issues page of react-markdown I found an issue, where a guy solved this by adding escapeHtml={false} to <ReactMarkdown /> component. I tried it and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.