Created
March 21, 2011 21:32
-
-
Save ded/880258 to your computer and use it in GitHub Desktop.
works for me
This file contains hidden or 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are 3 immediately-invoking-functions that seams useless. Inlining them would improve performance.
Also code is not DRY.