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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)