Posted over 7 years ago. Visible to the public.

simple_format helper for Javascript

The Javascript code below is a rough equivalent to the simple_format helper that ships with Rails:

Copy
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; }

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

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 6 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more