react-markdown icon indicating copy to clipboard operation
react-markdown copied to clipboard

<div> cannot appear as a descendant of <p>.

Open smmoosavi opened this issue 8 years ago • 23 comments

I am setting imageReference in renderers wich wrap img with div and I got this error

const imageRenderer = ({src, alt}) => {
  return (
    <div className="markdown-image__wrapper">
      <img src={src} alt={alt || ''}/>
    </div>
  )
}

const renderers = {imageReference: imageRenderer}
const md = <ReactMarkdown source={value}renderers={renderers}/>

error:

Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>.
    in div (at ...)
    in imageRenderer (created by ReactMarkdown)
    in p (created by ReactMarkdown)
    in div (created by ReactMarkdown)

how I can fix this?

Note: It render correctly, but error shown in console

smmoosavi avatar Jun 17 '18 10:06 smmoosavi

I would really like to know that to if someone finds the solution..

Charlicus avatar Jul 18 '18 14:07 Charlicus

I used something like this to fix img-tags from being wrapped in p-tags:

const ParagraphRenderer = ({ children }) => {
  const hasImage = !!children.find(
    (child) => typeof child === 'object' && child.key && !!child.key.match(/image/g)
  )
  return hasImage ? children : <p>{children}</p>
}

EKMN avatar Jul 22 '18 07:07 EKMN

The issue happens because you can't have div inside of a paragraph. So You may write:

<ReactMarkdown source={yourContent} renderers={{ paragraph: props => <div {...props} />, }} />

Now, the paragraph becomes a div, and there is no more troubles ;).

DoneDeal0 avatar Jun 18 '19 12:06 DoneDeal0

Or you can check, if it is an image make the container as a div, other than that make the container as a paragraph.

like this:

<ReactMarkdown
  source={source}
  renderers={{
    paragraph: props =>
      props.children[0].type.name === "image" ? (
        <div {...props} />
      ) : (
        <p {...props} />
      )
  }}
/>

one could argue this better for the sake of SEO

yassinebridi avatar Aug 05 '19 00:08 yassinebridi

@EKMN and @yassinebridi code didn't work for me.

Here is how I removed <p> tags around images:

const imagesWithoutPTags = (props) => {
  const element = props.children[0];
  return element.type === 'img' ? { ...element } : <p {...props} />;
};

<ReactMarkdown
  source={source}
  escapeHtml={false}
  renderers={{paragraph: imagesWithoutPTags}}
/>

magoz avatar Aug 19 '19 09:08 magoz

Thank you @magoz ! This did the trick for me :D

circuitsyn avatar Feb 02 '20 07:02 circuitsyn

Markdown does not allow images outside of paragraphs/headings, so you can’t just add a div there.

You can unwrap them though, e.g., with https://github.com/remarkjs/remark-unwrap-images. Or you can make your own plugin.

wooorm avatar Oct 08 '20 12:10 wooorm

@magoz Your solution just works with isolated images. @EKMN your solution makes inline images with div but their companion text out to any tag. @wooorm I'm trying to use it but it does not work for me.

SalahAdDin avatar Oct 15 '20 12:10 SalahAdDin

Why not?

wooorm avatar Oct 15 '20 13:10 wooorm

@wooorm I don't know, I'm passing it like:

import unwrapImages from "remark-unwrap-images"

const _mapProps = (props) => ({
  ...props,
  escapeHtml: false,
  plugins: [
    // RemarkMathPlugin,
    // RemarkHighlightPlugin,
    unwrapImages,
    VideoPlugin,
  ],
  renderers: {
    ...props.renderers,
    // math: ({ value }) => <BlockMath>{value}</BlockMath>,
    // inlineMath: ({ value }) => <InlineMath>{value}</InlineMath>,
    code: CodeBlock,
    image: ImageBlock,
    video: VideoBlock,
    paragraph: ParagraphBlock,
  },
})

const Content = (props) => <ReactMarkdown {..._mapProps(props)} />

But it does not unwrap the image.

SalahAdDin avatar Oct 15 '20 13:10 SalahAdDin

And if you try the v5 branch? https://github.com/remarkjs/react-markdown/commits/v5

wooorm avatar Oct 15 '20 13:10 wooorm

@wooorm, how can I install it? because it is not available right now.

SalahAdDin avatar Oct 15 '20 13:10 SalahAdDin

npm can install from git(hub): npm install remarkjs/remark#v5

wooorm avatar Oct 15 '20 13:10 wooorm

@wooorm I'm getting this problem all time: Module not found: Can't resolve 'react-markdown'

SalahAdDin avatar Oct 15 '20 14:10 SalahAdDin

whoops, that was a typo, as we’re talking about react-markdown here, it should’ve been: npm install remarkjs/react-markdown#v5

wooorm avatar Oct 15 '20 14:10 wooorm

@wooorm Actually i installed it like that but it didn't work even when the package was installed.

SalahAdDin avatar Oct 15 '20 14:10 SalahAdDin

Hmm, I guess a dist/ is built from src/ here, so you’re missing that. An alternative is to clone this project, npm install and npm link it, and then use the link locally.

But, this works for me. It might be because I’m on v5. We’re launching a version soon that’ll definitely support this: https://github.com/remarkjs/react-markdown/issues/470

wooorm avatar Oct 15 '20 15:10 wooorm

@wooorm Thanks, it is great.

SalahAdDin avatar Oct 15 '20 16:10 SalahAdDin

@wooorm The new version breaks any solution on this issue and it reopens the problem, is there any solution for this issue with the new version?

SalahAdDin avatar Apr 18 '21 00:04 SalahAdDin

@SalahAdDin no, it makes the first suggestion from @wooorm work, as you can see here https://codesandbox.io/s/react-markdown-debug-forked-75h2t

ChristianMurphy avatar Apr 18 '21 00:04 ChristianMurphy