showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.25k stars 1.56k forks source link

How to treat single line breaks as <br>? #206

Closed ifeltsweet closed 7 years ago

ifeltsweet commented 9 years ago

How to treat single line breaks as <br>, like what GitHub does? I realise this should be an extension, does any one have any pointers though?

tivie commented 9 years ago

To create a simple line break, add 2 spaces to the end of line.

ifeltsweet commented 9 years ago

Isn't it 2 spaces?

I know that in standard markdown you place two spaces at the end of the line to create a line break. Normal line breaks are not respected unless you have two consecutive line breaks which create a paragraph. That's how markdown works.

However, GitHub flavored markdown for example respects single line breaks. How to do the same in showdown?

To illustrate:

word
word

in standard markdown would render as:

word word

but with breaks enabled it would render as

word
word

An extension that does something like the following:

return text.replace(/[ ]*\n/g, "<br />\n")

works in most cases but breaks lists for example.

This feature is supported by a few markdown parsers already, namely marked (js) and parsedown (php). The question is how do I support this in showdown. I have played around with different regular expressions but can't find the one that works and doesn't break other stuff.

ifeltsweet commented 9 years ago

Okay, so after googling around, I found that Ghost does something like this already. They use a very simplified regex that doesn't work in all cases but the most trivial ones:

text.replace(/^[\w\<\'\'][^\n]*\n+/gm, function(text) {
    return text.match(/\n{2}/)? text : text.trim() + "  \n";
})

I played around with it and extended it to support more cases and be a little bit more robust (maybe?):

{
    type: 'lang',
    filter: function(text) {
        return text.replace(/^( *(\d+\. {1,4}|[\w\<\'\">\-*+])[^\n]*)\n{1}(?!\n| *\d+\. {1,4}| *[-*+] +|$)/gm, function(text) {
            return text.trim() + "  \n";
        })
    }
}

I've tested it with a number of documents and seems to work fine, but it still needs more testing, so be careful if you copy paste.

tivie commented 9 years ago

Hey

Thanks for your contrubution.

You can also use a otp extension. Since newlines are kept in the final document, you can iterarate over all paragraphs and replace \n with br tags

ospfranco commented 7 years ago

This is very useful for simple cases, I'm surprised this is not included as basic functionality.

tivie commented 7 years ago

@ospfranco I believe that, in our effort to fully support a Github's Flavored Markdown "mode" this should be an option in core. So I'm reopening the issue and adding this feature in the near future

powerfultraveling commented 3 years ago

To someone who need this in the future, showdown.js provide some methods to configure some option now! The code below may help fix your problem:

converter.setOption('simpleLineBreaks', true);

And here is some further options for showdown: https://github.com/showdownjs/showdown

abhidadhaniya23 commented 2 years ago

If any regex will not work to replace \n then use this regex text.replace(/\\n/g, ' ')