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

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

Closed
smmoosavi opened this issue Jun 17, 2018 · 23 comments
Closed

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

smmoosavi opened this issue Jun 17, 2018 · 23 comments
Labels
🙋 no/question This does not need any changes

Comments

@smmoosavi
Copy link

smmoosavi commented Jun 17, 2018

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

@Charlicus
Copy link

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

@EKMN
Copy link

EKMN commented Jul 22, 2018

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>
}

@DoneDeal0
Copy link

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 ;).

@yassinebridi
Copy link

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

@magoz
Copy link

magoz commented Aug 19, 2019

@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}}
/>

@circuitsyn
Copy link

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

@wooorm
Copy link
Member

wooorm commented Oct 8, 2020

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.

@SalahAdDin
Copy link
Contributor

SalahAdDin commented Oct 15, 2020

@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.

@wooorm
Copy link
Member

wooorm commented Oct 15, 2020

Why not?

@SalahAdDin
Copy link
Contributor

@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.

@wooorm
Copy link
Member

wooorm commented Oct 15, 2020

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

@SalahAdDin
Copy link
Contributor

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

@wooorm
Copy link
Member

wooorm commented Oct 15, 2020

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

@SalahAdDin
Copy link
Contributor

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

@wooorm
Copy link
Member

wooorm commented Oct 15, 2020

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

@SalahAdDin
Copy link
Contributor

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

@wooorm
Copy link
Member

wooorm commented Oct 15, 2020

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: #470

@SalahAdDin
Copy link
Contributor

@wooorm Thanks, it is great.

@SalahAdDin
Copy link
Contributor

@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?

@ChristianMurphy
Copy link
Member

ChristianMurphy commented Apr 18, 2021

@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
Copy link
Member

Also please direct general questions to https://github.com/remarkjs/remark/discussions

@ChristianMurphy ChristianMurphy added the 🙋 no/question This does not need any changes label Apr 18, 2021
@kolyabres
Copy link

"react-markdown": "^7.1.0", does not support renderers property anymore

@ChristianMurphy
Copy link
Member

@kolyabres yes, that happened several major versions ago.
You can learn more about the change from the changelog https://github.com/remarkjs/react-markdown/blob/main/changelog.md#600---2021-04-15
If you have questions, feel free to ask in the Q&A forum https://github.com/orgs/remarkjs/discussions

@remarkjs remarkjs locked as resolved and limited conversation to collaborators Feb 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🙋 no/question This does not need any changes
Development

No branches or pull requests