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 book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
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)