Skip to content

Instantly share code, notes, and snippets.

@habovh
Forked from kensnyder/angular.filters.nl2br.js
Last active July 22, 2016 18:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habovh/3dcb42d18975375d5247 to your computer and use it in GitHub Desktop.
Save habovh/3dcb42d18975375d5247 to your computer and use it in GitHub Desktop.
Safe NL2BR filter for Angular, compatible with linky. Transforms newlines ( \n) into HTML <br /> tags.
# nl2br linky-compatible Angular filter
# CoffeeScript Version (available in JS)
#
# Sample usage in html template:
# "xxx | nl2br"
# <div ng-bind-html=" YourString | nl2br "></div>
angular.module('nl2brFilter', []).filter('nl2br', ['$sanitize', ($sanitize) ->
tag = '<br />'
return (msg) ->
# ngSanitize's linky filter changes \r and \n to &#10; and &#13; respectively
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n|&#10;&#13;|&#13;&#10;|&#10;|&#13;)/g, tag + '$1')
return $sanitize(msg)
])
// nl2br linky-compatible Angular filter
// JS Version (available in CoffeeScript)
//
// Sample usage in html template:
// "xxx | nl2br"
// <div ng-bind-html=" YourString | nl2br "></div>
angular.module('myModule')
.filter('nl2br', ['$sanitize', function($sanitize) {
var tag = '<br />';
return function(msg) {
// ngSanitize's linky filter changes \r and \n to &#10; and &#13; respectively
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n|&#10;&#13;|&#13;&#10;|&#10;|&#13;)/g, tag + '$1');
return $sanitize(msg);
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment