Showdown: How to treat single line breaks as <br>?

Created on 7 Oct 2015  路  6Comments  路  Source: showdownjs/showdown

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?

Most helpful comment

@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

All 6 comments

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

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

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.

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.

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

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

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oscarmorrison picture oscarmorrison  路  4Comments

BusyHe picture BusyHe  路  4Comments

Ehesp picture Ehesp  路  3Comments

DesignResponds picture DesignResponds  路  3Comments

savsharma2 picture savsharma2  路  7Comments