Skip to content
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

Image caption didn't show #5

Closed
codewithnathan97 opened this issue Nov 28, 2020 · 4 comments
Closed

Image caption didn't show #5

codewithnathan97 opened this issue Nov 28, 2020 · 4 comments

Comments

@codewithnathan97
Copy link

Hello!

Thank you Sara for this wonderful template!

I just tried to add a caption to an image today, but I can't seem to have it visible on the development site. I tried adding showCaptions to the gatsby-remark-images option:

{
      resolve: `gatsby-remark-images`,
      options: {
        maxWidth: 590,
        showCaptions: true,
      },
 },

The code for the image looks like this:

![Cat here](./cat.jpg "Cat here" )

But I still can't render the caption:

Screen Shot 2020-11-29 at 00 33 11

Can anyone help me find out what's wrong? is this a problem with the gatsby-remark-image itself?

Thanks again!

@codewithnathan97
Copy link
Author

codewithnathan97 commented Nov 28, 2020

I was able to solve this. Will leave the solution here for anyone interested:

The image is rendered through react-markdown, so to add a caption to the image, you will need to add a custom renderer for images. You will find some info here

Basicallty, add an image renderer like this in index.js:

function imgBlock({src, title, alt}) {
  return (
    <figure>
      <img alt={alt} src={src} />
      <figcaption class="img-caption">{alt}</figcaption>
    </figure>
  );
}

Then for the renderers:

const renderers = {
  code: codeBlock,
  heading: headingRenderer,
  root: RootRenderer,
  image: imgBlock,
}

@SaraVieira
Copy link
Owner

Hey!

Oh, you closed as I was answering, I will add that as an option if the user has an image today :)

I'm sorry you had to look in the deep ends of react markdown like I had several times ahah

@codewithnathan97
Copy link
Author

Ah sorry! I was still tweaking the code after I posted the issue haha.

I will add that as an option if the user has an image today :)

That would be great! Thanks a lot for sharing this :)

@SaraVieira
Copy link
Owner

Done in this commit: fc47848

Thank you for the good request :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants