Skip to content

Instantly share code, notes, and snippets.

@ded
Created March 21, 2011 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ded/880258 to your computer and use it in GitHub Desktop.
Save ded/880258 to your computer and use it in GitHub Desktop.
works for me
function convert(text) {
var tokens = text.split('\n'), i, token, src = [];
for (var i=0; i < tokens.length; i++) {
var token = tokens[i];
token && token.charAt(0).match(/\w/) ? (function () {
src.push('<p>' + token + '</p>');
}()) : (function () {
token && token.match(/^\s*<(a|b|i|strong|em|cite)([ \=\'\"\w\-\/\.\:]+)*>/i) ?
(function() {
src.push('<p>' + token + '</p>');
}()) :
src.push(token);
}());
}
text = src.join('\n');
var r = /<pre><code>([\s\S]+?)(?=<\/code><\/pre>)/g;
text = text.replace(r, function (m, code) {
return '<pre><code>' + code.replace(/<p>([\s\S]+?)<\/p>/g, '$1');
});
return text;
}
@rkatic
Copy link

rkatic commented Mar 22, 2011

There are 3 immediately-invoking-functions that seams useless. Inlining them would improve performance.
Also code is not DRY.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment