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

Add support for React 19 #879

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @typedef {import('hast-util-to-jsx-runtime').ExtraProps} ExtraProps
* @typedef {import('./lib/index.js').AllowElement} AllowElement
* @typedef {import('./lib/index.js').Components} Components
* @typedef {import('./lib/index.js').ExtraProps} ExtraProps
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').UrlTransform} UrlTransform
*/
Expand Down
17 changes: 14 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @import {Element, ElementContent, Nodes, Parents, Root} from 'hast'
* @import {Components as JsxRuntimeComponents} from 'hast-util-to-jsx-runtime'
* @import {ReactElement} from 'react'
* @import {ComponentProps, ElementType, ReactElement} from 'react'
* @import {Options as RemarkRehypeOptions} from 'remark-rehype'
* @import {BuildVisitor} from 'unist-util-visit'
* @import {PluggableList} from 'unified'
Expand All @@ -21,7 +20,16 @@
*/

/**
* @typedef {Partial<JsxRuntimeComponents>} Components
* @typedef ExtraProps
* Extra fields we pass.
* @property {Element | undefined} [node]
* passed when `passNode` is on.
*/

/**
* @typedef {{
* [Key in Extract<ElementType, string>]?: ElementType<ComponentProps<Key> & ExtraProps>
* }} Components
* Map tag names to components.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this type is not equivalent to the previous Components.
But this is listed as a patch.
Is it intentional that you remove keyof JSX.IntrinsicElements?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ElementType includes JSX.IntrinsicElements.

Copy link
Member

@wooorm wooorm Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remcohaszing No, I mean keyof? Does this allow b: 'i'. I assume not, because i or all other element names are not related to props?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does! And there’s also a pre-existing test which covers this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I do not see it in the source

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renderToStaticMarkup(<Markdown children="# a" components={{h1: 'h2'}} />),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right no that, I meant in the @types/react code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like it could be a bug that @types/react supports this today — that they could remove it somewhere in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure where it happens but when I change that line to h1: 'x-y' we get a type error — which indicates that I should worry less because it is type checked — even though the types should likely allow custom elements like that.

*/

Expand Down Expand Up @@ -225,6 +233,9 @@ export function Markdown(options) {

return toJsxRuntime(hastTree, {
Fragment,
// @ts-expect-error
// React components are allowed to return numbers,
// but not according to the types in hast-util-to-jsx-runtime
Comment on lines +236 to +238
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat yes. But also apparently hast-util-to-jsx-runtime doesn’t support JSX.ElementType, which JSX runtimes may optionally define. That adds another complexity on top of that issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to solve such things instead of ts-expect-error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but that issue has been proven hard to resolve upstream. (If it can be resolved upstream at all.) This @ts-expect-error is internal anyway, and this change unblocks a lot of users.

components,
ignoreInvalidStyle: true,
jsx,
Expand Down
Loading