-
-
Save multiplegeorges/4347009 to your computer and use it in GitHub Desktop.
Handlebars helper to replicate the functionality of Rails' simple_format view helper.
Wraps everything in a paragraph and corrects newlines and carriage returns. Ignores all other HTML.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handlebars.registerHelper 'simple_format', (text) -> | |
carriage_returns = /\r\n?/g | |
paragraphs = /\n\n+/g | |
newline = /([^\n]\n)(?=[^\n])/g | |
text = text.replace(carriage_returns, "\n") # \r\n and \r -> \n | |
text = text.replace(paragraphs, "</p>\n\n<p>") # 2+ newline -> paragraph | |
text = text.replace(newline, "$1<br/>") # 1 newline -> br | |
text = "<p>" + text + "</p>"; | |
new Handlebars.SafeString text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saw this while googling for the exact same thing. Hope you're doing well, man!