Created
August 11, 2015 17:41
-
-
Save aradnom/06ef051c1c96f10c144d to your computer and use it in GitHub Desktop.
Simple auto-paragraph for JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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>'; | |
} | |
}); |
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
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