-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
I originally brought this up in #464 (comment) and again in system-ui/theme-ui#212 (comment), where it was suggested that I open a new issue. Here's the pitch:
Fenced code blocks should be able to specify "first-class" JSX props that get passed down to the MDX tag. Currently, everything after the language identifier is passed along via the metastring
prop, which is then passed as key/value strings, so this:
```js live foo=bar
```
...produces a remark code
node with properties: {live: true, foo: 'bar'}
, which is great! However, the key/value parser doesn't support anything with strings, such as foo="bar baz"
. When you test this in the MDX playground:
```jsx live foo=1 bar="baz 2"
```
...it produces a node with properties: {live: true, foo: '1', bar": '"baz', '2"': true}
, which is clearly not right. Rather than inventing a new parser for this, I think it would be best to just treat the metastring as JSX props and either parse them "properly" (i.e. with Babel) at build time or inline them directly in the resulting JSX during the HAST → JSX compilation phase. For instance, I would expect the above example to compile to:
<MDXTag tag="pre" className="language-jsx" live foo={1} bar="baz 2" />
Or, to prevent breakage of existing components, pass them in via a separate prop:
<MDXTag tag="pre" className="language-jsx"
meta={{live: true, foo: '1', bar: 'baz 2'}} />
As a proof of concept, I've hacked together remark-fenced-props to show how at least the remark-level transformation of metastring
→ props
could work. It's very much not production-ready or backward compatible with the key/value pair syntax, but it could easily be made so with a replacement like:
metastring.replace(/(\w+)=([^ ]+)/g, (_, key, value) => {
return isNaN(value) ? `${key}="${value}"` : `${key}={${value}}`
})
Anyway, lmk what you think!