rehype-react icon indicating copy to clipboard operation
rehype-react copied to clipboard

Root node is replaced by a `div` when a custom `div` is in `options.components`

Open nag opened this issue 3 years ago • 4 comments

Initial checklist

Affected packages and versions

rehype-react@latest

Link to runnable example

https://codesandbox.io/s/rehype-react-bug-szj7g

Steps to reproduce

I have included a link (you may have to reload the browser page). If we comment out line 28, the extra div will disappear.

components: {
  div: (props) => createElement("div", props) // try to remove this line
}

Note that there are no div elements in the source HTML.

Expected behavior

The div element should not be created.

Actual behavior

The div element is created.

Runtime

Node v16

Package manager

npm v7

OS

Windows

Build and bundle tools

Parcel, Next.js

nag avatar Dec 11 '21 01:12 nag

Yep. See: https://github.com/rehypejs/rehype-react/blob/f37d25d121217cb4734039d85e6051eca49d8900/lib/index.js#L61.

How is it a problem though?

wooorm avatar Dec 21 '21 15:12 wooorm

I expected the source HTML and result HTML to be the same. It looks like a side-effect to me. After all, a wrapping element is being implicitly created that shouldn't exist. Maybe there should be a Fragment in place of the div. 🤔

I was surprised to see that the root node gets passed to options.components.div and had to debug this.

nag avatar Dec 21 '21 23:12 nag

Maybe there should be a Fragment in place of the div.

The Fragment is optional. Did you see the code? The underlying project can only handle elements and text. So a div is created for the root. The code tries to change that to a fragment, but because you pass a component it’s no longer a simple div

wooorm avatar Dec 22 '21 09:12 wooorm

I get it’s unexpected, just not sure how to do anything about it

wooorm avatar Dec 22 '21 09:12 wooorm

solved in https://github.com/rehypejs/rehype-react/releases/tag/8.0.0

wooorm avatar Sep 01 '23 10:09 wooorm