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

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 no/question This does not need any changes
Development

No branches or pull requests

3 participants