-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathautolink.coffee
More file actions
26 lines (20 loc) · 817 Bytes
/
autolink.coffee
File metadata and controls
26 lines (20 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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]
callback = option["callback"]
linkAttributes = (
" #{k}='#{v}'" for k, v of option when k isnt 'callback'
).join('')
@replace pattern, (match, space, url) ->
link = callback?(url) or
"<a href='#{url}'#{linkAttributes}>#{url}</a>"
"#{space}#{link}"
String.prototype['autoLink'] = autoLink