Posted almost 8 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.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 7 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