Pre-formatted text loses line breaks
See original GitHub issueBug Report
- I would like to work on a fix!
Current behavior
Text inside a <pre>
block is loosing formatting when the block is converted to React.createElement
function call.
Input Code
<pre>
line 1
line 2
</pre>
Expected behavior
The line breaks (and any spacing!) inside a <pre>
block should be maintained.
I would expect this:
React.createElement("pre", null, "line 1\nline 2");
Instead I get this:
React.createElement("pre", null, "line 1 line 2");
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Pre-formatted text loses line breaks #1095 - mdx-js/mdx - GitHub
Multi-line text inside a <pre> block seems to loose line breaks. Your environment. OS: Microsoft Windows [Version 10.0.19041.264]; Packages: ...
Read more >Line breaks are removed in posts made in plain text format
Method 1 - Disable the feature that removes extra line breaks · Open Outlook. · On the Tools menu, select Options. · On...
Read more >Pasting into prefotmatted box removes line breaks - Jira
Issue Summary. When copying some texts into a preformatted box, in some situations it removes the line breaks. Steps to Reproduce.
Read more >Losing line break when displaying text - Stack Overflow
Use the pre tag and the new lines will be respected. pre is for preformatted text and will use non-proportional font so it...
Read more >How to Preserve Newlines, Line Breaks, and Whitespace in ...
For example, a text from a textarea may contain line breaks or empty lines. The white-space CSS property is helpful displaying the text...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
JSX supposed to work so. For any element, all spaces including newlines are replaced by a single line. I think is was desired behaviour by design.
If you pass
<pre>{'line1\nline2'}</pre>
to Babel, it will apprear as expected.The problem here is that markdown including MDX should be treated as HTML at first and as JSX at second. Saying that I mean everything inside lowercased tags (which supposed to be HTML elements) should be parsed in HTML way preserving all the spaces.
Although, we may say that the whitespace should be preserved not for special tags but for the whole MDX since it’s an HTML syntax.
I support the idea but it brings incompatibility with common JSX behaviour when the following example will behave as HTML but would be weird according to the common JSX practices.
Treating this as HTML would render 2 buttons with a space character in between when JSX strips any spacing between the tags.
Closing this issue as it does make sense from a JSX perspective to have the current behavior.