React-markdown: Allow multiple new lines in markdown

Created on 22 Feb 2019  路  9Comments  路  Source: remarkjs/react-markdown

Is there a way to allow multiple newlines in markdown?
I am running data from draftjs to markdown, which then later can be converted to html when needed. But if people add multiple new lines, these are always removed.

馃檵 typquestion

Most helpful comment

To get multiple line breaks after another, I found that adding a &nbsp after the \n solved this. This is the magic line value.replace(/\n/gi, '\n  ')

const value = `hello
\n
\n
world
`

const source =  value.replace(/\n/gi, '\n  ');
...
<ReactMarkdown
      source={source}
      className="reactMarkDown"
    />

Cheers.

All 9 comments

Hi @rexxars , could you answer on this question, please?
Why '\n\n\n\n' doesn't replaced by <br /><br /><br /><br />?
Maybe some property 'allowMultipleBreak'?

Yeah, even the demo page is broken and doesn't let use more than one empty line https://rexxars.github.io/react-markdown/

Have any of you worked around this yet? I have the same issue and need a quick solution?

Not sure this will work for converting draftjs to markdown, but the setting that works for me is passing options to ReactMarkdown. Enabling the commonmark option allows you to add backslashes to create extra line breaks.

<ReactMarkdown parserOptions={{ commonmark: true }} source={source} />

With source set to:

Paragraph 1
\
\
Paragraph 2

To get multiple line breaks after another, I found that adding a &nbsp after the \n solved this. This is the magic line value.replace(/\n/gi, '\n &nbsp;')

const value = `hello
\n
\n
world
`

const source =  value.replace(/\n/gi, '\n &nbsp;');
...
<ReactMarkdown
      source={source}
      className="reactMarkDown"
    />

Cheers.

@azirbel Didn't work for me unfortunately.

@JulesPatry That works, it's ugly though. Seems that I've been doing nbsp hacks for over ten years now...

Would much prefer some option to preserve new lines, like the preserveNewlines option in this Draft.js markdown plugin: https://github.com/Rosey/markdown-draft-js

@azirbel worked for me! Thanks!

To get spaces between paragraphs, I tried many things but this is what worked for me, give a className to react markdown component like so , className paragraph and in your stylesheet, pick the paragraph className and style it p tags like so .paragraph p { margin-bottom: 20px; } and this gets you spaces between your paragraphs. Just so it is clear, my react markdown component with the class name is stripped out of my answer.

This is a feature of markdown. Multiple EOLs turn into multiple paragraphs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

d0lb33 picture d0lb33  路  3Comments

zhenyulin picture zhenyulin  路  3Comments

neilyoung picture neilyoung  路  3Comments

dekryptic picture dekryptic  路  3Comments

AlexLerman picture AlexLerman  路  3Comments