-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Preserve whitespace in code blocks #226
Conversation
This pull request is automatically deployed with Now. To access deployments, click Details below or on the icon next to each push. |
I realized I could test this easily with runtime, so nvm, I think I got it. 😉 |
Ok, I managed to reproduce this in a test! Now MDX runtime also accepts |
So would moving the prism plugin to compilers fix the issue? What's the main difference between mdPlugins and this new option? |
I get lost pretty easily when working with unified because I haven't mastered it yet, but I'll continue trying to figure out where this is failing. |
I figured it out! In the process of converting to JSX there was an exception with newlines introduced by #180. I added an exception to that exception (exceptionception) and converted newlines to JSX text nodes only for code inside This works, I've been using it locally. @ifyoumakeit because you have a deeper understanding of that part of the code, could you take a look at this? |
I can definitely can take a look! |
packages/mdx/mdx-hast-to-jsx.js
Outdated
children = node.children.map(childNode => { | ||
const childOptions = { | ||
...options, | ||
preserveNewlines: preserveNewlines || node.tagName === 'pre', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I think this makes sense, as long as there's information about this default behavior somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! ⛱
I noted this in a comment, but I would just make sure this is well documented and not buried in the code.
@ifyoumakeit thanks a lot! I’ll make sure to at least add comments. @alexkrolick could you be more specific? What problem would these changes solve for you? |
Actually I might be me misinterpreting the problem. I thought this would fix the fact that newlines inside render functions are interpreted as MD blocks instead of as JS code to evaluate. You can see how the formatting in my link requires adding comments or semicolons anywhere you want a blank line. |
This PR unfortunately won't take care of that, it's for fenced code blocks only. We're aware of the issue you mentioned, though. |
The advantage of the toContain() matcher is apparent on test failure; Jest will print out the actual vs. expected, instead of only giving you the information that it failed.
In order to avoid issues like React warnings caused by text nodes in tables, we didn't convert text nodes consisting solely of one newline into text nodes. This caused some newlines inside fenced code blocks to be lost because, unlike in HTML, in React we have to explicitly create text nodes, which is important whitespace-sensitive tags like <pre>. These changes convert all newlines in <pre> tags into text nodes. Fixes #221.
Done! Polished it up a bit and added more comments to clarify what's going on. I also updated all mdx tests to use the
@johno ready when you are. 😉 |
Thanks all, this is amazing! |
@@ -97,7 +103,7 @@ it('Should render elements without wrapping blank new lines', async () => { | |||
| :--- | :---- | | |||
| Col1 | Col2 |`) | |||
|
|||
expect(result.includes('{`\n`}')).toBe(false) | |||
expect(result).not.toContain('{`\n`}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get this released? |
Now published as |
I'm trying to write a test that reproduces #221. The idea is to mount the component exported by the fixture and use
textContent
to check if whitespace in the code block is preserved.I'm having difficulties trying to load the module compiled by webpack as a React component and mounting it. Webpack gives me an object with a source text, but I'm not sure how to parse it as a Node module. If someone has an idea, let me know. 😅
P. S. I'm not 100% certain that this is really an MDX issue, but the fact that I can't reproduce it without it suggests that it is.