Skip to content

Instantly share code, notes, and snippets.

@aradnom
Created August 11, 2015 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aradnom/06ef051c1c96f10c144d to your computer and use it in GitHub Desktop.
Save aradnom/06ef051c1c96f10c144d to your computer and use it in GitHub Desktop.
Simple auto-paragraph for JavaScript
/**
* Convert text containing newlines to paragraph'd markup.
*
* @param {String} text Text to add p tags to
*
* @return {String} Returns paragraphized text
*/
function autoParagraph ( text ) {
return '<p>' + text.split( /\n+/ ).join( '</p>\n<p>' ) + '</p>';
}
/**
* Angular flavor.
*/
App.filter( 'autoParagraph', function () {
return function ( text ) {
return '<p>' + text.split( /\n+/ ).join( '</p>\n<p>' ) + '</p>';
}
});
@panbanda
Copy link

This is good for markdown stuff but may suggest something more advanced if you want: https://github.com/mavin/node-wordpress-autop/blob/master/index.js

@elhardoum
Copy link

Amazing - thanks. To transfer single line-breaks into <br/>:

'<p>' + text.split(/\n{2,}/).join( '</p><p>' ).split(/\n/).join( '<br/>' ) + '</p>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment