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.

Single line break should not be converted to paragraph

See original GitHub issue

Hello, I’ve just installed this converter and I’ve found an issue. How do I prevent single line breaks to being turned into new paragraphs?

Right now writing

Example Text\n
Second Line

gets turned into

<p>Example Text</p><p>Second Line</p>

whereas the result I expect should be

<p>Example Text<br>Second Line</p>

Two consecutive line breaks instead should be turned into a new paragraph.

Any ideas on how I could achieve this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:5

github_iconTop GitHub Comments

15reactions
jonzlin95commented, Aug 1, 2017

This should be the default behaviour. However DraftJS out of the box doesn’t ship with
handling of newlines like you’d expect. The default is a new paragraph (which mirrors Microsoft Word etc.). Try this in your Editor

  handleReturn(e) {
    const { editorState } = this.state;
    if (e.shiftKey) {
      this.setState({ editorState: RichUtils.insertSoftNewline(editorState) });
      return 'handled';
    }
    return 'not-handled';
  }

<Editor
    handleReturn={this.handleReturn}
/>
1reaction
bengotowcommented, Jan 5, 2018

Hey — thanks, that makes a lot of sense. I’m using DraftJS-convert in the Mailspring email client so I probably have unusual goals—basically need to get HTML that exactly visually matches what the user typed so the editor seems WYSWIG. Here’s the config I ended up using which (at least so far) seems to maintain whitespace reliably and still be read back into DraftJS correctly via convertFromHTML:

    blockToHTML: block => {
      switch (block.type) {
        case 'unstyled':
          if (block.text === ' ' || block.text === '') return <br />;
          return <div />;
        case 'paragraph':
          return <p />;
        case 'header-one':
          return <h1 />;
....
image
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Secret To Converting Paragraph Breaks Into Line Breaks ...
Convert Paragraph (Or Line Break) Lists To A Comma Separated List · Use CTRL + H to bring up the Find and Replace...
Read more >
Is it possible to convert paragraph breaks in Word to line ...
With the code as a single paragraph I only need to set the style on any point for it all to be covered,...
Read more >
Converting new lines to paragraph/br HTML tags, can this be ...
First, replace the case when there are two line breaks. to = to.replace(/\n{2}/g, ' </p><p> ...
Read more >
The Dos and Don'ts of Adding an HTML Line Break
The HTML line break element can be used to display poems, song lyrics, ... The first uses one paragraph element and multiple line...
Read more >
Understanding paragraphs and line breaks in WordPress
Hitting the Enter (Return) key will end the paragraph you are writing and start a new one. If you want to start a...
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