Skip to content

Instantly share code, notes, and snippets.

@ultimatemonty
Created December 4, 2014 21:41
Show Gist options
  • Save ultimatemonty/9efec24377856fc3c91a to your computer and use it in GitHub Desktop.
Save ultimatemonty/9efec24377856fc3c91a to your computer and use it in GitHub Desktop.
Ember nl2br helper
Ember.Handlebars.registerBoundHelper('nl2br', function (text) {
var breakTag = '<br />';
return new Handlebars.SafeString((text + '')
.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2'));
});
@ohcibi
Copy link

ohcibi commented Mar 15, 2017

With sanitized user input and string interpolation (because)

import Ember from 'ember';

const {
  Handlebars: { Utils: { escapeExpression }},
  Helper: { helper },
  String: { htmlSafe }
} = Ember;

export function nl2br([text]) {
  var breakTag = '<br />';
  return new htmlSafe(`${escapeExpression(text)}`.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2'));
}

export default helper(nl2br);

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