Read more

simple_format helper for Javascript

Henning Koch
July 25, 2011Software engineer at makandra GmbH

The Javascript code below is a rough equivalent to the simple_format Show archive.org snapshot helper that ships with Rails:

function simpleFormat(str) {
  str = str.replace(/\r\n?/, "\n");
  str = $.trim(str);
  if (str.length > 0) {
    str = str.replace(/\n\n+/g, '</p><p>');
    str = str.replace(/\n/g, '<br />');
    str = '<p>' + str + '</p>';
  }
  return str;
}
Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

Unlike the Rails helper, this does not preserve whitespace. You probably don't care.

Posted by Henning Koch to makandra dev (2011-07-25 11:03)