Skip to content

Commit d89f35d

Browse files
author
Heng
committed
Autolink mentions for Instagram and Twitter
- Deprecated `twitter` option - Added `mention` option with possible values `twitter`, `instagram` - `mention` option will take precedence over `twitter` option - (breaking) removed Twitter match and matcher objects. Code that checks `matcher.getType() == 'twitter'` will need to be changed to check `matcher.getType() == 'mention' && matcher.getServiceName() == 'twitter'`
1 parent f8b1ff3 commit d89f35d

18 files changed

+608
-244
lines changed

README.md

+36-30
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ So, this utility attempts to handle everything. It:
1414
- Will properly handle URLs with query parameters or a named anchor (i.e. hash)
1515
- Will autolink email addresses.
1616
- Will autolink phone numbers.
17-
- Will autolink Twitter handles.
17+
- Will autolink mentions (Twitter, Instagram).
1818
- Will autolink hashtags.
1919
- Will properly handle HTML input. The utility will not change the `href`
2020
attribute inside anchor (<a>) tags (or any other tag/attribute for that
@@ -109,66 +109,71 @@ providing an Object as the second parameter to [Autolinker.link()](http://gregja
109109
hashtags should be truncated to inside the text of a link. If the match is
110110
over the number of characters, it will be truncated to this length by
111111
replacing the end of the string with a two period ellipsis ('..').<br /><br />
112-
112+
113113
Example: a url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated
114114
to 25 characters may look like this: 'yahoo.com/some/long/pat..'<br /><br />
115-
116-
In the object form, both `length` and `location` may be specified to perform
117-
truncation. Available options for `location` are: 'end' (default), 'middle',
118-
or 'smart'. Example usage:
119-
115+
116+
In the object form, both `length` and `location` may be specified to perform
117+
truncation. Available options for `location` are: 'end' (default), 'middle',
118+
or 'smart'. Example usage:
119+
120120
```javascript
121121
truncate: { length: 32, location: 'middle' }
122122
```
123-
124-
The 'smart' truncation option is for URLs where the algorithm attempts to
125-
strip out unnecessary parts of the URL (such as the 'www.', then URL scheme,
126-
hash, etc.) before trying to find a good point to insert the ellipsis if it is
127-
still too long. For details, see source code of:
123+
124+
The 'smart' truncation option is for URLs where the algorithm attempts to
125+
strip out unnecessary parts of the URL (such as the 'www.', then URL scheme,
126+
hash, etc.) before trying to find a good point to insert the ellipsis if it is
127+
still too long. For details, see source code of:
128128
[TruncateSmart](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker.truncate.TruncateSmart)
129129
- [className](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-className) : String<br />
130130
A CSS class name to add to the generated anchor tags. This class will be added
131-
to all links, as well as this class plus "url"/"email"/"phone"/"twitter"/"hashtag"
132-
suffixes for styling url/email/phone/twitter/hashtag links differently.
131+
to all links, as well as this class plus "url"/"email"/"phone"/"hashtag"/"mention"/"twitter"/"instagram"
132+
suffixes for styling url/email/phone/hashtag/mention links differently.
133133

134134
For example, if this config is provided as "myLink", then:
135135

136136
1) URL links will have the CSS classes: "myLink myLink-url"<br />
137137
2) Email links will have the CSS classes: "myLink myLink-email"<br />
138138
3) Phone links will have the CSS classes: "myLink myLink-phone"<br />
139-
4) Twitter links will have the CSS classes: "myLink myLink-twitter"<br />
139+
4) Twitter links will have the CSS classes: "myLink myLink-mention myLink-twitter"<br />
140+
5) Instagram links will have the CSS classes: "myLink myLink-mention myLink-instagram"<br />
140141
5) Hashtag links will have the CSS classes: "myLink myLink-hashtag"<br />
141142

142143
- [urls](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-urls) : Boolean/Object<br />
143144
`true` to have URLs auto-linked, `false` to skip auto-linking of URLs.
144145
Defaults to `true`.<br>
145-
146+
146147
This option also accepts an Object form with 3 properties, to allow for more
147148
customization of what exactly gets linked. All default to `true`:
148-
149+
149150
- schemeMatches (Boolean): `true` to match URLs found prefixed with a scheme,
150151
i.e. `http://google.com`, or `other+scheme://google.com`, `false` to
151152
prevent these types of matches.
152153
- wwwMatches (Boolean): `true` to match urls found prefixed with `'www.'`,
153-
i.e. `www.google.com`. `false` to prevent these types of matches. Note
154-
that if the URL had a prefixed scheme, and `schemeMatches` is true, it
154+
i.e. `www.google.com`. `false` to prevent these types of matches. Note
155+
that if the URL had a prefixed scheme, and `schemeMatches` is true, it
155156
will still be linked.
156157
- tldMatches: `true` to match URLs with known top level domains (.com, .net,
157-
etc.) that are not prefixed with a scheme or `'www.'`. Ex: `google.com`,
158+
etc.) that are not prefixed with a scheme or `'www.'`. Ex: `google.com`,
158159
`asdf.org/?page=1`, etc. `false` to prevent these types of matches.
159160
<br />
160-
161+
161162
Example usage: `urls: { schemeMatches: true, wwwMatches: true, tldMatches: false }`
162-
163+
163164
- [email](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-email) : Boolean<br />
164165
`true` to have email addresses auto-linked, `false` to skip auto-linking of
165166
email addresses. Defaults to `true`.<br /><br />
166167
- [phone](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-phone) : Boolean<br />
167168
`true` to have phone numbers auto-linked, `false` to skip auto-linking of
168169
phone numbers. Defaults to `true`.<br /><br />
169-
- [twitter](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-twitter) : Boolean<br />
170+
- [twitter](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-twitter) (deprecated) : Boolean<br />
170171
`true` to have Twitter handles auto-linked, `false` to skip auto-linking of
171172
Twitter handles. Defaults to `true`.<br /><br />
173+
- [mention](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-mention) : String<br />
174+
A string for the service name to have mentions (@username) auto-linked to. Supported
175+
values at this time are 'twitter', and 'instagram'. Pass `false` to skip
176+
auto-linking of mentions. Overrides `twitter` parameter. Defaults to `twitter`.<br /><br />
172177
- [hashtag](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-hashtag) : Boolean/String<br />
173178
A string for the service name to have hashtags auto-linked to. Supported
174179
values at this time are 'twitter', 'facebook' and 'instagram'. Pass `false` to skip
@@ -231,7 +236,7 @@ individual basis, based on the return from this function.
231236
#### Full example, for purposes of documenting the API:
232237

233238
```javascript
234-
var input = "..."; // string with URLs, Email Addresses, Twitter Handles, and Hashtags
239+
var input = "..."; // string with URLs, Email Addresses, Mentions (Twitter, Instagram), and Hashtags
235240

236241
var linkedText = Autolinker.link( input, {
237242
replaceFn : function( autolinker, match ) {
@@ -241,7 +246,7 @@ var linkedText = Autolinker.link( input, {
241246
switch( match.getType() ) {
242247
case 'url' :
243248
console.log( "url: ", match.getUrl() );
244-
249+
245250
return true; // let Autolinker perform its normal anchor tag replacement
246251

247252
case 'email' :
@@ -259,10 +264,11 @@ var linkedText = Autolinker.link( input, {
259264

260265
return '<a href="http://newplace.to.link.phone.numbers.to/">' + match.getNumber() + '</a>';
261266

262-
case 'twitter' :
263-
console.log( "Twitter Handle: ", match.getTwitterHandle() );
267+
case 'mention' :
268+
console.log( "Mention: ", match.getMention() );
269+
console.log( "Mention Service Name: ", match.getServiceName() );
264270

265-
return '<a href="http://newplace.to.link.twitter.handles.to/">' + match.getTwitterHandle() + '</a>';
271+
return '<a href="http://newplace.to.link.mention.handles.to/">' + match.getMention() + '</a>';
266272

267273
case 'hashtag' :
268274
console.log( "Hashtag: ", match.getHashtag() );
@@ -276,13 +282,13 @@ var linkedText = Autolinker.link( input, {
276282
#### Modifying the default generated anchor tag
277283

278284
```javascript
279-
var input = "..."; // string with URLs, Email Addresses, Twitter Handles, and Hashtags
285+
var input = "..."; // string with URLs, Email Addresses, Mentions (Twitter, Instagram), and Hashtags
280286

281287
var linkedText = Autolinker.link( input, {
282288
replaceFn : function( autolinker, match ) {
283289
console.log( "href = ", match.getAnchorHref() );
284290
console.log( "text = ", match.getAnchorText() );
285-
291+
286292
var tag = match.buildTag(); // returns an `Autolinker.HtmlTag` instance for an <a> tag
287293
tag.setAttr( 'rel', 'nofollow' ); // adds a 'rel' attribute
288294
tag.addClass( 'external-link' ); // adds a CSS class

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"authors": [
66
"Gregory Jacobs <[email protected]>"
77
],
8-
"description": "Automatically links URLs, email addresses, and Twitter handles in a string of text or HTML.",
8+
"description": "Automatically links URLs, email addresses, mentions (Twitter, Instagram) in a string of text or HTML.",
99
"keywords": [
1010
"auto",
1111
"link",

0 commit comments

Comments
 (0)