|
55 | 55 | var Autolinker = function( cfg ) { |
56 | 56 | cfg = cfg || {}; |
57 | 57 |
|
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. |
59 | 59 | for( var prop in cfg ) |
60 | 60 | 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 | + } ); |
61 | 68 | }; |
62 | 69 |
|
63 | 70 |
|
64 | 71 | Autolinker.prototype = { |
65 | 72 | constructor : Autolinker, // fix constructor property |
66 | 73 |
|
| 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 | + |
67 | 95 | /** |
68 | 96 | * @cfg {Boolean} newWindow |
69 | 97 | * |
|
89 | 117 | * something like this: 'http://www...th/to/a/file' |
90 | 118 | */ |
91 | 119 |
|
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 | | - |
113 | 120 | /** |
114 | 121 | * @cfg {String} className |
115 | 122 | * |
|
259 | 266 | '>' |
260 | 267 | ].join( "" ), 'g' ); |
261 | 268 | } )(), |
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, |
270 | 269 |
|
271 | 270 |
|
272 | 271 | /** |
|
446 | 445 | } |
447 | 446 |
|
448 | 447 | // wrap the match in an anchor tag |
449 | | - var anchorTag = me.createAnchorTag( linkType, anchorHref, anchorText ); |
| 448 | + var anchorTag = me.anchorTagBuilder.createAnchorTag( linkType, anchorHref, anchorText ); |
450 | 449 | return prefixStr + anchorTag + suffixStr; |
451 | 450 | } ); |
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 (<a>) 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, |
453 | 499 |
|
454 | 500 |
|
455 | 501 | /** |
456 | | - * Generates the actual anchor (<a>) 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 |
457 | 515 | * |
| 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 | + /** |
458 | 539 | * @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 (<a>) tag to use in place of a source url/email/twitter link. |
| 549 | + * |
459 | 550 | * @param {"url"/"email"/"twitter"} linkType The type of link that an anchor tag is being generated for. |
460 | 551 | * @param {String} anchorHref The href for the anchor tag. |
461 | 552 | * @param {String} anchorText The anchor tag's text (i.e. what will be displayed). |
|
576 | 667 | } |
577 | 668 | return anchorText; |
578 | 669 | } |
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 <a href="http://www.yahoo.com">http://www.yahoo.com</a>` |
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 | + |
605 | 671 | }; |
606 | 672 |
|
607 | 673 | return Autolinker; |
|
0 commit comments