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