React-markdown: Images are wrapped in a p tag

Created on 2 Nov 2017  路  7Comments  路  Source: remarkjs/react-markdown

All of the images are wrapped in a p tag, which makes styling images hard. If I set a max-width of a paragraph 600px, but I want the image to be 700px and responsive at that, this becomes awful.

I have to custom render paragraphs, add a class to them, style them based on that. I feel like it would be easier not to wrap images in paragraphs.

Any reason why this is done like this?

Most helpful comment

@wallawe something like this should work.

renderParagraph(props) {
  const { children } = props;

  if (children && children[0]
    && children.length === 1
    && children[0].props
    && children[0].props.src) { // rendering media without p wrapper

    return children;
  }

  return <p>{children}</p>;
}
renderPost() {
  return (
    <ReactMarkdown
      source={source}
      renderers={{
        paragraph: this.renderParagraph,
      }}
    />
  );
}

All 7 comments

Only because the current parser emits a paragraph. In v3 you have the option to manipulate the AST before rendering, which should make unwrapping images pretty simple. I'll get back to you with an example once v3 is released.

I found a work around. I'm just checking whether it contains an image and return only the image.

Can we get an example of how to do this in v3?

@rexxars How can we achieve this is v3? How can we make it not wrap the output in a paragraph? :)

@iremlopsum mind providing an example of how you did this?

@wallawe something like this should work.

renderParagraph(props) {
  const { children } = props;

  if (children && children[0]
    && children.length === 1
    && children[0].props
    && children[0].props.src) { // rendering media without p wrapper

    return children;
  }

  return <p>{children}</p>;
}
renderPost() {
  return (
    <ReactMarkdown
      source={source}
      renderers={{
        paragraph: this.renderParagraph,
      }}
    />
  );
}

you're the man, thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wqh17101 picture wqh17101  路  3Comments

pcvandamcb picture pcvandamcb  路  3Comments

hiteshpatwari picture hiteshpatwari  路  3Comments

IonicaBizau picture IonicaBizau  路  4Comments

zhenyulin picture zhenyulin  路  3Comments