Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Any reason why the root element has to be a <div>? #54

Closed
domharrington opened this issue Jun 26, 2018 · 2 comments
Closed

Any reason why the root element has to be a <div>? #54

domharrington opened this issue Jun 26, 2018 · 2 comments
Labels
🙋 no/question This does not need any changes

Comments

@domharrington
Copy link

domharrington commented Jun 26, 2018

I'm refactoring some of our code to use this module, instead of dangerouslySetInnerHtml using marked. Marked appears to have a root element of a <p> tag, this module has a <div> that everything gets wrapped in.

This is causing some issues in our tests because previously we were inserting markdown content inside of a <p> tag. A <p> inside of a <p> is a valid descendant. A <div> inside of a <p> results in this react error:

Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>.

Would you be open to accepting a pr which makes the root tag customisable?

domharrington pushed a commit to readmeio/api-explorer that referenced this issue Jun 26, 2018
@domharrington
Copy link
Author

I managed to work around this by passing in the following to remarkReactComponents:

remark()
  .use(react, {
    remarkReactComponents: {
      div: function(props) {
        return React.createElement(React.Fragment, props);
      },
    },
  });

This seems to do the trick, and I'm not sure whenever else a <div> would be constructed from markdown?

@davidtheclark
Copy link
Collaborator

Hi @domharrington.

Hm, <p> elements are also not supposed to be nested within <p> elements. I guess React doesn't warn against that even though the HTML validator does.

I'd be open to a PR that uses Fragment instead of div if Fragment is available (don't want to break things for React <16). That seems like the right approach 👍

@wooorm wooorm closed this as completed in 860d56d Nov 25, 2018
@wooorm wooorm added the 🙋 no/question This does not need any changes label Aug 14, 2019
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

3 participants