Skip to content

Commit 0c73285

Browse files
committed
Split the anchor tag building functionality out into its own class.
1 parent bc37dae commit 0c73285

File tree

6 files changed

+356
-222
lines changed

6 files changed

+356
-222
lines changed

Gruntfile.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ module.exports = function(grunt) {
2424
},
2525

2626
jasmine: {
27-
src: 'dist/Autolinker.min.js',
28-
options: {
29-
specs: 'tests/*Spec.js',
27+
dist: {
28+
options: {
29+
specs: 'tests/*Spec.js',
30+
},
31+
src: 'dist/Autolinker.min.js'
3032
}
3133
},
3234

@@ -41,7 +43,7 @@ module.exports = function(grunt) {
4143
return '\t' + src.replace( /\n/g, '\n\t' ); // indent each source file, which is placed inside the UMD block
4244
}
4345
},
44-
src: [ 'src/umdBegin.js', 'src/Autolinker.js', 'src/umdEnd.js' ],
46+
src: [ 'src/umdBegin.js', 'src/Autolinker.js', 'src/AnchorTagBuilder.js', 'src/umdEnd.js' ],
4547
dest: 'dist/Autolinker.js',
4648
},
4749
},

dist/Autolinker.js

Lines changed: 125 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,43 @@
5555
var Autolinker = function( cfg ) {
5656
cfg = cfg || {};
5757

58-
// Assign the properties of `cfg` onto the Autolinker instance
58+
// Assign the properties of `cfg` onto the Autolinker instance. Prototype properties will be used for missing configs.
5959
for( var prop in cfg )
6060
if( cfg.hasOwnProperty( prop ) ) this[ prop ] = cfg[ prop ];
61+
62+
this.anchorTagBuilder = new Autolinker.AnchorTagBuilder( {
63+
newWindow : this.newWindow,
64+
stripPrefix : this.stripPrefix,
65+
truncate : this.truncate,
66+
className : this.className
67+
} );
6168
};
6269

6370

6471
Autolinker.prototype = {
6572
constructor : Autolinker, // fix constructor property
6673

74+
/**
75+
* @cfg {Boolean} urls
76+
*
77+
* `true` if miscellaneous URLs should be automatically linked, `false` if they should not be.
78+
*/
79+
urls : true,
80+
81+
/**
82+
* @cfg {Boolean} email
83+
*
84+
* `true` if email addresses should be automatically linked, `false` if they should not be.
85+
*/
86+
email : true,
87+
88+
/**
89+
* @cfg {Boolean} twitter
90+
*
91+
* `true` if Twitter handles ("@example") should be automatically linked, `false` if they should not be.
92+
*/
93+
twitter : true,
94+
6795
/**
6896
* @cfg {Boolean} newWindow
6997
*
@@ -89,27 +117,6 @@
89117
* something like this: 'http://www...th/to/a/file'
90118
*/
91119

92-
/**
93-
* @cfg {Boolean} twitter
94-
*
95-
* `true` if Twitter handles ("@example") should be automatically linked, `false` if they should not be.
96-
*/
97-
twitter : true,
98-
99-
/**
100-
* @cfg {Boolean} email
101-
*
102-
* `true` if email addresses should be automatically linked, `false` if they should not be.
103-
*/
104-
email : true,
105-
106-
/**
107-
* @cfg {Boolean} urls
108-
*
109-
* `true` if miscellaneous URLs should be automatically linked, `false` if they should not be.
110-
*/
111-
urls : true,
112-
113120
/**
114121
* @cfg {String} className
115122
*
@@ -259,14 +266,6 @@
259266
'>'
260267
].join( "" ), 'g' );
261268
} )(),
262-
263-
/**
264-
* @private
265-
* @property {RegExp} urlPrefixRegex
266-
*
267-
* A regular expression used to remove the 'http://' or 'https://' and/or the 'www.' from URLs.
268-
*/
269-
urlPrefixRegex: /^(https?:\/\/)?(www\.)?/i,
270269

271270

272271
/**
@@ -446,16 +445,108 @@
446445
}
447446

448447
// wrap the match in an anchor tag
449-
var anchorTag = me.createAnchorTag( linkType, anchorHref, anchorText );
448+
var anchorTag = me.anchorTagBuilder.createAnchorTag( linkType, anchorHref, anchorText );
450449
return prefixStr + anchorTag + suffixStr;
451450
} );
452-
},
451+
}
452+
453+
};
454+
455+
456+
/**
457+
* Automatically links URLs, email addresses, and Twitter handles found in the given chunk of HTML.
458+
* Does not link URLs found within HTML tags.
459+
*
460+
* For instance, if given the text: `You should go to http://www.yahoo.com`, then the result
461+
* will be `You should go to <a href="http://www.yahoo.com">http://www.yahoo.com</a>`
462+
*
463+
* Example:
464+
*
465+
* var linkedText = Autolinker.link( "Go to google.com", { newWindow: false } );
466+
* // Produces: "Go to <a href="http://google.com">google.com</a>"
467+
*
468+
* @static
469+
* @method link
470+
* @param {String} html The HTML text to link URLs within.
471+
* @param {Object} [options] Any of the configuration options for the Autolinker class, specified in an Object (map).
472+
* See the class description for an example call.
473+
* @return {String} The HTML text, with URLs automatically linked
474+
*/
475+
Autolinker.link = function( text, options ) {
476+
var autolinker = new Autolinker( options );
477+
return autolinker.link( text );
478+
};
479+
480+
/**
481+
* @private
482+
* @class Autolinker.AnchorTagBuilder
483+
* @extends Object
484+
*
485+
* Builds the anchor (&lt;a&gt;) tags for the Autolinker utility when a match is found.
486+
*
487+
* @constructor
488+
* @param {Object} [config] The configuration options for the AnchorTagBuilder instance, specified in an Object (map).
489+
*/
490+
Autolinker.AnchorTagBuilder = function( cfg ) {
491+
// Assign the properties of `cfg` onto the Autolinker instance
492+
for( var prop in cfg )
493+
if( cfg.hasOwnProperty( prop ) ) this[ prop ] = cfg[ prop ];
494+
};
495+
496+
497+
Autolinker.AnchorTagBuilder.prototype = {
498+
constructor : Autolinker.AnchorTagBuilder,
453499

454500

455501
/**
456-
* Generates the actual anchor (&lt;a&gt;) tag to use in place of a source url/email/twitter link.
502+
* @cfg {Boolean} newWindow
503+
*
504+
* `true` if the links should open in a new window, `false` otherwise.
505+
*/
506+
507+
/**
508+
* @cfg {Boolean} stripPrefix
509+
*
510+
* `true` if 'http://' or 'https://' and/or the 'www.' should be stripped from the beginning of links, `false` otherwise.
511+
*/
512+
513+
/**
514+
* @cfg {Number} truncate
457515
*
516+
* A number for how many characters long URLs/emails/twitter handles should be truncated to inside the text of
517+
* a link. If the URL/email/twitter is over this number of characters, it will be truncated to this length by
518+
* adding a two period ellipsis ('..') into the middle of the string.
519+
*
520+
* For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated to 25 characters might look
521+
* something like this: 'http://www...th/to/a/file'
522+
*/
523+
524+
/**
525+
* @cfg {String} className
526+
*
527+
* A CSS class name to add to the generated links. This class will be added to all links, as well as this class
528+
* plus url/email/twitter suffixes for styling url/email/twitter links differently.
529+
*
530+
* For example, if this config is provided as "myLink", then:
531+
*
532+
* 1) URL links will have the CSS classes: "myLink myLink-url"
533+
* 2) Email links will have the CSS classes: "myLink myLink-email", and
534+
* 3) Twitter links will have the CSS classes: "myLink myLink-twitter"
535+
*/
536+
537+
538+
/**
458539
* @private
540+
* @property {RegExp} urlPrefixRegex
541+
*
542+
* A regular expression used to remove the 'http://' or 'https://' and/or the 'www.' from URLs.
543+
*/
544+
urlPrefixRegex: /^(https?:\/\/)?(www\.)?/i,
545+
546+
547+
/**
548+
* Generates the actual anchor (&lt;a&gt;) tag to use in place of a source url/email/twitter link.
549+
*
459550
* @param {"url"/"email"/"twitter"} linkType The type of link that an anchor tag is being generated for.
460551
* @param {String} anchorHref The href for the anchor tag.
461552
* @param {String} anchorText The anchor tag's text (i.e. what will be displayed).
@@ -576,32 +667,7 @@
576667
}
577668
return anchorText;
578669
}
579-
580-
};
581-
582-
583-
/**
584-
* Automatically links URLs, email addresses, and Twitter handles found in the given chunk of HTML.
585-
* Does not link URLs found within HTML tags.
586-
*
587-
* For instance, if given the text: `You should go to http://www.yahoo.com`, then the result
588-
* will be `You should go to &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;`
589-
*
590-
* Example:
591-
*
592-
* var linkedText = Autolinker.link( "Go to google.com", { newWindow: false } );
593-
* // Produces: "Go to <a href="http://google.com">google.com</a>"
594-
*
595-
* @static
596-
* @method link
597-
* @param {String} html The HTML text to link URLs within.
598-
* @param {Object} [options] Any of the configuration options for the Autolinker class, specified in an Object (map).
599-
* See the class description for an example call.
600-
* @return {String} The HTML text, with URLs automatically linked
601-
*/
602-
Autolinker.link = function( text, options ) {
603-
var autolinker = new Autolinker( options );
604-
return autolinker.link( text );
670+
605671
};
606672

607673
return Autolinker;

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.

0 commit comments

Comments
 (0)