UNPKG

24.8 kBTypeScriptView Raw
1import { Match } from './match/match';
2import { HtmlTag } from './html-tag';
3import { MentionService } from './parser/mention-utils';
4import { HashtagService } from './parser/hashtag-utils';
5/**
6 * @class Autolinker
7 * @extends Object
8 *
9 * Utility class used to process a given string of text, and wrap the matches in
10 * the appropriate anchor (<a>) tags to turn them into links.
11 *
12 * Any of the configuration options may be provided in an Object provided
13 * to the Autolinker constructor, which will configure how the {@link #link link()}
14 * method will process the links.
15 *
16 * For example:
17 *
18 * var autolinker = new Autolinker( {
19 * newWindow : false,
20 * truncate : 30
21 * } );
22 *
23 * var html = autolinker.link( "Joe went to www.yahoo.com" );
24 * // produces: 'Joe went to <a href="http://www.yahoo.com">yahoo.com</a>'
25 *
26 *
27 * The {@link #static-link static link()} method may also be used to inline
28 * options into a single call, which may be more convenient for one-off uses.
29 * For example:
30 *
31 * var html = Autolinker.link( "Joe went to www.yahoo.com", {
32 * newWindow : false,
33 * truncate : 30
34 * } );
35 * // produces: 'Joe went to <a href="http://www.yahoo.com">yahoo.com</a>'
36 *
37 *
38 * ## Custom Replacements of Links
39 *
40 * If the configuration options do not provide enough flexibility, a {@link #replaceFn}
41 * may be provided to fully customize the output of Autolinker. This function is
42 * called once for each URL/Email/Phone#/Hashtag/Mention (Twitter, Instagram, Soundcloud)
43 * match that is encountered.
44 *
45 * For example:
46 *
47 * var input = "..."; // string with URLs, Email Addresses, Phone #s, Hashtags, and Mentions (Twitter, Instagram, Soundcloud)
48 *
49 * var linkedText = Autolinker.link( input, {
50 * replaceFn : function( match ) {
51 * console.log( "href = ", match.getAnchorHref() );
52 * console.log( "text = ", match.getAnchorText() );
53 *
54 * switch( match.getType() ) {
55 * case 'url' :
56 * console.log( "url: ", match.getUrl() );
57 *
58 * if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) {
59 * var tag = match.buildTag(); // returns an `Autolinker.HtmlTag` instance, which provides mutator methods for easy changes
60 * tag.setAttr( 'rel', 'nofollow' );
61 * tag.addClass( 'external-link' );
62 *
63 * return tag;
64 *
65 * } else {
66 * return true; // let Autolinker perform its normal anchor tag replacement
67 * }
68 *
69 * case 'email' :
70 * var email = match.getEmail();
71 * console.log( "email: ", email );
72 *
73 * if( email === "my@own.address" ) {
74 * return false; // don't auto-link this particular email address; leave as-is
75 * } else {
76 * return; // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)
77 * }
78 *
79 * case 'phone' :
80 * var phoneNumber = match.getPhoneNumber();
81 * console.log( phoneNumber );
82 *
83 * return '<a href="http://newplace.to.link.phone.numbers.to/">' + phoneNumber + '</a>';
84 *
85 * case 'hashtag' :
86 * var hashtag = match.getHashtag();
87 * console.log( hashtag );
88 *
89 * return '<a href="http://newplace.to.link.hashtag.handles.to/">' + hashtag + '</a>';
90 *
91 * case 'mention' :
92 * var mention = match.getMention();
93 * console.log( mention );
94 *
95 * return '<a href="http://newplace.to.link.mention.to/">' + mention + '</a>';
96 * }
97 * }
98 * } );
99 *
100 *
101 * The function may return the following values:
102 *
103 * - `true` (Boolean): Allow Autolinker to replace the match as it normally
104 * would.
105 * - `false` (Boolean): Do not replace the current match at all - leave as-is.
106 * - Any String: If a string is returned from the function, the string will be
107 * used directly as the replacement HTML for the match.
108 * - An {@link Autolinker.HtmlTag} instance, which can be used to build/modify
109 * an HTML tag before writing out its HTML text.
110 */
111export default class Autolinker {
112 /**
113 * @static
114 * @property {String} version
115 *
116 * The Autolinker version number in the form major.minor.patch
117 *
118 * Ex: 3.15.0
119 */
120 static readonly version = "4.0.0";
121 /**
122 * Automatically links URLs, Email addresses, Phone Numbers, Twitter handles,
123 * Hashtags, and Mentions found in the given chunk of HTML. Does not link URLs
124 * found within HTML tags.
125 *
126 * For instance, if given the text: `You should go to http://www.yahoo.com`,
127 * then the result will be `You should go to &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;`
128 *
129 * Example:
130 *
131 * var linkedText = Autolinker.link( "Go to google.com", { newWindow: false } );
132 * // Produces: "Go to <a href="http://google.com">google.com</a>"
133 *
134 * @static
135 * @param {String} textOrHtml The HTML or text to find matches within (depending
136 * on if the {@link #urls}, {@link #email}, {@link #phone}, {@link #mention},
137 * {@link #hashtag}, and {@link #mention} options are enabled).
138 * @param {Object} [options] Any of the configuration options for the Autolinker
139 * class, specified in an Object (map). See the class description for an
140 * example call.
141 * @return {String} The HTML text, with matches automatically linked.
142 */
143 static link(textOrHtml: string, options?: AutolinkerConfig): string;
144 /**
145 * Parses the input `textOrHtml` looking for URLs, email addresses, phone
146 * numbers, username handles, and hashtags (depending on the configuration
147 * of the Autolinker instance), and returns an array of {@link Autolinker.match.Match}
148 * objects describing those matches (without making any replacements).
149 *
150 * Note that if parsing multiple pieces of text, it is slightly more efficient
151 * to create an Autolinker instance, and use the instance-level {@link #parse}
152 * method.
153 *
154 * Example:
155 *
156 * var matches = Autolinker.parse( "Hello google.com, I am asdf@asdf.com", {
157 * urls: true,
158 * email: true
159 * } );
160 *
161 * console.log( matches.length ); // 2
162 * console.log( matches[ 0 ].getType() ); // 'url'
163 * console.log( matches[ 0 ].getUrl() ); // 'google.com'
164 * console.log( matches[ 1 ].getType() ); // 'email'
165 * console.log( matches[ 1 ].getEmail() ); // 'asdf@asdf.com'
166 *
167 * @static
168 * @param {String} textOrHtml The HTML or text to find matches within
169 * (depending on if the {@link #urls}, {@link #email}, {@link #phone},
170 * {@link #hashtag}, and {@link #mention} options are enabled).
171 * @param {Object} [options] Any of the configuration options for the Autolinker
172 * class, specified in an Object (map). See the class description for an
173 * example call.
174 * @return {Autolinker.match.Match[]} The array of Matches found in the
175 * given input `textOrHtml`.
176 */
177 static parse(textOrHtml: string, options: AutolinkerConfig): Match[];
178 /**
179 * The Autolinker version number exposed on the instance itself.
180 *
181 * Ex: 0.25.1
182 *
183 * @property {String} version
184 */
185 readonly version = "4.0.0";
186 /**
187 * @cfg {Boolean/Object} [urls]
188 *
189 * `true` if URLs should be automatically linked, `false` if they should not
190 * be. Defaults to `true`.
191 *
192 * Examples:
193 *
194 * urls: true
195 *
196 * // or
197 *
198 * urls: {
199 * schemeMatches : true,
200 * tldMatches : true,
201 * ipV4Matches : true
202 * }
203 *
204 * As shown above, this option also accepts an Object form with 3 properties
205 * to allow for more customization of what exactly gets linked. All default
206 * to `true`:
207 *
208 * @cfg {Boolean} [urls.schemeMatches] `true` to match URLs found prefixed
209 * with a scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
210 * `false` to prevent these types of matches.
211 * @cfg {Boolean} [urls.tldMatches] `true` to match URLs with known top
212 * level domains (.com, .net, etc.) that are not prefixed with a scheme
213 * (such as 'http://'). This option attempts to match anything that looks
214 * like a URL in the given text. Ex: `google.com`, `asdf.org/?page=1`, etc.
215 * `false` to prevent these types of matches.
216 * @cfg {Boolean} [urls.ipV4Matches] `true` to match IPv4 addresses in text
217 * that are not prefixed with a scheme (such as 'http://'). This option
218 * attempts to match anything that looks like an IPv4 address in text. Ex:
219 * `192.168.0.1`, `10.0.0.1/?page=1`, etc. `false` to prevent these types
220 * of matches.
221 */
222 private readonly urls;
223 /**
224 * @cfg {Boolean} [email=true]
225 *
226 * `true` if email addresses should be automatically linked, `false` if they
227 * should not be.
228 */
229 private readonly email;
230 /**
231 * @cfg {Boolean} [phone=true]
232 *
233 * `true` if Phone numbers ("(555)555-5555") should be automatically linked,
234 * `false` if they should not be.
235 */
236 private readonly phone;
237 /**
238 * @cfg {Boolean/String} [hashtag=false]
239 *
240 * A string for the service name to have hashtags (ex: "#myHashtag")
241 * auto-linked to. The currently-supported values are:
242 *
243 * - 'twitter'
244 * - 'facebook'
245 * - 'instagram'
246 *
247 * Pass `false` to skip auto-linking of hashtags.
248 */
249 private readonly hashtag;
250 /**
251 * @cfg {String/Boolean} [mention=false]
252 *
253 * A string for the service name to have mentions (ex: "@myuser")
254 * auto-linked to. The currently supported values are:
255 *
256 * - 'twitter'
257 * - 'instagram'
258 * - 'soundcloud'
259 * - 'tiktok'
260 *
261 * Defaults to `false` to skip auto-linking of mentions.
262 */
263 private readonly mention;
264 /**
265 * @cfg {Boolean} [newWindow=true]
266 *
267 * `true` if the links should open in a new window, `false` otherwise.
268 */
269 private readonly newWindow;
270 /**
271 * @cfg {Boolean/Object} [stripPrefix=true]
272 *
273 * `true` if 'http://' (or 'https://') and/or the 'www.' should be stripped
274 * from the beginning of URL links' text, `false` otherwise. Defaults to
275 * `true`.
276 *
277 * Examples:
278 *
279 * stripPrefix: true
280 *
281 * // or
282 *
283 * stripPrefix: {
284 * scheme : true,
285 * www : true
286 * }
287 *
288 * As shown above, this option also accepts an Object form with 2 properties
289 * to allow for more customization of what exactly is prevented from being
290 * displayed. Both default to `true`:
291 *
292 * @cfg {Boolean} [stripPrefix.scheme] `true` to prevent the scheme part of
293 * a URL match from being displayed to the user. Example:
294 * `'http://google.com'` will be displayed as `'google.com'`. `false` to
295 * not strip the scheme. NOTE: Only an `'http://'` or `'https://'` scheme
296 * will be removed, so as not to remove a potentially dangerous scheme
297 * (such as `'file://'` or `'javascript:'`)
298 * @cfg {Boolean} [stripPrefix.www] www (Boolean): `true` to prevent the
299 * `'www.'` part of a URL match from being displayed to the user. Ex:
300 * `'www.google.com'` will be displayed as `'google.com'`. `false` to not
301 * strip the `'www'`.
302 */
303 private readonly stripPrefix;
304 /**
305 * @cfg {Boolean} [stripTrailingSlash=true]
306 *
307 * `true` to remove the trailing slash from URL matches, `false` to keep
308 * the trailing slash.
309 *
310 * Example when `true`: `http://google.com/` will be displayed as
311 * `http://google.com`.
312 */
313 private readonly stripTrailingSlash;
314 /**
315 * @cfg {Boolean} [decodePercentEncoding=true]
316 *
317 * `true` to decode percent-encoded characters in URL matches, `false` to keep
318 * the percent-encoded characters.
319 *
320 * Example when `true`: `https://en.wikipedia.org/wiki/San_Jos%C3%A9` will
321 * be displayed as `https://en.wikipedia.org/wiki/San_José`.
322 */
323 private readonly decodePercentEncoding;
324 /**
325 * @cfg {Number/Object} [truncate=0]
326 *
327 * ## Number Form
328 *
329 * A number for how many characters matched text should be truncated to
330 * inside the text of a link. If the matched text is over this number of
331 * characters, it will be truncated to this length by adding a two period
332 * ellipsis ('..') to the end of the string.
333 *
334 * For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file'
335 * truncated to 25 characters might look something like this:
336 * 'yahoo.com/some/long/pat..'
337 *
338 * Example Usage:
339 *
340 * truncate: 25
341 *
342 *
343 * Defaults to `0` for "no truncation."
344 *
345 *
346 * ## Object Form
347 *
348 * An Object may also be provided with two properties: `length` (Number) and
349 * `location` (String). `location` may be one of the following: 'end'
350 * (default), 'middle', or 'smart'.
351 *
352 * Example Usage:
353 *
354 * truncate: { length: 25, location: 'middle' }
355 *
356 * @cfg {Number} [truncate.length=0] How many characters to allow before
357 * truncation will occur. Defaults to `0` for "no truncation."
358 * @cfg {"end"/"middle"/"smart"} [truncate.location="end"]
359 *
360 * - 'end' (default): will truncate up to the number of characters, and then
361 * add an ellipsis at the end. Ex: 'yahoo.com/some/long/pat..'
362 * - 'middle': will truncate and add the ellipsis in the middle. Ex:
363 * 'yahoo.com/s..th/to/a/file'
364 * - 'smart': for URLs where the algorithm attempts to strip out unnecessary
365 * parts first (such as the 'www.', then URL scheme, hash, etc.),
366 * attempting to make the URL human-readable before looking for a good
367 * point to insert the ellipsis if it is still too long. Ex:
368 * 'yahoo.com/some..to/a/file'. For more details, see
369 * {@link Autolinker.truncate.TruncateSmart}.
370 */
371 private readonly truncate;
372 /**
373 * @cfg {String} className
374 *
375 * A CSS class name to add to the generated links. This class will be added
376 * to all links, as well as this class plus match suffixes for styling
377 * url/email/phone/hashtag/mention links differently.
378 *
379 * For example, if this config is provided as "myLink", then:
380 *
381 * - URL links will have the CSS classes: "myLink myLink-url"
382 * - Email links will have the CSS classes: "myLink myLink-email", and
383 * - Phone links will have the CSS classes: "myLink myLink-phone"
384 * - Hashtag links will have the CSS classes: "myLink myLink-hashtag"
385 * - Mention links will have the CSS classes: "myLink myLink-mention myLink-[type]"
386 * where [type] is either "instagram", "twitter" or "soundcloud"
387 */
388 private readonly className;
389 /**
390 * @cfg {Function} replaceFn
391 *
392 * A function to individually process each match found in the input string.
393 *
394 * See the class's description for usage.
395 *
396 * The `replaceFn` can be called with a different context object (`this`
397 * reference) using the {@link #context} cfg.
398 *
399 * This function is called with the following parameter:
400 *
401 * @cfg {Autolinker.match.Match} replaceFn.match The Match instance which
402 * can be used to retrieve information about the match that the `replaceFn`
403 * is currently processing. See {@link Autolinker.match.Match} subclasses
404 * for details.
405 */
406 private readonly replaceFn;
407 /**
408 * @cfg {Object} context
409 *
410 * The context object (`this` reference) to call the `replaceFn` with.
411 *
412 * Defaults to this Autolinker instance.
413 */
414 private readonly context;
415 /**
416 * @cfg {Boolean} [sanitizeHtml=false]
417 *
418 * `true` to HTML-encode the start and end brackets of existing HTML tags found
419 * in the input string. This will escape `<` and `>` characters to `&lt;` and
420 * `&gt;`, respectively.
421 *
422 * Setting this to `true` will prevent XSS (Cross-site Scripting) attacks,
423 * but will remove the significance of existing HTML tags in the input string. If
424 * you would like to maintain the significance of existing HTML tags while also
425 * making the output HTML string safe, leave this option as `false` and use a
426 * tool like https://github.com/cure53/DOMPurify (or others) on the input string
427 * before running Autolinker.
428 */
429 private readonly sanitizeHtml;
430 /**
431 * @private
432 * @property {Autolinker.AnchorTagBuilder} tagBuilder
433 *
434 * The AnchorTagBuilder instance used to build match replacement anchor tags.
435 * Note: this is lazily instantiated in the {@link #getTagBuilder} method.
436 */
437 private tagBuilder;
438 /**
439 * @method constructor
440 * @param {Object} [cfg] The configuration options for the Autolinker instance,
441 * specified in an Object (map).
442 */
443 constructor(cfg?: AutolinkerConfig);
444 /**
445 * Parses the input `textOrHtml` looking for URLs, email addresses, phone
446 * numbers, username handles, and hashtags (depending on the configuration
447 * of the Autolinker instance), and returns an array of {@link Autolinker.match.Match}
448 * objects describing those matches (without making any replacements).
449 *
450 * This method is used by the {@link #link} method, but can also be used to
451 * simply do parsing of the input in order to discover what kinds of links
452 * there are and how many.
453 *
454 * Example usage:
455 *
456 * var autolinker = new Autolinker( {
457 * urls: true,
458 * email: true
459 * } );
460 *
461 * var matches = autolinker.parse( "Hello google.com, I am asdf@asdf.com" );
462 *
463 * console.log( matches.length ); // 2
464 * console.log( matches[ 0 ].getType() ); // 'url'
465 * console.log( matches[ 0 ].getUrl() ); // 'google.com'
466 * console.log( matches[ 1 ].getType() ); // 'email'
467 * console.log( matches[ 1 ].getEmail() ); // 'asdf@asdf.com'
468 *
469 * @param {String} textOrHtml The HTML or text to find matches within
470 * (depending on if the {@link #urls}, {@link #email}, {@link #phone},
471 * {@link #hashtag}, and {@link #mention} options are enabled).
472 * @return {Autolinker.match.Match[]} The array of Matches found in the
473 * given input `textOrHtml`.
474 */
475 parse(textOrHtml: string): Match[];
476 /**
477 * After we have found all matches, we need to remove matches that overlap
478 * with a previous match. This can happen for instance with URLs, where the
479 * url 'google.com/#link' would match '#link' as a hashtag. Because the
480 * '#link' part is contained in a larger match that comes before the HashTag
481 * match, we'll remove the HashTag match.
482 *
483 * @private
484 * @param {Autolinker.match.Match[]} matches
485 * @return {Autolinker.match.Match[]}
486 */
487 private compactMatches;
488 /**
489 * Removes matches for matchers that were turned off in the options. For
490 * example, if {@link #hashtag hashtags} were not to be matched, we'll
491 * remove them from the `matches` array here.
492 *
493 * Note: we *must* use all Matchers on the input string, and then filter
494 * them out later. For example, if the options were `{ url: false, hashtag: true }`,
495 * we wouldn't want to match the text '#link' as a HashTag inside of the text
496 * 'google.com/#link'. The way the algorithm works is that we match the full
497 * URL first (which prevents the accidental HashTag match), and then we'll
498 * simply throw away the URL match.
499 *
500 * @private
501 * @param {Autolinker.match.Match[]} matches The array of matches to remove
502 * the unwanted matches from. Note: this array is mutated for the
503 * removals.
504 * @return {Autolinker.match.Match[]} The mutated input `matches` array.
505 */
506 private removeUnwantedMatches;
507 /**
508 * Parses the input `text` looking for URLs, email addresses, phone
509 * numbers, username handles, and hashtags (depending on the configuration
510 * of the Autolinker instance), and returns an array of {@link Autolinker.match.Match}
511 * objects describing those matches.
512 *
513 * This method processes a **non-HTML string**, and is used to parse and
514 * match within the text nodes of an HTML string. This method is used
515 * internally by {@link #parse}.
516 *
517 * @private
518 * @param {String} text The text to find matches within (depending on if the
519 * {@link #urls}, {@link #email}, {@link #phone},
520 * {@link #hashtag}, and {@link #mention} options are enabled). This must be a non-HTML string.
521 * @param {Number} [offset=0] The offset of the text node within the
522 * original string. This is used when parsing with the {@link #parse}
523 * method to generate correct offsets within the {@link Autolinker.match.Match}
524 * instances, but may be omitted if calling this method publicly.
525 * @return {Autolinker.match.Match[]} The array of Matches found in the
526 * given input `text`.
527 */
528 private parseText;
529 /**
530 * Automatically links URLs, Email addresses, Phone numbers, Hashtags,
531 * and Mentions (Twitter, Instagram, Soundcloud) found in the given chunk of HTML. Does not link
532 * URLs found within HTML tags.
533 *
534 * For instance, if given the text: `You should go to http://www.yahoo.com`,
535 * then the result will be `You should go to
536 * &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;`
537 *
538 * This method finds the text around any HTML elements in the input
539 * `textOrHtml`, which will be the text that is processed. Any original HTML
540 * elements will be left as-is, as well as the text that is already wrapped
541 * in anchor (&lt;a&gt;) tags.
542 *
543 * @param {String} textOrHtml The HTML or text to autolink matches within
544 * (depending on if the {@link #urls}, {@link #email}, {@link #phone}, {@link #hashtag}, and {@link #mention} options are enabled).
545 * @return {String} The HTML, with matches automatically linked.
546 */
547 link(textOrHtml: string): string;
548 /**
549 * Creates the return string value for a given match in the input string.
550 *
551 * This method handles the {@link #replaceFn}, if one was provided.
552 *
553 * @private
554 * @param {Autolinker.match.Match} match The Match object that represents
555 * the match.
556 * @return {String} The string that the `match` should be replaced with.
557 * This is usually the anchor tag string, but may be the `matchStr` itself
558 * if the match is not to be replaced.
559 */
560 private createMatchReturnVal;
561 /**
562 * Returns the {@link #tagBuilder} instance for this Autolinker instance,
563 * lazily instantiating it if it does not yet exist.
564 *
565 * @private
566 * @return {Autolinker.AnchorTagBuilder}
567 */
568 private getTagBuilder;
569}
570export interface AutolinkerConfig {
571 urls?: UrlsConfig;
572 email?: boolean;
573 phone?: boolean;
574 hashtag?: HashtagConfig;
575 mention?: MentionConfig;
576 newWindow?: boolean;
577 stripPrefix?: StripPrefixConfig;
578 stripTrailingSlash?: boolean;
579 truncate?: TruncateConfig;
580 className?: string;
581 replaceFn?: ReplaceFn | null;
582 context?: any;
583 sanitizeHtml?: boolean;
584 decodePercentEncoding?: boolean;
585}
586export declare type UrlsConfig = boolean | UrlsConfigObj;
587export interface UrlsConfigObj {
588 schemeMatches?: boolean;
589 tldMatches?: boolean;
590 ipV4Matches?: boolean;
591}
592export declare type StripPrefixConfig = boolean | StripPrefixConfigObj;
593export interface StripPrefixConfigObj {
594 scheme?: boolean;
595 www?: boolean;
596}
597export declare type TruncateConfig = number | TruncateConfigObj;
598export interface TruncateConfigObj {
599 length?: number;
600 location?: 'end' | 'middle' | 'smart';
601}
602export declare type HashtagConfig = false | HashtagService;
603export declare type MentionConfig = false | MentionService;
604export declare type ReplaceFn = (match: Match) => ReplaceFnReturn;
605export declare type ReplaceFnReturn = boolean | string | HtmlTag | null | undefined | void;