-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Test case for multiline string attribute values #15
Test case for multiline string attribute values #15
Conversation
de59d52
to
6362ce1
Compare
6362ce1
to
1594507
Compare
The code examples in the comments were missing indentation, e.g. ``` { marginTop: env(safe-area-inset-top) } ``` would be rendered as ``` { marginTop: env(safe-area-inset-top) } ``` The trimming seems to be caused by syntax-tree/mdast-util-mdx-jsx#15 Instead of waiting for an upstream fix this commit refactors the examples to remove the indentation and use CSS properties instead of JS objects.
We are dealing with ASTs here: many things don’t roundtrip exactly. That the first spaces are eaten is intentional: people want to indent their things. And that’s the solution: indent your things. |
This isn’t about indentation / formatting. This is about a JavaScript template literal whose value isn’t parsed correctly. Whitespace inside strings matters. |
Yes, it is, whitespace matters in markdown. Markdown is a whitespace sensitive language. This is markdown first. Then JavaScript. |
Not good:
Good:
|
|
Thanks for the link to https://mdxjs.com/playground/, that's a useful tool. Previously I had been using astexplorer.net (linked from https://v0.mdxjs.com/advanced/ast) as a reference but it's using mdxhast rather than mdast. I find mdxhast's parsing to match my expectation, where the start indentation of a node is used as the reference for subsequent lines https://astexplorer.net/#/gist/2befce6edce1475eb4bbec001356b222/ff4a11cb7688bd590efceae09d70064aed9e29ff Here's the code I was comparing, I used # top level
<pre children={`
0
1
2`}></pre>
# nested in blockquote
> <pre children={`
> 0
> 1
> 2`}></pre>
# nested in list in blockquote
> - <pre children={`
> 0
> 1
> 2`}></pre> |
You can configure the playground to show different things by selecting the select next to “Show”, markdown AST (mdast), HTML AST (hast), JS AST (estree). Yet. It all works the same. AST Explorer seems out of date. The term I recommend using our website: it is always up to date, and has many options. Plus, docs. This behavior is intentional; there is much code in place to handle things this way. In MDX, you can indent JSX and expressions and ESM a bit, just like other things in markdown: {
`asd
qwe
zxc`
}
<x {
...{y: `asd
qwe
zxc`} } />
<x y={
`asd
qwe
zxc`} /> Each time gives a string:
That is not quite the case. Try indenting the |
It is, we're still waiting for MDX 2 to be merged for almost 2 years Much less MDX 3 |
The code examples in the comments were missing indentation, e.g. ``` { marginTop: env(safe-area-inset-top) } ``` would be rendered as ``` { marginTop: env(safe-area-inset-top) } ``` The trimming seems to be caused by syntax-tree/mdast-util-mdx-jsx#15 Instead of waiting for an upstream fix this commit refactors the examples to remove the indentation and use CSS properties instead of JS objects.
Closing as intentional, thanks! |
This comment has been minimized.
This comment has been minimized.
The behaviour feels a bit unnatural at first, but with this explanation it makes perfect sense. This does mean the text range between the JSX attribute expression start and end offsets isn’t always a valid JavaScript expression. There are probably some related bugs in MDX analyzer. |
Initial checklist
Description of changes
The initial 2 spaces are always trimmed from multiline string values in JSX attributes. I encountered this when trying to render code in MDX by doing something like this:
What ends up happening is that the string is parsed as
\n{\nfoo: "bar"\n}
and the indentation is lost.Examples in the wild:
This PR adds a test case for it, but I'm not sure the best approach to fixing this.