You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+70-43Lines changed: 70 additions & 43 deletions
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,24 @@
1
1
# Autolinker.js
2
2
3
-
Because I had so much trouble finding a good autolinking implementation out in the wild, I decided to roll my own. It
4
-
seemed that everything I found out there was either an implementation that didn't cover every case, or was just limited
5
-
in one way or another.
3
+
Because I had so much trouble finding a good auto-linking implementation out in
4
+
the wild, I decided to roll my own. It seemed that everything I found out there
5
+
was either an implementation that didn't cover every case, or was just limited
6
+
in one way or another.
6
7
7
8
So, this utility attempts to handle everything. It:
8
9
9
-
- Autolinks URLs, whether or not they start with the protocol (i.e. 'http://'). In other words, it will automatically link the
10
-
text "google.com", as well as "http://google.com".
10
+
- Autolinks URLs, whether or not they start with the protocol (i.e. 'http://').
11
+
In other words, it will automatically link the text "google.com", as well as
12
+
"http://google.com".
11
13
- Will properly handle URLs with special characters
12
14
- Will properly handle URLs with query parameters or a named anchor (i.e. hash)
13
15
- Will autolink email addresses.
16
+
- Will autolink phone numbers.
14
17
- Will autolink Twitter handles.
15
-
- Will properly handle HTML input. The utility will not change the `href` attribute inside anchor (<a>) tags (or any other
16
-
tag/attribute for that matter), and will not accidentally wrap the inner text of an anchor tag with a new one (which would cause
17
-
doubly-nested anchor tags).
18
+
- Will properly handle HTML input. The utility will not change the `href`
19
+
attribute inside anchor (<a>) tags (or any other tag/attribute for that
20
+
matter), and will not accidentally wrap the inner text of an anchor tag with a
21
+
new one (which would cause doubly-nested anchor tags).
18
22
19
23
Hope that this utility helps you as well!
20
24
@@ -23,7 +27,8 @@ Hope that this utility helps you as well!
23
27
24
28
#### Download
25
29
26
-
Simply clone or download the zip of the project, and link to either `dist/Autolinker.js` or `dist/Autolinker.min.js` with a script tag:
30
+
Simply clone or download the zip of the project, and link to either
31
+
`dist/Autolinker.js` or `dist/Autolinker.min.js` with a script tag:
27
32
28
33
```html
29
34
<scriptsrc="path/to/Autolinker.min.js"></script>
@@ -49,14 +54,15 @@ JavaScript:
49
54
50
55
```javascript
51
56
var Autolinker =require( 'autolinker' );
52
-
// note: npm wants an all-lowercase package name, but the utility is a class and should be
53
-
// aliased with a capital letter
57
+
// note: npm wants an all-lowercase package name, but the utility is a class and
58
+
//should be aliased with a capital letter
54
59
```
55
60
56
61
57
62
## Usage
58
63
59
-
Using the static [link()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-static-method-link) method:
64
+
Using the static [link()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-static-method-link)
65
+
method:
60
66
61
67
```javascript
62
68
var linkedText =Autolinker.link( textToAutolink[, options] );
@@ -70,10 +76,12 @@ var autolinker = new Autolinker( [ options ] );
70
76
var linkedText =autolinker.link( textToAutoLink );
71
77
```
72
78
73
-
Note: if using the same options to autolink multiple pieces of html/text, it is slightly more efficient to create a single
74
-
Autolinker instance, and run the [link()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-method-link) method repeatedly (i.e. use the "class" form above).
79
+
Note: if using the same options to autolink multiple pieces of html/text, it is
80
+
slightly more efficient to create a single Autolinker instance, and run the
method repeatedly (i.e. use the "class" form above).
83
+
75
84
76
-
77
85
#### Example:
78
86
79
87
```javascript
@@ -83,7 +91,8 @@ var linkedText = Autolinker.link( "Check out google.com", { className: "myLink"
83
91
84
92
## Options
85
93
86
-
These are the options which may be specified for linking. These are specified by providing an Object as the second parameter to [Autolinker.link()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-static-method-link). These include:
94
+
These are the options which may be specified for linking. These are specified by
95
+
providing an Object as the second parameter to [Autolinker.link()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-static-method-link). These include:
Using the same pre-configured [Autolinker](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker) instance in multiple locations of a codebase (usually by dependency injection):
150
+
Using the same pre-configured [Autolinker](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker)
151
+
instance in multiple locations of a codebase (usually by dependency injection):
139
152
140
153
```javascript
141
154
var autolinker =newAutolinker( { newWindow:false, truncate:25 } );
@@ -155,8 +168,9 @@ autolinker.link( "Go to www.google.com" );
155
168
156
169
## Custom Replacement Function
157
170
158
-
A custom replacement function ([replaceFn](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-replaceFn)) may be provided to replace url/email/twitter matches on an individual basis, based
159
-
on the return from this function.
171
+
A custom replacement function ([replaceFn](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-replaceFn))
172
+
may be provided to replace url/email/twitter matches on an individual basis,
173
+
based on the return from this function.
160
174
161
175
Full example, for purposes of documenting the API:
162
176
@@ -167,36 +181,42 @@ var linkedText = Autolinker.link( input, {
@@ -206,23 +226,30 @@ var linkedText = Autolinker.link( input, {
206
226
207
227
The function is provided two arguments:
208
228
209
-
1. The Autolinker instance that is performing replacements. This can be used to query the options that the Autolinker
210
-
instance is configured with, or to retrieve its TagBuilder instance (via [autolinker.getTagBuilder()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-method-getTagBuilder)).
211
-
2. An [Autolinker.match.Match](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker.match.Match) object which details the match that is to be replaced.
229
+
1. The Autolinker instance that is performing replacements. This can be used to
230
+
query the options that the Autolinker instance is configured with, or to
231
+
retrieve its TagBuilder instance (via [autolinker.getTagBuilder()](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-method-getTagBuilder)).
232
+
2. An [Autolinker.match.Match](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker.match.Match)
233
+
object which details the match that is to be replaced.
234
+
212
235
236
+
A replacement of the match is made based on the return value of the function.
237
+
The following return values may be provided:
213
238
214
-
A replacement of the match is made based on the return value of the function. The following return values may be provided:
215
-
216
-
- No return value (`undefined`), or `true` (Boolean): Delegate back to Autolinker to replace the match as it normally would.
239
+
- No return value (`undefined`), or `true` (Boolean): Delegate back to
240
+
Autolinker to replace the match as it normally would.
217
241
-`false` (Boolean): Do not replace the current match at all - leave as-is.
218
-
- Any String: If a string is returned from the function, the string will be used directly as the replacement HTML for
219
-
the match.
220
-
- An [Autolinker.HtmlTag](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker.HtmlTag) instance, which can be used to build/modify an HTML tag before writing out its HTML text.
242
+
- Any String: If a string is returned from the function, the string will be used
243
+
directly as the replacement HTML for the match.
244
+
- An [Autolinker.HtmlTag](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker.HtmlTag)
245
+
instance, which can be used to build/modify an HTML tag before writing out its
246
+
HTML text.
221
247
222
248
223
249
## Full API Docs
224
250
225
-
The full API docs for Autolinker may be referenced at: [http://gregjacobs.github.io/Autolinker.js/docs/](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker)
251
+
The full API docs for Autolinker may be referenced at:
0 commit comments