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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)