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.

Empty p tags is being generated

See original GitHub issue

I read that rich text would remove the unnecessary empty <p></p> tags generated from the text editor. But for some reason, I’m still getting p tags.

Here’s what my sample code looks like

const options = {
  renderNode: {
    [BLOCKS.HEADING_1]: (node, children) => <h1>{children}</h1>,
    [BLOCKS.HEADING_5]: (node, children) => <h5>{children}</h5>,
    [BLOCKS.EMBEDDED_ASSET]: (node) => {
      const fileUrl = node.data.target.fields.file['en-US'].url

      return (
        <ContentfulFixedAsset
           fileUrl={fileUrl}
        />
      )
    }
  }
}

documentToReactComponents(richText, options)

What am i doing wrong?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:16

github_iconTop GitHub Comments

41reactions
coreysymscommented, Nov 14, 2019

Honestly the easiest work-around is CSS, and what I use is simple. CSS: p:empty { display: none; } Any paragraph tag that is empty, hide it. You can use this universally like the above. Or more specifically wrap your displayed Contentful richtext entries in a div classed as such.

.richtext > p:empty { display:none; }

This can be problematic for clients who like to add vertical space with p tags but it’s better than having my layout wrecked by either the content editor or the CMS.

This is a bit of a nuisance problem, so I understand why it’s not high on the priorities, hope the above workaround helps!

9reactions
Justbigmackcommented, Jan 27, 2022

Wondering how this has not been fixed with the issue being open since 2019

Read more comments on GitHub >

github_iconTop Results From Across the Web

An empty <p> tag is automatically getting inserted, how do I ...
Here is the code which renders the HTML block: The <p></p> tag gets generated dynamically, but not sure, how it is getting generated...
Read more >
Wrapping drupal-media image in a link produces empty P tags ...
There's an issue when linking a media item in the editor whereby it adds empty P tags. It seems to happen when the...
Read more >
How to Remove Empty Paragraph Tags from Shortcodes in ...
Easily learn how to remove empty paragraph (p) tags from shortcodes in WordPress with this simple code snippet. The fix takes seconds!
Read more >
Empty P tags. Grr. | ExpressionEngine 2 Tech Support
I'm not sure I'm being clear… here's essentially what I'm entering into the XHTML-ized field: some text that gets wrapped in a P...
Read more >
Showing empty p tags in views on the front end. - Toolset
This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete....
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