refactorit-logo

Ember equivalent of simple_format Rails helper

Jan 30, 2015

Here’s a rough equivalent of simple_format helper from Rails. I borrowed basic implementation from Henning Koch.

This version is written in Coffeescript, it escapes given string and returns an instance of SafeString.

Ember.Handlebars.helper 'simpleFormat', (string) ->
  string = Handlebars.Utils.escapeExpression(string)
  string = string.replace(/\r\n?/g, "\n").trim()
  if string.length > 0
    string = string.replace(/\n\n+/g, '</p><p>');
    string = string.replace(/\n/g, '<br />');
  new Ember.Handlebars.SafeString("<p>string</p>")