Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ifeltsweet opened this issue Oct 7, 2015 · 8 comments
Closed

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

ifeltsweet opened this issue Oct 7, 2015 · 8 comments
Assignees

Comments

@ifeltsweet
Copy link

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
Copy link
Member

tivie commented Oct 8, 2015

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

@ifeltsweet
Copy link
Author

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
Copy link
Author

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
Copy link
Member

tivie commented Oct 9, 2015

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
Copy link

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

@tivie tivie self-assigned this Nov 30, 2016
@tivie
Copy link
Member

tivie commented Nov 30, 2016

@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

@tivie tivie reopened this Nov 30, 2016
@tivie tivie closed this as completed in 0942b5e Dec 1, 2016
@powerfultraveling
Copy link

powerfultraveling commented Aug 24, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants