Skip to content

Commit 7f84b92

Browse files
committed
Add the ability to stripPrefix option to decide whether to only strip the scheme, the 'www', or both
1 parent 0c44489 commit 7f84b92

File tree

7 files changed

+336
-63
lines changed

7 files changed

+336
-63
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ providing an Object as the second parameter to [Autolinker.link()](http://gregja
122122
`true` to have URLs auto-linked, `false` to skip auto-linking of URLs.
123123
Defaults to `true`.<br>
124124

125-
This option also accepts an Object form with 3 properties, to allow for more
126-
customization of what exactly gets linked. All default to `true`:
125+
This option also accepts an Object form with 3 properties to allow for
126+
more customization of what exactly gets linked. All default to `true`:
127127

128128
- schemeMatches (Boolean): `true` to match URLs found prefixed with a scheme,
129129
i.e. `http://google.com`, or `other+scheme://google.com`, `false` to
@@ -154,8 +154,25 @@ providing an Object as the second parameter to [Autolinker.link()](http://gregja
154154
values at this time are 'twitter', 'facebook' and 'instagram'. Pass `false` to skip
155155
auto-linking of hashtags. Defaults to `false`.<br /><br />
156156
- [stripPrefix](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-stripPrefix) : Boolean<br />
157-
`true` to have the 'http://' or 'https://' and/or the 'www.' stripped from the
158-
beginning of links, `false` otherwise. Defaults to `true`.<br /><br />
157+
`true` to have the `'http://'` (or `'https://'`) and/or the `'www.'`
158+
stripped from the beginning of displayed links, `false` otherwise.
159+
Defaults to `true`.<br />
160+
161+
This option also accepts an Object form with 2 properties to allow for
162+
more customization of what exactly is prevented from being displayed.
163+
Both default to `true`:
164+
165+
- scheme (Boolean): `true` to prevent the scheme part of a URL match
166+
from being displayed to the user. Example: `'http://google.com'`
167+
will be displayed as `'google.com'`. `false` to not strip the
168+
scheme. NOTE: Only an `'http://'` or `'https://'` scheme will be
169+
removed, so as not to remove a potentially dangerous scheme (such
170+
as `'file://'` or `'javascript:'`).
171+
- www (Boolean): `true` to prevent the `'www.'` part of a URL match
172+
from being displayed to the user. Ex: `'www.google.com'` will be
173+
displayed as `'google.com'`. `false` to not strip the `'www'`.
174+
175+
<br /><br />
159176
- [stripTrailingSlash](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-stripTrailingSlash) : Boolean<br />
160177
`true` to remove the trailing slash from URL matches, `false` to keep
161178
the trailing slash. Example when `true`: `http://google.com/` will be

dist/Autolinker.js

Lines changed: 121 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ var Autolinker = function( cfg ) {
137137
this.hashtag = cfg.hashtag || false;
138138
this.mention = cfg.mention || false;
139139
this.newWindow = typeof cfg.newWindow === 'boolean' ? cfg.newWindow : true;
140-
this.stripPrefix = typeof cfg.stripPrefix === 'boolean' ? cfg.stripPrefix : true;
140+
this.stripPrefix = this.normalizeStripPrefixCfg( cfg.stripPrefix );
141141
this.stripTrailingSlash = typeof cfg.stripTrailingSlash === 'boolean' ? cfg.stripTrailingSlash : true;
142142

143143
// Validate the value of the `mention` cfg
@@ -207,23 +207,36 @@ Autolinker.prototype = {
207207
constructor : Autolinker, // fix constructor property
208208

209209
/**
210-
* @cfg {Boolean/Object} [urls=true]
210+
* @cfg {Boolean/Object} [urls]
211211
*
212212
* `true` if URLs should be automatically linked, `false` if they should not
213-
* be.
213+
* be. Defaults to `true`.
214214
*
215-
* This option also accepts an Object form with 3 properties, to allow for
216-
* more customization of what exactly gets linked. All default to `true`:
215+
* Examples:
217216
*
218-
* @param {Boolean} schemeMatches `true` to match URLs found prefixed with a
219-
* scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
217+
* urls: true
218+
*
219+
* // or
220+
*
221+
* urls: {
222+
* schemeMatches : true,
223+
* wwwMatches : true,
224+
* tldMatches : true
225+
* }
226+
*
227+
* As shown above, this option also accepts an Object form with 3 properties
228+
* to allow for more customization of what exactly gets linked. All default
229+
* to `true`:
230+
*
231+
* @cfg {Boolean} [urls.schemeMatches] `true` to match URLs found prefixed
232+
* with a scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
220233
* `false` to prevent these types of matches.
221-
* @param {Boolean} wwwMatches `true` to match urls found prefixed with
234+
* @cfg {Boolean} [urls.wwwMatches] `true` to match urls found prefixed with
222235
* `'www.'`, i.e. `www.google.com`. `false` to prevent these types of
223236
* matches. Note that if the URL had a prefixed scheme, and
224237
* `schemeMatches` is true, it will still be linked.
225-
* @param {Boolean} tldMatches `true` to match URLs with known top level
226-
* domains (.com, .net, etc.) that are not prefixed with a scheme or
238+
* @cfg {Boolean} [urls.tldMatches] `true` to match URLs with known top
239+
* level domains (.com, .net, etc.) that are not prefixed with a scheme or
227240
* `'www.'`. This option attempts to match anything that looks like a URL
228241
* in the given text. Ex: `google.com`, `asdf.org/?page=1`, etc. `false`
229242
* to prevent these types of matches.
@@ -275,10 +288,37 @@ Autolinker.prototype = {
275288
*/
276289

277290
/**
278-
* @cfg {Boolean} [stripPrefix=true]
291+
* @cfg {Boolean/Object} [stripPrefix]
292+
*
293+
* `true` if 'http://' (or 'https://') and/or the 'www.' should be stripped
294+
* from the beginning of URL links' text, `false` otherwise. Defaults to
295+
* `true`.
296+
*
297+
* Examples:
279298
*
280-
* `true` if 'http://' or 'https://' and/or the 'www.' should be stripped
281-
* from the beginning of URL links' text, `false` otherwise.
299+
* stripPrefix: true
300+
*
301+
* // or
302+
*
303+
* stripPrefix: {
304+
* scheme : true,
305+
* www : true
306+
* }
307+
*
308+
* As shown above, this option also accepts an Object form with 2 properties
309+
* to allow for more customization of what exactly is prevented from being
310+
* displayed. Both default to `true`:
311+
*
312+
* @cfg {Boolean} [stripPrefix.scheme] `true` to prevent the scheme part of
313+
* a URL match from being displayed to the user. Example:
314+
* `'http://google.com'` will be displayed as `'google.com'`. `false` to
315+
* not strip the scheme. NOTE: Only an `'http://'` or `'https://'` scheme
316+
* will be removed, so as not to remove a potentially dangerous scheme
317+
* (such as `'file://'` or `'javascript:'`)
318+
* @cfg {Boolean} [stripPrefix.www] www (Boolean): `true` to prevent the
319+
* `'www.'` part of a URL match from being displayed to the user. Ex:
320+
* `'www.google.com'` will be displayed as `'google.com'`. `false` to not
321+
* strip the `'www'`.
282322
*/
283323

284324
/**
@@ -445,6 +485,31 @@ Autolinker.prototype = {
445485
},
446486

447487

488+
/**
489+
* Normalizes the {@link #stripPrefix} config into an Object with 2
490+
* properties: `scheme`, and `www` - both Booleans.
491+
*
492+
* See {@link #stripPrefix} config for details.
493+
*
494+
* @private
495+
* @param {Boolean/Object} stripPrefix
496+
* @return {Object}
497+
*/
498+
normalizeStripPrefixCfg : function( stripPrefix ) {
499+
if( stripPrefix == null ) stripPrefix = true; // default to `true`
500+
501+
if( typeof stripPrefix === 'boolean' ) {
502+
return { scheme: stripPrefix, www: stripPrefix };
503+
504+
} else { // object form
505+
return {
506+
scheme : typeof stripPrefix.scheme === 'boolean' ? stripPrefix.scheme : true,
507+
www : typeof stripPrefix.www === 'boolean' ? stripPrefix.www : true
508+
};
509+
}
510+
},
511+
512+
448513
/**
449514
* Normalizes the {@link #truncate} config into an Object with 2 properties:
450515
* `length` (Number), and `location` (String).
@@ -2749,8 +2814,9 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
27492814
*/
27502815

27512816
/**
2752-
* @cfg {Boolean} stripPrefix (required)
2753-
* @inheritdoc Autolinker#cfg-stripPrefix
2817+
* @cfg {Object} stripPrefix (required)
2818+
*
2819+
* The Object form of {@link Autolinker#cfg-stripPrefix}.
27542820
*/
27552821

27562822
/**
@@ -2785,12 +2851,20 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
27852851

27862852
/**
27872853
* @private
2788-
* @property {RegExp} urlPrefixRegex
2854+
* @property {RegExp} schemePrefixRegex
2855+
*
2856+
* A regular expression used to remove the 'http://' or 'https://' from
2857+
* URLs.
2858+
*/
2859+
schemePrefixRegex: /^(https?:\/\/)?/i,
2860+
2861+
/**
2862+
* @private
2863+
* @property {RegExp} wwwPrefixRegex
27892864
*
2790-
* A regular expression used to remove the 'http://' or 'https://' and/or
2791-
* the 'www.' from URLs.
2865+
* A regular expression used to remove the 'www.' from URLs.
27922866
*/
2793-
urlPrefixRegex: /^(https?:\/\/)?(www\.)?/i,
2867+
wwwPrefixRegex: /^(https?:\/\/)?(www\.)?/i,
27942868

27952869
/**
27962870
* @private
@@ -2881,8 +2955,11 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
28812955
// Strip off any protocol-relative '//' from the anchor text
28822956
anchorText = this.stripProtocolRelativePrefix( anchorText );
28832957
}
2884-
if( this.stripPrefix ) {
2885-
anchorText = this.stripUrlPrefix( anchorText );
2958+
if( this.stripPrefix.scheme ) {
2959+
anchorText = this.stripSchemePrefix( anchorText );
2960+
}
2961+
if( this.stripPrefix.www ) {
2962+
anchorText = this.stripWwwPrefix( anchorText );
28862963
}
28872964
if( this.stripTrailingSlash ) {
28882965
anchorText = this.removeTrailingSlash( anchorText ); // remove trailing slash, if there is one
@@ -2897,15 +2974,29 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
28972974
// Utility Functionality
28982975

28992976
/**
2900-
* Strips the URL prefix (such as "http://" or "https://") from the given text.
2977+
* Strips the scheme prefix (such as "http://" or "https://") from the given
2978+
* `url`.
29012979
*
29022980
* @private
2903-
* @param {String} text The text of the anchor that is being generated, for which to strip off the
2904-
* url prefix (such as stripping off "http://")
2905-
* @return {String} The `anchorText`, with the prefix stripped.
2981+
* @param {String} url The text of the anchor that is being generated, for
2982+
* which to strip off the url scheme.
2983+
* @return {String} The `url`, with the scheme stripped.
2984+
*/
2985+
stripSchemePrefix : function( url ) {
2986+
return url.replace( this.schemePrefixRegex, '' );
2987+
},
2988+
2989+
2990+
/**
2991+
* Strips the 'www' prefix from the given `url`.
2992+
*
2993+
* @private
2994+
* @param {String} url The text of the anchor that is being generated, for
2995+
* which to strip off the 'www' if it exists.
2996+
* @return {String} The `url`, with the 'www' stripped.
29062997
*/
2907-
stripUrlPrefix : function( text ) {
2908-
return text.replace( this.urlPrefixRegex, '' );
2998+
stripWwwPrefix : function( url ) {
2999+
return url.replace( this.wwwPrefixRegex, '$1' ); // leave any scheme ($1), it one exists
29093000
},
29103001

29113002

@@ -3279,8 +3370,9 @@ Autolinker.matcher.Mention = Autolinker.Util.extend( Autolinker.matcher.Matcher,
32793370
Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
32803371

32813372
/**
3282-
* @cfg {Boolean} stripPrefix (required)
3283-
* @inheritdoc Autolinker#stripPrefix
3373+
* @cfg {Object} stripPrefix (required)
3374+
*
3375+
* The Object form of {@link Autolinker#cfg-stripPrefix}.
32843376
*/
32853377

32863378
/**

dist/Autolinker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Autolinker.js

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var Autolinker = function( cfg ) {
119119
this.hashtag = cfg.hashtag || false;
120120
this.mention = cfg.mention || false;
121121
this.newWindow = typeof cfg.newWindow === 'boolean' ? cfg.newWindow : true;
122-
this.stripPrefix = typeof cfg.stripPrefix === 'boolean' ? cfg.stripPrefix : true;
122+
this.stripPrefix = this.normalizeStripPrefixCfg( cfg.stripPrefix );
123123
this.stripTrailingSlash = typeof cfg.stripTrailingSlash === 'boolean' ? cfg.stripTrailingSlash : true;
124124

125125
// Validate the value of the `mention` cfg
@@ -189,23 +189,36 @@ Autolinker.prototype = {
189189
constructor : Autolinker, // fix constructor property
190190

191191
/**
192-
* @cfg {Boolean/Object} [urls=true]
192+
* @cfg {Boolean/Object} [urls]
193193
*
194194
* `true` if URLs should be automatically linked, `false` if they should not
195-
* be.
195+
* be. Defaults to `true`.
196196
*
197-
* This option also accepts an Object form with 3 properties, to allow for
198-
* more customization of what exactly gets linked. All default to `true`:
197+
* Examples:
199198
*
200-
* @param {Boolean} schemeMatches `true` to match URLs found prefixed with a
201-
* scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
199+
* urls: true
200+
*
201+
* // or
202+
*
203+
* urls: {
204+
* schemeMatches : true,
205+
* wwwMatches : true,
206+
* tldMatches : true
207+
* }
208+
*
209+
* As shown above, this option also accepts an Object form with 3 properties
210+
* to allow for more customization of what exactly gets linked. All default
211+
* to `true`:
212+
*
213+
* @cfg {Boolean} [urls.schemeMatches] `true` to match URLs found prefixed
214+
* with a scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
202215
* `false` to prevent these types of matches.
203-
* @param {Boolean} wwwMatches `true` to match urls found prefixed with
216+
* @cfg {Boolean} [urls.wwwMatches] `true` to match urls found prefixed with
204217
* `'www.'`, i.e. `www.google.com`. `false` to prevent these types of
205218
* matches. Note that if the URL had a prefixed scheme, and
206219
* `schemeMatches` is true, it will still be linked.
207-
* @param {Boolean} tldMatches `true` to match URLs with known top level
208-
* domains (.com, .net, etc.) that are not prefixed with a scheme or
220+
* @cfg {Boolean} [urls.tldMatches] `true` to match URLs with known top
221+
* level domains (.com, .net, etc.) that are not prefixed with a scheme or
209222
* `'www.'`. This option attempts to match anything that looks like a URL
210223
* in the given text. Ex: `google.com`, `asdf.org/?page=1`, etc. `false`
211224
* to prevent these types of matches.
@@ -257,10 +270,37 @@ Autolinker.prototype = {
257270
*/
258271

259272
/**
260-
* @cfg {Boolean} [stripPrefix=true]
273+
* @cfg {Boolean/Object} [stripPrefix]
274+
*
275+
* `true` if 'http://' (or 'https://') and/or the 'www.' should be stripped
276+
* from the beginning of URL links' text, `false` otherwise. Defaults to
277+
* `true`.
278+
*
279+
* Examples:
280+
*
281+
* stripPrefix: true
261282
*
262-
* `true` if 'http://' or 'https://' and/or the 'www.' should be stripped
263-
* from the beginning of URL links' text, `false` otherwise.
283+
* // or
284+
*
285+
* stripPrefix: {
286+
* scheme : true,
287+
* www : true
288+
* }
289+
*
290+
* As shown above, this option also accepts an Object form with 2 properties
291+
* to allow for more customization of what exactly is prevented from being
292+
* displayed. Both default to `true`:
293+
*
294+
* @cfg {Boolean} [stripPrefix.scheme] `true` to prevent the scheme part of
295+
* a URL match from being displayed to the user. Example:
296+
* `'http://google.com'` will be displayed as `'google.com'`. `false` to
297+
* not strip the scheme. NOTE: Only an `'http://'` or `'https://'` scheme
298+
* will be removed, so as not to remove a potentially dangerous scheme
299+
* (such as `'file://'` or `'javascript:'`)
300+
* @cfg {Boolean} [stripPrefix.www] www (Boolean): `true` to prevent the
301+
* `'www.'` part of a URL match from being displayed to the user. Ex:
302+
* `'www.google.com'` will be displayed as `'google.com'`. `false` to not
303+
* strip the `'www'`.
264304
*/
265305

266306
/**
@@ -427,6 +467,31 @@ Autolinker.prototype = {
427467
},
428468

429469

470+
/**
471+
* Normalizes the {@link #stripPrefix} config into an Object with 2
472+
* properties: `scheme`, and `www` - both Booleans.
473+
*
474+
* See {@link #stripPrefix} config for details.
475+
*
476+
* @private
477+
* @param {Boolean/Object} stripPrefix
478+
* @return {Object}
479+
*/
480+
normalizeStripPrefixCfg : function( stripPrefix ) {
481+
if( stripPrefix == null ) stripPrefix = true; // default to `true`
482+
483+
if( typeof stripPrefix === 'boolean' ) {
484+
return { scheme: stripPrefix, www: stripPrefix };
485+
486+
} else { // object form
487+
return {
488+
scheme : typeof stripPrefix.scheme === 'boolean' ? stripPrefix.scheme : true,
489+
www : typeof stripPrefix.www === 'boolean' ? stripPrefix.www : true
490+
};
491+
}
492+
},
493+
494+
430495
/**
431496
* Normalizes the {@link #truncate} config into an Object with 2 properties:
432497
* `length` (Number), and `location` (String).

0 commit comments

Comments
 (0)