question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I there a way to make img tags render as top level elements

See original GitHub issue

currently even if the markdown file has just an img, it will render as so: <p><img/></p>

Is there a way to make img’s top level block components?

so I would get from a markdown file with a couple of pargraph’s and an img in the middle the following html:

<p>some text/markdown</p>
<p>some text</p>
<img/>
<p>some text</p>

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sunknudsencommented, Feb 26, 2020

The following hack works (inspired by @coreyward’s approach).

It removes nesting altogether when a p tag has an Image child.

p: {
  component: props => {
    for (let child of props.children) {
      console.log(child)
    }
    return props.children.some(
      (child: JSX.Element) => child.type && child.type === Image
    ) ? (
      <Fragment>{props.children}</Fragment>
    ) : (
      <p {...props} />
    )
  },
},
2reactions
coreywardcommented, Jul 4, 2019

I have a similar issue, only I’m using the overrides to replace the img prop with a React component (Gatsby Image) and that’s creating invalid markup (p > div). For now I’m sidestepping the issue with this approach/hack:

<MarkdownBlock
  overrides={{
    img: MyImageComponent
    p: props => {
      return props.children.some(child => typeof child === "string") ? (
        <p {...props} />
      ) : (
        <div {...props} />
      )
    },
  }}
>
  {content}
</MarkdownBlock>

Effectively I’m checking the type of the children (bad since React expects children to be treated as opaque) and if it’s not a string render a div element. This has been working for the project I need it on, but I suspect there are more than a few pitfalls I haven’t run into yet.

It would be great to get this as an option, or for the library to expose a function to an overridden element that would let it determine the type of the parent/ancestor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

<img>: The Image Embed element - HTML - MDN Web Docs
The HTML element embeds an image into the document.
Read more >
HTML Tags Guide To Adding Images To Your Web Documents »
Using the <img> tag. The <img> element is the most straight-forward way of displaying a static image on a page. You should normally...
Read more >
The picture element - web.dev
You can specify multiple source elements inside a picture element, each one with its own srcset attribute. The browser then executes the first...
Read more >
CSS I want a div to be on top of everything - Stack Overflow
In order for z-index to work, you'll need to give the element a position:absolute or a position:relative property. Once you do that, your...
Read more >
A 4-Minute Guide to the Img src Attribute in HTML
That's why it's important to understand how the image element and source attribute work in HTML. It can come in handy when you're...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found