How to have GitHub style treating of newlines using showdown?
I have tried an extension that does something like:
return text.replace(/[ ]*\n/g, "<br />\n")
It works in some cases but breaks lists for example.
Okay so I came up with extension which does that.
/**
* Showdown extension for GFM newlines.
*
* In very clear cases, let newlines become <br/> tags.
*
* This implementation is adopted from showdown-ghost.
*
*/
(function () {
var newline = function () {
return [{
type: 'lang',
filter: function(text) {
return text.replace(/^( *(\d+\. {1,4}|[\w\<\'\">\-*+])[^\n]*)\n{1}(?!\n| *\d+\. {1,4}| *[-*+] +|#|$)/gm, function(e) {
return e.trim() + " \n";
})
}
}];
};
if (window.showdown) {
window.showdown.extensions.newline = newline;
}
})();
This is working great for me, although the regex is not 100% tested and will probably fail in some rare cases, so consider yourself warned.