Skip to content

Instantly share code, notes, and snippets.

@hfwang
Last active December 13, 2015 03:58
Show Gist options
  • Save hfwang/7f0857f61ac906ca89d6 to your computer and use it in GitHub Desktop.
Save hfwang/7f0857f61ac906ca89d6 to your computer and use it in GitHub Desktop.
Javascript (and jQuery) port of Rail's simple_format
function simpleFormat(text) {
text = text.replace(/\r\n?/, "\n");
var paragraphs = text.split(/\n\n+/);
var p, b, blocks;
for (var i = paragraphs.length - 1; i >= 0; i--) {
p = $('<p />').prependTo($e);
blocks = paragraphs[i].split(/\n/);
b = [];
for (var j = 0; j < blocks.length; j++) {
b.push($('<div/>').text(blocks[j]).html());
}
p.html(b.join("\n<br />"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment