-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
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:
but with breaks enabled it would render as
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 |
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:
And here is some further options for showdown: |
If any regex will not work to replace |
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?The text was updated successfully, but these errors were encountered: