Skip to content

Instantly share code, notes, and snippets.

@pennig
Forked from ded/text-to-html.js
Created March 22, 2011 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pennig/880863 to your computer and use it in GitHub Desktop.
Save pennig/880863 to your computer and use it in GitHub Desktop.
function convert(text) {
var word = /^\w/,
tag = /^\s*<(a|b|i|strong|em|cite)([ \=\'\"\w\-\/\.\:]+)*>/i,
tokens = text.split('\n'), token, src = [];
for (var i=0, len=tokens.length; i < len; i++) {
token = tokens[i];
if (word.test(token) || tag.test(token)) {
src.push('<p>' + token + '</p>');
} else {
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