autolinker

  • Version 4.1.5
  • Published
  • 2.44 MB
  • 1 dependency
  • MIT license

Install

npm i autolinker
yarn add autolinker
pnpm add autolinker

Overview

Utility to automatically link the URLs, email addresses, phone numbers, hashtags, and mentions (Twitter, Instagram) in a given block of text/HTML

Index

Variables

Functions

Classes

Interfaces

Type Aliases

Variables

variable whitespaceRe

const whitespaceRe: RegExp;

    Functions

    function excludeUnbalancedTrailingBracesAndPunctuation

    excludeUnbalancedTrailingBracesAndPunctuation: (matchedText: string) => string;
    • Determines if a match found has unmatched closing parenthesis, square brackets or curly brackets. If so, these unbalanced symbol(s) will be removed from the URL match itself.

      A match may have an extra closing parenthesis/square brackets/curly brackets at the end of the match because these are valid URL path characters. For example, "wikipedia.com/something_(disambiguation)" should be auto-linked.

      However, an extra parenthesis *will* be included when the URL itself is wrapped in parenthesis, such as in the case of:

      "(wikipedia.com/something_(disambiguation))"

      In this case, the last closing parenthesis should *not* be part of the URL itself, and this method will exclude it from the returned URL.

      For square brackets in URLs such as in PHP arrays, the same behavior as parenthesis discussed above should happen:

      "[http://www.example.com/foo.php?bar[]=1&bar[]=2&bar[]=3]"

      The very last closing square bracket should not be part of the URL itself, and therefore this method will remove it.

      Parameter matchedText

      The full matched URL/email/hashtag/etc. from the state machine parser. The updated matched text with extraneous suffix characters removed.

    function parseMatches

    parseMatches: (text: string, args: ParseMatchesArgs) => Match[];
    • Parses URL, email, twitter, mention, and hashtag matches from the given text.

    Classes

    class AbstractMatch

    abstract class AbstractMatch {}
    • Autolinker.match.AbstractMatch

      Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a , and may be used to query for details about the match.

      For example:

      var input = "..."; // string with URLs, Email Addresses, and Mentions (Twitter, Instagram, Soundcloud)

      var linkedText = Autolinker.link( input, { replaceFn : function( match ) { console.log( "href = ", match.getAnchorHref() ); console.log( "text = ", match.getAnchorText() );

      switch( match.getType() ) { case 'url' : console.log( "url: ", match.getUrl() );

      case 'email' : console.log( "email: ", match.getEmail() );

      case 'mention' : console.log( "mention: ", match.getMention() ); } } } );

      See the Autolinker class for more details on using the .

    constructor

    constructor(cfg: AbstractMatchConfig);
    • Autolinker.match.Match constructor

      Parameter cfg

      The configuration properties for the Match instance, specified in an Object (map).

    property matchedText

    protected readonly matchedText: string;

    property type

    abstract readonly type: MatchType;
    • {'url'/'email'/'hashtag'/'mention'/'phone'} type

      A string name for the type of match that this class represents. Can be used in a TypeScript discriminating union to type-narrow from the Match type.

      Modifiers

      • @public

    method buildTag

    buildTag: () => HtmlTag;
    • Builds and returns an Autolinker.HtmlTag instance based on the Match.

      This can be used to easily generate anchor tags from matches, and either return their HTML string, or modify them before doing so.

      Example Usage:

      var tag = match.buildTag(); tag.addClass( 'cordova-link' ); tag.setAttr( 'target', '_system' );

      tag.toAnchorString(); // Google

      Example Usage in Autolinker#replaceFn:

      var html = Autolinker.link( "Test google.com", { replaceFn : function( match ) { var tag = match.buildTag(); // returns an Autolinker.HtmlTag instance tag.setAttr( 'rel', 'nofollow' );

      return tag; } } );

      // generated html: // Test google.com

    method getAnchorHref

    abstract getAnchorHref: () => string;
    • Returns the anchor href that should be generated for the match.

      {String}

    method getAnchorText

    abstract getAnchorText: () => string;
    • Returns the anchor text that should be generated for the match.

      {String}

    method getCssClassSuffixes

    getCssClassSuffixes: () => string[];
    • Returns the CSS class suffix(es) for this match.

      A CSS class suffix is appended to the Autolinker#className in the Autolinker.AnchorTagBuilder when a match is translated into an anchor tag.

      For example, if Autolinker#className was configured as 'myLink', and this method returns [ 'url' ], the final class name of the element will become: 'myLink myLink-url'.

      The match may provide multiple CSS class suffixes to be appended to the Autolinker#className in order to facilitate better styling options for different match criteria. See Autolinker.match.Mention for an example.

      By default, this method returns a single array with the match's name, but may be overridden by subclasses.

      {String[]}

    method getMatchedText

    getMatchedText: () => string;
    • Returns the original text that was matched.

      {String}

    method getOffset

    getOffset: () => number;
    • Returns the offset of where the match was made in the input string. This is the 0-based index of the match.

      {Number}

    method getType

    abstract getType: () => MatchType;
    • Returns a string name for the type of match that this class represents.

      Deprecated

      Use instead which can assist in type-narrowing for TypeScript. {String}

    method setOffset

    setOffset: (offset: number) => void;
    • Sets the of where the match was made in the input string.

      A Autolinker.matcher.Matcher will be fed only HTML text nodes, and will therefore set an original offset that is relative to the HTML text node itself. However, we want this offset to be relative to the full HTML input string, and thus if using Autolinker#parse (rather than calling a Autolinker.matcher.Matcher directly), then this offset is corrected after the Matcher itself has done its job.

      Parameter offset

    class AnchorTagBuilder

    class AnchorTagBuilder {}
    • Autolinker.AnchorTagBuilder Object

      Builds anchor (<a>) tags for the Autolinker utility when a match is found.

      Normally this class is instantiated, configured, and used internally by an Autolinker instance, but may actually be used indirectly in a to create instances which may be modified before returning from the . For example:

      var html = Autolinker.link("Test google.com", { replaceFn: function(match) { var tag = match.buildTag(); // returns an Autolinker.HtmlTag instance tag.setAttr('rel', 'nofollow');

      return tag; } });

      // generated html: // Test google.com

    constructor

    constructor(cfg?: AnchorTagBuilderCfg);
    • constructor

      Parameter cfg

      The configuration options for the AnchorTagBuilder instance, specified in an Object (map).

    method build

    build: (match: AbstractMatch) => HtmlTag;
    • Generates the actual anchor (<a>) tag to use in place of the matched text, via its match object.

      Parameter match

      The Match instance to generate an anchor tag from. The HtmlTag instance for the anchor tag.

    method createAttrs

    protected createAttrs: (match: AbstractMatch) => { [attrName: string]: string };
    • Creates the Object (map) of the HTML attributes for the anchor (<a>) tag being generated.

      Parameter match

      The Match instance to generate an anchor tag from. A key/value Object (map) of the anchor tag's attributes.

    method createCssClass

    protected createCssClass: (match: AbstractMatch) => string;
    • Creates the CSS class that will be used for a given anchor tag, based on the matchType and the config.

      Example returns:

      - "" // no - "myLink myLink-url" // url match - "myLink myLink-email" // email match - "myLink myLink-phone" // phone match - "myLink myLink-hashtag" // hashtag match - "myLink myLink-mention myLink-twitter" // mention match with Twitter service

      Parameter match

      The Match instance to generate an anchor tag from. The CSS class string for the link. Example return: "myLink myLink-url". If no was configured, returns an empty string.

    class Autolinker

    class Autolinker {}
    • Autolinker Object

      Utility class used to process a given string of text, and wrap the matches in the appropriate anchor (<a>) tags to turn them into links.

      Any of the configuration options may be provided in an Object provided to the Autolinker constructor, which will configure how the method will process the links.

      For example:

      var autolinker = new Autolinker( { newWindow : false, truncate : 30 } );

      var html = autolinker.link( "Joe went to www.yahoo.com" ); // produces: 'Joe went to yahoo.com'

      The method may also be used to inline options into a single call, which may be more convenient for one-off uses. For example:

      var html = Autolinker.link( "Joe went to www.yahoo.com", { newWindow : false, truncate : 30 } ); // produces: 'Joe went to yahoo.com'

      ## Custom Replacements of Links

      If the configuration options do not provide enough flexibility, a may be provided to fully customize the output of Autolinker. This function is called once for each URL/Email/Phone#/Hashtag/Mention (Twitter, Instagram, Soundcloud) match that is encountered.

      For example:

      var input = "..."; // string with URLs, Email Addresses, Phone #s, Hashtags, and Mentions (Twitter, Instagram, Soundcloud)

      var linkedText = Autolinker.link( input, { replaceFn : function( match ) { console.log( "href = ", match.getAnchorHref() ); console.log( "text = ", match.getAnchorText() );

      switch( match.getType() ) { case 'url' : console.log( "url: ", match.getUrl() );

      if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) { var tag = match.buildTag(); // returns an Autolinker.HtmlTag instance, which provides mutator methods for easy changes tag.setAttr( 'rel', 'nofollow' ); tag.addClass( 'external-link' );

      return tag;

      } else { return true; // let Autolinker perform its normal anchor tag replacement }

      case 'email' : var email = match.getEmail(); console.log( "email: ", email );

      if( email === "my@own.address" ) { return false; // don't auto-link this particular email address; leave as-is } else { return; // no return value will have Autolinker perform its normal anchor tag replacement (same as returning true) }

      case 'phone' : var phoneNumber = match.getPhoneNumber(); console.log( phoneNumber );

      return '' + phoneNumber + '';

      case 'hashtag' : var hashtag = match.getHashtag(); console.log( hashtag );

      return '' + hashtag + '';

      case 'mention' : var mention = match.getMention(); console.log( mention );

      return '' + mention + ''; } } } );

      The function may return the following values:

      - true (Boolean): Allow Autolinker to replace the match as it normally would. - false (Boolean): Do not replace the current match at all - leave as-is. - Any String: If a string is returned from the function, the string will be used directly as the replacement HTML for the match. - An Autolinker.HtmlTag instance, which can be used to build/modify an HTML tag before writing out its HTML text.

    constructor

    constructor(cfg?: AutolinkerConfig);
    • constructor

      Parameter cfg

      The configuration options for the Autolinker instance, specified in an Object (map).

    property version

    static readonly version: string;
    • {String} version

      The Autolinker version number in the form major.minor.patch

      Ex: 3.15.0

    property version

    readonly version: string;
    • The Autolinker version number exposed on the instance itself.

      Ex: 0.25.1

      {String} version

    static link: (textOrHtml: string, options?: AutolinkerConfig) => string;
    • Automatically links URLs, Email addresses, Phone Numbers, Twitter handles, Hashtags, and Mentions found in the given chunk of HTML. Does not link URLs found within HTML tags.

      For instance, if given the text: You should go to http://www.yahoo.com, then the result will be You should go to <a href="http://www.yahoo.com">http://www.yahoo.com</a>

      Example:

      var linkedText = Autolinker.link( "Go to google.com", { newWindow: false } ); // Produces: "Go to google.com"

      Parameter textOrHtml

      The HTML or text to find matches within (depending on if the , , , , , and options are enabled).

      Parameter options

      Any of the configuration options for the Autolinker class, specified in an Object (map). See the class description for an example call. {String} The HTML text, with matches automatically linked.

    • Automatically links URLs, Email addresses, Phone numbers, Hashtags, and Mentions (Twitter, Instagram, Soundcloud) found in the given chunk of HTML. Does not link URLs found within HTML tags.

      For instance, if given the text: You should go to http://www.yahoo.com, then the result will be `You should go to <a href="http://www.yahoo.com">http://www.yahoo.com</a>`

      This method finds the text around any HTML elements in the input textOrHtml, which will be the text that is processed. Any original HTML elements will be left as-is, as well as the text that is already wrapped in anchor (<a>) tags.

      Parameter textOrHtml

      The HTML or text to autolink matches within (depending on if the , , , , and options are enabled). {String} The HTML, with matches automatically linked.

    method parse

    static parse: (textOrHtml: string, options?: AutolinkerConfig) => Match[];
    • Parses the input textOrHtml looking for URLs, email addresses, phone numbers, username handles, and hashtags (depending on the configuration of the Autolinker instance), and returns an array of Autolinker.match.Match objects describing those matches (without making any replacements).

      Note that if parsing multiple pieces of text, it is slightly more efficient to create an Autolinker instance, and use the instance-level method.

      Example:

      var matches = Autolinker.parse("Hello google.com, I am asdf@asdf.com", { urls: true, email: true });

      console.log(matches.length); // 2 console.log(matches[0].getType()); // 'url' console.log(matches[0].getUrl()); // 'google.com' console.log(matches[1].getType()); // 'email' console.log(matches[1].getEmail()); // 'asdf@asdf.com'

      Parameter textOrHtml

      The HTML or text to find matches within (depending on if the , , , , and options are enabled).

      Parameter options

      Any of the configuration options for the Autolinker class, specified in an Object (map). See the class description for an example call. {Autolinker.match.Match[]} The array of Matches found in the given input textOrHtml.

    • Parses the input textOrHtml looking for URLs, email addresses, phone numbers, username handles, and hashtags (depending on the configuration of the Autolinker instance), and returns an array of Autolinker.match.Match objects describing those matches (without making any replacements).

      This method is used by the method, but can also be used to simply do parsing of the input in order to discover what kinds of links there are and how many.

      Example usage:

      var autolinker = new Autolinker( { urls: true, email: true } );

      var matches = autolinker.parse( "Hello google.com, I am asdf@asdf.com" );

      console.log( matches.length ); // 2 console.log( matches[ 0 ].getType() ); // 'url' console.log( matches[ 0 ].getUrl() ); // 'google.com' console.log( matches[ 1 ].getType() ); // 'email' console.log( matches[ 1 ].getEmail() ); // 'asdf@asdf.com'

      Parameter textOrHtml

      The HTML or text to find matches within (depending on if the , , , , and options are enabled). {Autolinker.match.Match[]} The array of Matches found in the given input textOrHtml.

    class EmailMatch

    class EmailMatch extends AbstractMatch {}
    • Autolinker.match.Email Autolinker.match.AbstractMatch

      Represents a Email match found in an input string which should be Autolinked.

      See this class's superclass (Autolinker.match.Match) for more details.

    constructor

    constructor(cfg: EmailMatchConfig);
    • constructor

      Parameter cfg

      The configuration properties for the Match instance, specified in an Object (map).

    property type

    readonly type: string;
    • {'email'} type

      A string name for the type of match that this class represents. Can be used in a TypeScript discriminating union to type-narrow from the Match type.

      Modifiers

      • @public

    method getAnchorHref

    getAnchorHref: () => string;
    • Returns the anchor href that should be generated for the match.

      {String}

    method getAnchorText

    getAnchorText: () => string;
    • Returns the anchor text that should be generated for the match.

      {String}

    method getEmail

    getEmail: () => string;
    • Returns the email address that was matched.

      {String}

    method getType

    getType: () => 'email';
    • Returns a string name for the type of match that this class represents. For the case of EmailMatch, returns 'email'.

      {String}

    class HashtagMatch

    class HashtagMatch extends AbstractMatch {}
    • Autolinker.match.Hashtag Autolinker.match.AbstractMatch

      Represents a Hashtag match found in an input string which should be Autolinked.

      See this class's superclass (Autolinker.match.Match) for more details.

    constructor

    constructor(cfg: HashtagMatchConfig);
    • constructor

      Parameter cfg

      The configuration properties for the Match instance, specified in an Object (map).

    property type

    readonly type: string;
    • {'hashtag'} type

      A string name for the type of match that this class represents. Can be used in a TypeScript discriminating union to type-narrow from the Match type.

      Modifiers

      • @public

    method getAnchorHref

    getAnchorHref: () => string;
    • Returns the anchor href that should be generated for the match.

      {String}

    method getAnchorText

    getAnchorText: () => string;
    • Returns the anchor text that should be generated for the match.

      {String}

    method getCssClassSuffixes

    getCssClassSuffixes: () => string[];

    method getHashtag

    getHashtag: () => string;
    • Returns the matched hashtag, without the '#' character.

      {String}

    method getServiceName

    getServiceName: () => HashtagService;
    • Returns the configured to point the HashtagMatch to. Ex: 'facebook', 'twitter'.

      {String}

    method getType

    getType: () => 'hashtag';
    • Returns a string name for the type of match that this class represents. For the case of HashtagMatch, returns 'hashtag'.

      {String}

    class HtmlTag

    class HtmlTag {}
    • Autolinker.HtmlTag Object

      Represents an HTML tag, which can be used to easily build/modify HTML tags programmatically.

      Autolinker uses this abstraction to create HTML tags, and then write them out as strings. You may also use this class in your code, especially within a .

      ## Examples

      Example instantiation:

      var tag = new Autolinker.HtmlTag( { tagName : 'a', attrs : { 'href': 'http://google.com', 'class': 'external-link' }, innerHtml : 'Google' } );

      tag.toAnchorString(); // Google

      // Individual accessor methods tag.getTagName(); // 'a' tag.getAttr( 'href' ); // 'http://google.com' tag.hasClass( 'external-link' ); // true

      Using mutator methods (which may be used in combination with instantiation config properties):

      var tag = new Autolinker.HtmlTag(); tag.setTagName( 'a' ); tag.setAttr( 'href', 'http://google.com' ); tag.addClass( 'external-link' ); tag.setInnerHtml( 'Google' );

      tag.getTagName(); // 'a' tag.getAttr( 'href' ); // 'http://google.com' tag.hasClass( 'external-link' ); // true

      tag.toAnchorString(); // Google

      ## Example use within a

      var html = Autolinker.link( "Test google.com", { replaceFn : function( match ) { var tag = match.buildTag(); // returns an Autolinker.HtmlTag instance, configured with the Match's href and anchor text tag.setAttr( 'rel', 'nofollow' );

      return tag; } } );

      // generated html: // Test google.com

      ## Example use with a new tag for the replacement

      var html = Autolinker.link( "Test google.com", { replaceFn : function( match ) { var tag = new Autolinker.HtmlTag( { tagName : 'button', attrs : { 'title': 'Load URL: ' + match.getAnchorHref() }, innerHtml : 'Load URL: ' + match.getAnchorText() } );

      return tag; } } );

      // generated html: // Test Load URL: google.com

    constructor

    constructor(cfg?: HtmlTagCfg);
    • constructor

      Parameter cfg

      The configuration properties for this class, in an Object (map)

    method addClass

    addClass: (cssClass: string) => this;
    • Convenience method to add one or more CSS classes to the HtmlTag. Will not add duplicate CSS classes.

      Parameter cssClass

      One or more space-separated CSS classes to add. {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method buildAttrsStr

    protected buildAttrsStr: () => string;
    • Support method for , returns the string space-separated key="value" pairs, used to populate the stringified HtmlTag.

      {String} Example return: attr1="value1" attr2="value2"

    method getAttr

    getAttr: (attrName: string) => string;
    • Retrieves an attribute from the HtmlTag. If the attribute does not exist, returns undefined.

      Parameter attrName

      The attribute name to retrieve. {String} The attribute's value, or undefined if it does not exist on the HtmlTag.

    method getAttrs

    getAttrs: () => { [key: string]: string };
    • Retrieves the attributes Object (map) for the HtmlTag.

      {Object.<String, String>} A key/value object of the attributes for the HtmlTag.

    method getClass

    getClass: () => string;
    • Convenience method to retrieve the CSS class(es) for the HtmlTag, which will each be separated by spaces when there are multiple.

      {String}

    method getInnerHtml

    getInnerHtml: () => string;
    • Backward compatibility method name.

      {String}

    method getInnerHTML

    getInnerHTML: () => string;
    • Retrieves the inner HTML for the tag.

      {String}

    method getTagName

    getTagName: () => string;
    • Retrieves the tag name.

      {String}

    method hasClass

    hasClass: (cssClass: string) => boolean;
    • Convenience method to check if the tag has a CSS class or not.

      Parameter cssClass

      The CSS class to check for. {Boolean} true if the HtmlTag has the CSS class, false otherwise.

    method removeClass

    removeClass: (cssClass: string) => this;
    • Convenience method to remove one or more CSS classes from the HtmlTag.

      Parameter cssClass

      One or more space-separated CSS classes to remove. {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method setAttr

    setAttr: (attrName: string, attrValue: string) => this;
    • Sets an attribute on the HtmlTag.

      Parameter attrName

      The attribute name to set.

      Parameter attrValue

      The attribute value to set. {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method setAttrs

    setAttrs: (attrs: { [attr: string]: string }) => this;
    • Sets one or more attributes on the HtmlTag.

      Parameter attrs

      A key/value Object (map) of the attributes to set. {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method setClass

    setClass: (cssClass: string) => this;
    • Sets the provided cssClass, overwriting any current CSS classes on the HtmlTag.

      Parameter cssClass

      One or more space-separated CSS classes to set (overwrite). {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method setInnerHtml

    setInnerHtml: (html: string) => this;
    • Backwards compatibility method name.

      Parameter html

      The inner HTML to set. {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method setInnerHTML

    setInnerHTML: (html: string) => this;
    • Sets the inner HTML for the tag.

      Parameter html

      The inner HTML to set. {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method setTagName

    setTagName: (tagName: string) => this;
    • Sets the tag name that will be used to generate the tag with.

      Parameter tagName

      {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.

    method toAnchorString

    toAnchorString: () => string;
    • Generates the HTML string for the tag.

      {String}

    class MentionMatch

    class MentionMatch extends AbstractMatch {}
    • Autolinker.match.Mention Autolinker.match.AbstractMatch

      Represents a Mention match found in an input string which should be Autolinked.

      See this class's superclass (Autolinker.match.Match) for more details.

    constructor

    constructor(cfg: MentionMatchConfig);
    • constructor

      Parameter cfg

      The configuration properties for the Match instance, specified in an Object (map).

    property type

    readonly type: string;
    • {'mention'} type

      A string name for the type of match that this class represents. Can be used in a TypeScript discriminating union to type-narrow from the Match type.

      Modifiers

      • @public

    method getAnchorHref

    getAnchorHref: () => string;
    • Returns the anchor href that should be generated for the match.

      {String}

    method getAnchorText

    getAnchorText: () => string;
    • Returns the anchor text that should be generated for the match.

      {String}

    method getCssClassSuffixes

    getCssClassSuffixes: () => string[];

    method getMention

    getMention: () => string;
    • Returns the mention, without the '@' character.

      {String}

    method getServiceName

    getServiceName: () => MentionService;
    • Returns the configured to point the mention to. Ex: 'instagram', 'twitter', 'soundcloud'.

      {String}

    method getType

    getType: () => 'mention';
    • Returns a string name for the type of match that this class represents. For the case of MentionMatch, returns 'mention'.

      {String}

    class PhoneMatch

    class PhoneMatch extends AbstractMatch {}
    • Autolinker.match.Phone Autolinker.match.AbstractMatch

      Represents a Phone number match found in an input string which should be Autolinked.

      See this class's superclass (Autolinker.match.Match) for more details.

    constructor

    constructor(cfg: PhoneMatchConfig);
    • constructor

      Parameter cfg

      The configuration properties for the Match instance, specified in an Object (map).

    property type

    readonly type: string;
    • {'phone'} type

      A string name for the type of match that this class represents. Can be used in a TypeScript discriminating union to type-narrow from the Match type.

      Modifiers

      • @public

    method getAnchorHref

    getAnchorHref: () => string;
    • Returns the anchor href that should be generated for the match.

      {String}

    method getAnchorText

    getAnchorText: () => string;
    • Returns the anchor text that should be generated for the match.

      {String}

    method getNumber

    getNumber: () => string;
    • Alias of , returns the phone number that was matched as a string, without any delimiter characters.

      Note: This is a string to allow for prefixed 0's.

      {String}

    method getPhoneNumber

    getPhoneNumber: () => string;
    • Returns the phone number that was matched as a string, without any delimiter characters.

      Note: This is a string to allow for prefixed 0's.

      {String}

    method getType

    getType: () => 'phone';
    • Returns a string name for the type of match that this class represents. For the case of PhoneMatch, returns 'phone'.

      {String}

    class UrlMatch

    class UrlMatch extends AbstractMatch {}
    • Autolinker.match.Url Autolinker.match.AbstractMatch

      Represents a Url match found in an input string which should be Autolinked.

      See this class's superclass (Autolinker.match.Match) for more details.

    constructor

    constructor(cfg: UrlMatchConfig);
    • constructor

      Parameter cfg

      The configuration properties for the Match instance, specified in an Object (map).

    property type

    readonly type: string;
    • {'url'} type

      A string name for the type of match that this class represents. Can be used in a TypeScript discriminating union to type-narrow from the Match type.

      Modifiers

      • @public

    method getAnchorHref

    getAnchorHref: () => string;
    • Returns the anchor href that should be generated for the match.

      {String}

    method getAnchorText

    getAnchorText: () => string;
    • Returns the anchor text that should be generated for the match.

      {String}

    method getType

    getType: () => 'url';
    • Returns a string name for the type of match that this class represents. For the case of UrlMatch, returns 'url'.

      {String}

    method getUrl

    getUrl: () => string;
    • Returns the url that was matched, assuming the protocol to be 'http://' if the original match was missing a protocol.

      {String}

    method getUrlMatchType

    getUrlMatchType: () => UrlMatchType;
    • Returns a string name for the type of URL match that this class represents.

      This helps to determine if the match was made in the original text with a prefixed scheme (ex: 'http://www.google.com'), a prefixed 'www' (ex: 'www.google.com'), or was matched by a known top-level domain (ex: 'google.com').

      {"scheme"/"www"/"tld"}

    Interfaces

    interface AbstractMatchConfig

    interface AbstractMatchConfig {}

      property matchedText

      matchedText: string;

        property offset

        offset: number;

          property tagBuilder

          tagBuilder: AnchorTagBuilder;

            interface AnchorTagBuilderCfg

            interface AnchorTagBuilderCfg {}

              property className

              className?: string;

                property newWindow

                newWindow?: boolean;

                  property truncate

                  truncate?: TruncateConfigObj;

                    interface AutolinkerConfig

                    interface AutolinkerConfig {}

                      property className

                      className?: string;

                        property context

                        context?: object;

                          property decodePercentEncoding

                          decodePercentEncoding?: boolean;

                            property email

                            email?: boolean;

                              property hashtag

                              hashtag?: HashtagConfig;

                                property mention

                                mention?: MentionConfig;

                                  property newWindow

                                  newWindow?: boolean;

                                    property phone

                                    phone?: boolean;

                                      property replaceFn

                                      replaceFn?: ReplaceFn | null;

                                        property sanitizeHtml

                                        sanitizeHtml?: boolean;

                                          property stripPrefix

                                          stripPrefix?: StripPrefixConfig;

                                            property stripTrailingSlash

                                            stripTrailingSlash?: boolean;

                                              property truncate

                                              truncate?: TruncateConfig;

                                                property urls

                                                urls?: UrlsConfig;

                                                  interface EmailMatchConfig

                                                  interface EmailMatchConfig extends AbstractMatchConfig {}

                                                    property email

                                                    email: string;

                                                      interface HashtagMatchConfig

                                                      interface HashtagMatchConfig extends AbstractMatchConfig {}

                                                        property hashtag

                                                        hashtag: string;

                                                          property serviceName

                                                          serviceName: HashtagService;

                                                            interface HtmlTagCfg

                                                            interface HtmlTagCfg {}

                                                              property attrs

                                                              attrs?: {
                                                              [key: string]: string;
                                                              };

                                                                property innerHtml

                                                                innerHtml?: string;

                                                                  property innerHTML

                                                                  innerHTML?: string;

                                                                    property tagName

                                                                    tagName?: string;

                                                                      interface MentionMatchConfig

                                                                      interface MentionMatchConfig extends AbstractMatchConfig {}

                                                                        property mention

                                                                        mention: string;

                                                                          property serviceName

                                                                          serviceName: MentionService;

                                                                            interface ParseMatchesArgs

                                                                            interface ParseMatchesArgs {}

                                                                              property decodePercentEncoding

                                                                              decodePercentEncoding: boolean;

                                                                                property hashtagServiceName

                                                                                hashtagServiceName: HashtagService;

                                                                                  property mentionServiceName

                                                                                  mentionServiceName: MentionService;

                                                                                    property stripPrefix

                                                                                    stripPrefix: Required<StripPrefixConfigObj>;

                                                                                      property stripTrailingSlash

                                                                                      stripTrailingSlash: boolean;

                                                                                        property tagBuilder

                                                                                        tagBuilder: AnchorTagBuilder;

                                                                                          interface PhoneMatchConfig

                                                                                          interface PhoneMatchConfig extends AbstractMatchConfig {}

                                                                                            property number

                                                                                            number: string;

                                                                                              property plusSign

                                                                                              plusSign: boolean;

                                                                                                interface StripPrefixConfigObj

                                                                                                interface StripPrefixConfigObj {}

                                                                                                  property scheme

                                                                                                  scheme?: boolean;

                                                                                                    property www

                                                                                                    www?: boolean;

                                                                                                      interface TruncateConfigObj

                                                                                                      interface TruncateConfigObj {}

                                                                                                        property length

                                                                                                        length?: number;

                                                                                                          property location

                                                                                                          location?: 'end' | 'middle' | 'smart';

                                                                                                            interface UrlMatchConfig

                                                                                                            interface UrlMatchConfig extends AbstractMatchConfig {}

                                                                                                              property decodePercentEncoding

                                                                                                              decodePercentEncoding: boolean;

                                                                                                                property protocolRelativeMatch

                                                                                                                protocolRelativeMatch: boolean;

                                                                                                                  property stripPrefix

                                                                                                                  stripPrefix: Required<StripPrefixConfigObj>;

                                                                                                                    property stripTrailingSlash

                                                                                                                    stripTrailingSlash: boolean;

                                                                                                                      property url

                                                                                                                      url: string;

                                                                                                                        property urlMatchType

                                                                                                                        urlMatchType: UrlMatchType;

                                                                                                                          interface UrlsConfigObj

                                                                                                                          interface UrlsConfigObj {}

                                                                                                                            property ipV4Matches

                                                                                                                            ipV4Matches?: boolean;

                                                                                                                              property schemeMatches

                                                                                                                              schemeMatches?: boolean;

                                                                                                                                property tldMatches

                                                                                                                                tldMatches?: boolean;

                                                                                                                                  Type Aliases

                                                                                                                                  type HashtagConfig

                                                                                                                                  type HashtagConfig = false | HashtagService;

                                                                                                                                    type Match

                                                                                                                                    type Match = EmailMatch | HashtagMatch | MentionMatch | PhoneMatch | UrlMatch;

                                                                                                                                      type MatchType

                                                                                                                                      type MatchType = 'email' | 'hashtag' | 'mention' | 'phone' | 'url';

                                                                                                                                        type MentionConfig

                                                                                                                                        type MentionConfig = false | MentionService;

                                                                                                                                          type ReplaceFn

                                                                                                                                          type ReplaceFn = (match: Match) => ReplaceFnReturn;

                                                                                                                                            type ReplaceFnReturn

                                                                                                                                            type ReplaceFnReturn = boolean | string | HtmlTag | null | undefined | void;

                                                                                                                                              type StripPrefixConfig

                                                                                                                                              type StripPrefixConfig = boolean | StripPrefixConfigObj;

                                                                                                                                                type TruncateConfig

                                                                                                                                                type TruncateConfig = number | TruncateConfigObj;

                                                                                                                                                  type UrlMatchType

                                                                                                                                                  type UrlMatchType = 'scheme' | 'tld' | 'ipV4';

                                                                                                                                                    type UrlsConfig

                                                                                                                                                    type UrlsConfig = boolean | UrlsConfigObj;

                                                                                                                                                      Package Files (12)

                                                                                                                                                      Dependencies (1)

                                                                                                                                                      Dev Dependencies (48)

                                                                                                                                                      Peer Dependencies (0)

                                                                                                                                                      No peer dependencies.

                                                                                                                                                      Badge

                                                                                                                                                      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                                                                      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/autolinker.

                                                                                                                                                      • Markdown
                                                                                                                                                        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/autolinker)
                                                                                                                                                      • HTML
                                                                                                                                                        <a href="https://www.jsdocs.io/package/autolinker"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>