Created
August 8, 2012 23:14
-
-
Save mgeraci/3299637 to your computer and use it in GitHub Desktop.
better coffeescript url detection
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
| # regex is from http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/ | |
| exp = /// | |
| \b(?:(?:https?|ftp|file)://|www\.|ftp\.) | |
| (?:\([-A-Z0-9+&@\#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@\#/%=~_|$?!:,.])* | |
| (?:\([-A-Z0-9+&@\#/%=~_|$?!:,.]*\)|[A-Z0-9+&@\#/%=~_|$]) | |
| ///ig | |
| for url in $(this).text().match exp | |
| # add a protocol if it just starts with www | |
| url_with_protocol = "http://#{url}" if url.match(/^www.+/) | |
| visual_url = url.split('').join('​').replace(/(^.{300}).+/, '$1...') | |
| $(this).html $(this).html().replace(url, "<a href='#{url_with_protocol}'>#{visual_url}</a>") | |
| $(this).addClass('autolinked') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ABSOLUTELY. @daxter, that's a much better loop. Thanks!