question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-formatted text loses line breaks

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
viktor-yakubivcommented, Jun 5, 2020

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.

import Button from './button'

<Button>Action 1</Button>
<Button>Action 2</Button>

Treating this as HTML would render 2 buttons with a space character in between when JSX strips any spacing between the tags.

0reactions
pedrolamascommented, Jun 5, 2020

Closing this issue as it does make sense from a JSX perspective to have the current behavior.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found