Skip to content

Instantly share code, notes, and snippets.

@navarr
Created July 21, 2013 06:06
Show Gist options
  • Save navarr/6047651 to your computer and use it in GitHub Desktop.
Save navarr/6047651 to your computer and use it in GitHub Desktop.
Convert a limited set of Markdown to HTML - specifically bold, italics, and links. This is useful in basic chat environments where you do not want block content (images, paragraphs, etc).
String.prototype.markdown2html = function () {
var text = this;
// Bold
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, "<strong>$2</strong>");
// Italics
text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, "<em>$2</em>");
// Auto-detect links and convert them to markdown
text = text.replace(/(\]\()?((https?|ftp|dict):[^'">\s]+)/gi, function($0, $1, $2) { return $1?$0:"[" + $2 + "](" + $2 + ")"});
// Inline Links
text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, '<a href="$4">$2</a>');
return text;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment