-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Make VFileContents generic to support processors that return objects #45
Comments
We had a conversation about this here too: unifiedjs/unified#47 (comment) |
Well, casting works as a workaround ; I'll try to do a PR that adds the type parameter on VFile without breaking existing type definitions in other libraries. From what I see in type definitions, the hardest part will be on the |
I'd be okay with exploring what generalizing Getting unified to correctly inference the type of |
I disagree about introducing generics. But the alternative sounds good to me. If remark-react were outside of remarkjs, I would say the customized VFile interface(having other content type than string or buffer) should be defined in remark-react though. Now it's in remarkjs now. So it might be nice time to update our VFile interface spec. |
remark-react / remark-vdom / rehype-react don’t have types yet. Would it be possible to define types that solve this there, compared to changing vfile? We could start using a different property for virtual DOMs as well, e.g., |
It is just putting away the problem to other place. If we do so, So, in my opinion, the easiest option should be allowing any type for contents of |
A compiler could then look like so: function rehypeReact() {
return serialize
}
function serialize(tree, file) {
// Set result somewhere different
file.vdom = toReact(tree)
// Return the current value to not change anything
return file.contents
} |
It sounds great! I think we should do like that although it causes breaking changes but much more makes sense. |
Should be start doing one 'vdom' property? If so, other naming suggestions? If not, I guess we go with namespaced keys directly on file or on file.data, e.g., like file.data.reactTree? |
|
Alternatively, this could be solved by unified: unified has a “compiler” concept, and compiling is a broader term that serializing. Serializing is also compiling, but to text. What if unified takes the result of that compiler (which could be the compilers from remark-stringify or remark-react), checks if the result is I think I like this better because plugins won’t need to update. |
@wooorm Sounds good to me. Serializing feels too limited to me. |
unified is typically, but not always, used to *serialize* when it compiles. Compilers don’t always return such a serialized (`string`, `Buffer`) though. The previous behavior was to place the result of the stringify phase when processing on `file.contents`. This doesn’t make sense for non-text. This change places non-text results on `file.result`. Closes vfile/vfile#45.
This may work.
class Example {
toString() {
return "<h1>hello world</h1>";
}
}
// where the compiler returns
new Example();
// which when used as a string
`${new Example()}`
// logs "<h1>hello world</h1>" Is this something that would be considered a
|
Related to vfile/vfile#45. Related to unifiedjs/unified#87.
Related to vfile/vfile#45. Related to unifiedjs/unified#87. Closes GH-45. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
unified is typically, but not always, used to *serialize* when it compiles. Compilers don’t always return such a serialized `string` or `Buffer` though. The previous behavior was to place the result of the stringify phase when processing on `file.contents`. This doesn’t make sense for non-text. This change places non-text results on `file.result`. Closes vfile/vfile#45. Closes GH-87.
Subject of the feature
Right now, in the type definitions, VFileContents is either a
Buffer
or astring
. In some unified processors, the VFile content is an object, which makes them impossible to typedef them correctly.Problem
For example,
remark-react
returns a ReactNode in its compiler, making it impossible to define types for it. Please note that patches would also be necessary inunified
types to support processors that modify the VFile content type.remark-react
is not the only compiler concerned by this. I'll open issues in the projects I mentioned here related to this.Expected behaviour
VFile should be a generic for its content. The default can be
Buffer | string
in order to be fully backward-compatible. With this done, we should be able to declare theremark-react
compiler output as something likeVFile<Contents = ReactNode>
.Alternatives
An alternative would be to declare the
VFileContents
asany
. It would be a lot easier to do, but we would loose a lot of strict type checking.The text was updated successfully, but these errors were encountered: