1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
autoLink = (options...) ->
pattern = ///
(^|[\s\n]|<[A-Za-z]*\/?>) # Capture the beginning of string or line or leading whitespace
(
(?:https?|ftp):// # Look for a valid URL protocol (non-captured)
[\-A-Z0-9+\u0026\u2019@#/%?=()~_|!:,.;]* # Valid URL characters (any number of times)
[\-A-Z0-9+\u0026@#/%=~()_|] # String must end in a valid URL character
)
///gi
return @replace(pattern, "$1<a href='$2'>$2</a>") unless options.length > 0
option = options[0]
linkAttributes = (
" #{k}='#{v}'" for k, v of option when k isnt 'callback'
).join('')
@replace pattern, (match, space, url) ->
link = option.callback?(url) or
"<a href='#{url}'#{linkAttributes}>#{url}</a>"
"#{space}#{link}"
String.prototype['autoLink'] = autoLink
|