remarkjs / remark-rehype

plugin that turns markdown into HTML to support rehype
https://remark.js.org
MIT License
258 stars 18 forks source link

Fix plugin output type #31

Closed andrejpavlovic closed 11 months ago

andrejpavlovic commented 1 year ago

Initial checklist

Description of changes

Current

Plugin output type defaults to the input type which is HastRoot

Expected

Plugin output type should be MdastRoot which corresponds to the toHast return type.

Change

Plugin output type set to HastRoot instead of defaulting to the input type MdastRoot.

wooorm commented 1 year ago

Hi!

You are mostly correct. But there is a comment above this, introduced here: https://github.com/remarkjs/remark-rehype/commit/c6a4c646135c87c73f2a31fed4b5501c8dc27185, that mentions it doesn’t work.

There is also the case where the current types are as true as your types: this plugin can be used in bridge mode and in mutate mode. Mutate mode would be typed with your proposal, whereas the current types represent bridge mode.

I don’t know exactly why I wrote that comment anymore. My guess is that TypeScript sometimes bails at complex types, resulting in nevers.

In the inverse plugin, both uses are typed. Perhaps that’s of interest: https://github.com/rehypejs/rehype-remark/commit/35fda3efcbbd823826eefacfb8019f2dc4741862. Could you check if that type, inverted, works here too?

remcohaszing commented 1 year ago

I believe it was related to the unified types. TypeScript 5 adds support for @overload, maybe that helps.

smackesey commented 1 year ago

Just ran into this issue and got confused, then a bit more confused reading this PR. @andrejpavlovic is your PR description right? I think HastRoot and MdastRoot should be switched in the "Current" and "Expected" sections?

Also is this PR moot in light of existing overload annotations that are either wrong or that typescript is choking on?

wooorm commented 11 months ago

I checked again and it doesn’t seem possible for TS to understand the overloads yet.

I made sure that 99% of the use case of this plugin works: https://github.com/remarkjs/remark-rehype/blob/b4799b580f80a3dc6edee2c9d6c6144c05ff0685/test.js#L100-L103. I think that’s what you are trying to solve here: mdast -> hast.

For the other 1% of cases, I don’t know. But the solution would likely be in the space of fixing the unified types for use, if it can even be solved there, and otherwise for TS itself.

I’ll close this, I believe I did what you were looking for with https://github.com/remarkjs/remark-rehype/releases/tag/11.0.0. The trouble with overloads will have to be solved somewhere else!

github-actions[bot] commented 11 months ago

Hi! This was closed. Team: If this was merged, please describe when this is likely to be released. Otherwise, please add one of the no/* labels.