Reputation: 11327
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.
Upvotes: 2
Views: 2309
Reputation: 1483
Maybe helpful for future readers. Showdown now has an option to do that.
converter.setOption('simpleLineBreaks', true);
Upvotes: 5
Reputation: 1811
Thanks for this great extension. It was very helpful for one of my projects.
It looks like this way of defining extensions is deprecated now. Since they didn't update the documentation yet I used the prettify extension as a template to update the extension you wrote to the new way of writing extensions in showdown:
(function (extension) {
'use strict';
if (typeof showdown !== 'undefined') {
extension(showdown);
} else if (typeof define === 'function' && define.amd) {
define(['showdown'], extension);
} else if (typeof exports === 'object') {
module.exports = extension(require('showdown'));
} else {
throw Error('Could not find showdown library');
}
}(function(showdown){
'use strict';
showdown.extension('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";
});
}
}];
})
}));
Upvotes: 3
Reputation: 11327
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.
Upvotes: 4