-
-
Notifications
You must be signed in to change notification settings - Fork 893
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
Comments
I would really like to know that to if someone finds the solution.. |
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>
} |
The issue happens because you can't have div inside of a paragraph. So You may write:
Now, the paragraph becomes a div, and there is no more troubles ;). |
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 |
@EKMN and @yassinebridi code didn't work for me. Here is how I removed const imagesWithoutPTags = (props) => {
const element = props.children[0];
return element.type === 'img' ? { ...element } : <p {...props} />;
};
<ReactMarkdown
source={source}
escapeHtml={false}
renderers={{paragraph: imagesWithoutPTags}}
/> |
Thank you @magoz ! This did the trick for me :D |
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. |
Why not? |
@wooorm I don't know, I'm passing it like:
But it does not unwrap the image. |
And if you try the v5 branch? https://github.com/remarkjs/react-markdown/commits/v5 |
@wooorm, how can I install it? because it is not available right now. |
npm can install from git(hub): |
@wooorm |
whoops, that was a typo, as we’re talking about react-markdown here, it should’ve been: |
@wooorm Actually i installed it like that but it didn't work even when the package was installed. |
Hmm, I guess a 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 |
@wooorm Thanks, it is great. |
@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 no, it makes the first suggestion from @wooorm work, as you can see here https://codesandbox.io/s/react-markdown-debug-forked-75h2t |
Also please direct general questions to https://github.com/remarkjs/remark/discussions |
"react-markdown": "^7.1.0", does not support renderers property anymore |
@kolyabres yes, that happened several major versions ago. |
I am setting
imageReference
inrenderers
wich wrap img with div and I got this errorerror:
how I can fix this?
Note: It render correctly, but error shown in console
The text was updated successfully, but these errors were encountered: