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
Allow u(type, props, ...children) form of API #10
Comments
Perhaps better to add support for the modern automatic runtime, like https://github.com/syntax-tree/hastscript#jsx and xastscript? |
Can we do both? Hastscript also supports both the |
I’d prefer only an automatic JSX runtime. I didn’t really like it myself when they announced it, but with my MDX work, I now really really prefer it over the classic runtime. Main important points for me:
If those are met, I’m 👍 |
I don’t see any problem breaking changes when adding support to a classic runtime. Currently the API accepts a string or an array of objects as its third argument. assert.deepEqual(
<root>
string
</root>,
{ type: "root", value: "string" }
)
assert.deepEqual(
<root>
<child />
</root>,
{ type: "root", children: [ { type: 'child' } ] }
)
assert.deepEqual(
<root>
<child />
<child />
</root>,
{ type: "root", children: [ { type: 'child' }, { type: 'child' } ] }
)
assert.throws(
() => (
<root>
string
<child />
</root>
),
'Expected either a string or an array of nodes, got invalid array'
) |
Should fragments get a special meaning? |
JSX in the classic runtime only accepts children (
unist accepts anything. For JSX, everything is a parent, and text nodes are a certain thing. There’s a mismatch there that can’t be solved without domain knowledge. That‘s why hast/mdast/xast/etc can work well with JSX, but I don’t think this can. |
You’re right, my bad. Still this seems fine? u('root', null, 'value')
assert.deepEqual(
u('root', null, u('child', null), u('child', null)),
u('root', null, [u('child', null), u('child', null)])
)
assert.throws(() => {
u('root', null, 'value', 'value?')
}, 'Unexpected arguments, one string value is allowed, got: "value?"')
I don’t see any problems here. One could also pass plain values when writing plain JavaScript. This can be checked both at runtime and using TypeScript.
Isn’t this already supported? From the readme: u('void', {id: 4})
I think it could work. I definitely see the similarities between JSX and unist. Yes, JSX has text nodes, but that doesn’t mean should allow them. We could disallow them using TypeScript, even though a single string child would still technically work. I’m not sure it should be supported though. I like that it’s supported by |
It could be fine. But, according to unist there are three shapes of nodes:
Users will try to pass
A root that has a |
I think JSX is a bad idea at this abstraction (see previous comment). As for accepting a node instead of the current array of nodes or the value, that would get complex if props are also omitted, because a node matches the props interface, however, we can be sure that a node has a u('element', {type: 'text', value: 'whatever'}) Accepting const children = []
u('element', {whatever: true}, ...children) // {type: 'element', whatever: true}
u('element', ...children) // {type: 'element'} As it’s all so ambiguous, so many edge cases that an author needs to be aware of, I’m inclined to just not do it. What do you think @Josh-Cena? Haven‘t heard from you in a while :) |
Ok closing, I’m not really sure about the trade offs! |
Initial checklist
Problem
I'm probably doing something cursed, but I'm trying to see if it's able to write unists in JSX. I reproduced the example in the README as:
And using
u
as the JSX factory. Unfortunately, this didn't work, becauseu
only accepts an array of children. In order to make this work, I have to use a single array as children everywhere.Solution
I propose to make this valid:
Because JSX always compiles to
type, props, ...children
, whenever there are more than three arguments, the rest must all be children, and we can simply collect them into an array.Alternatives
Do not use JSX for unist? 😄
I'm really curious if I'm able to make it work, though
The text was updated successfully, but these errors were encountered: