autolinker

  • Version 3.14.3
  • Published
  • 1.99 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

Classes

Interfaces

Type Aliases

Classes

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: Match) => 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. {Autolinker.HtmlTag} The HtmlTag instance for the anchor tag.

method createAttrs

protected createAttrs: (match: Match) => { [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. {Object} A key/value Object (map) of the anchor tag's attributes.

method createCssClass

protected createCssClass: (match: Match) => 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. {String} 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 AnchorTagBuilder

static readonly AnchorTagBuilder: typeof AnchorTagBuilder;
  • For backwards compatibility with Autolinker 1.x, the AnchorTagBuilder class is provided as a static on the Autolinker class.

property HtmlTag

static readonly HtmlTag: typeof HtmlTag;
  • For backwards compatibility with Autolinker 1.x, the HtmlTag class is provided as a static on the Autolinker class.

property match

static readonly match: {
Email: typeof EmailMatch;
Hashtag: typeof HashtagMatch;
Match: typeof Match;
Mention: typeof MentionMatch;
Phone: typeof PhoneMatch;
Url: typeof UrlMatch;
};
  • For backwards compatibility with Autolinker 1.x, the Match classes are provided as statics on the Autolinker class.

property matcher

static readonly matcher: {
Email: typeof EmailMatcher;
Hashtag: typeof HashtagMatcher;
Matcher: typeof Matcher;
Mention: typeof MentionMatcher;
Phone: typeof PhoneMatcher;
Url: typeof UrlMatcher;
};
  • For backwards compatibility with Autolinker 1.x, the Matcher classes are provided as statics on the Autolinker class.

property version

static readonly version: string;
  • {String} version

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

    Ex: 0.25.1

property version

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

    Ex: 0.25.1

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 Match {}
  • Autolinker.match.Email Autolinker.match.Match

    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).

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: () => string;
  • Returns a string name for the type of match that this class represents. For the case of EmailMatch, returns 'email'.

    {String}

class EmailMatcher

class EmailMatcher extends Matcher {}
  • Autolinker.matcher.Email Autolinker.matcher.Matcher

    Matcher to find email matches in an input string.

    See this class's superclass (Autolinker.matcher.Matcher) for more details.

property localPartCharRegex

protected localPartCharRegex: RegExp;
  • Valid characters that can be used in the "local" part of an email address, i.e. the "name" part of "name@site.com"

property strictTldRegex

protected strictTldRegex: RegExp;
  • Stricter TLD regex which adds a beginning and end check to ensure the string is a valid TLD

method parseMatches

parseMatches: (text: string) => Match[];

class HashtagMatch

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

    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).

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 getHashtag

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

    {String}

method getServiceName

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

    {String}

method getType

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

    {String}

class HashtagMatcher

class HashtagMatcher extends Matcher {}
  • Autolinker.matcher.Hashtag Autolinker.matcher.Matcher

    Matcher to find HashtagMatch matches in an input string.

constructor

constructor(cfg: HashtagMatcherConfig);
  • constructor

    Parameter cfg

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

property matcherRegex

protected matcherRegex: RegExp;
  • The regular expression to match Hashtags. Example match:

    #asdf

    {RegExp} matcherRegex

property nonWordCharRegex

protected nonWordCharRegex: RegExp;
  • The regular expression to use to check the character before a username match to make sure we didn't accidentally match an email address.

    For example, the string "asdf@asdf.com" should not match "@asdf" as a username.

    {RegExp} nonWordCharRegex

property serviceName

protected readonly serviceName: HashtagServices;
  • {String} serviceName

    The service to point hashtag matches to. See Autolinker#hashtag for available values.

method parseMatches

parseMatches: (text: string) => Match[];

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)

property whitespaceRegex

protected whitespaceRegex: RegExp;
  • {RegExp} whitespaceRegex

    Regular expression used to match whitespace in a string of CSS classes.

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;
  • Override of superclass method used to generate the HTML string for the tag.

    {String}

class Match

abstract class Match {}
  • Autolinker.match.Match

    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: MatchConfig);
  • Autolinker.match.Match constructor

    Parameter cfg

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

property matchedText

protected readonly matchedText: string;

method buildTag

buildTag: () => import('..').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: () => string;
  • Returns a string name for the type of match that this class represents.

    {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 Matcher

abstract class Matcher {}
  • Autolinker.matcher.Matcher

    An abstract class and interface for individual matchers to find matches in an input string with linkified versions of them.

    Note that Matchers do not take HTML into account - they must be fed the text nodes of any HTML string, which is handled by Autolinker#parse.

constructor

constructor(cfg: MatcherConfig);
  • constructor

    Parameter cfg

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

property tagBuilder

protected tagBuilder: AnchorTagBuilder;

    method parseMatches

    abstract parseMatches: (text: string) => Match[];
    • Parses the input text and returns the array of for the matcher.

      Parameter text

      The text to scan and replace matches in. {Autolinker.match.Match[]}

    class MentionMatch

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

      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).

    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: () => MentionServices;
    • Returns the configured to point the mention to. Ex: 'instagram', 'twitter', 'soundcloud'.

      {String}

    method getType

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

      {String}

    class MentionMatcher

    class MentionMatcher extends Matcher {}
    • Autolinker.matcher.Mention Autolinker.matcher.Matcher

      Matcher to find/replace username matches in an input string.

    constructor

    constructor(cfg: MentionMatcherConfig);
    • constructor

      Parameter cfg

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

    property matcherRegexes

    protected readonly matcherRegexes: { [key: string]: RegExp };
    • Hash of regular expression to match username handles. Example match:

      {Object} matcherRegexes

    property nonWordCharRegex

    protected readonly nonWordCharRegex: RegExp;
    • The regular expression to use to check the character before a username match to make sure we didn't accidentally match an email address.

      For example, the string "asdf@asdf.com" should not match "@asdf" as a username.

      {RegExp} nonWordCharRegex

    property serviceName

    protected serviceName: MentionServices;
    • {'twitter'/'instagram'/'soundcloud'} protected

      The name of service to link to.

      Valid values are: 'twitter', 'instagram', or 'soundcloud'

    method parseMatches

    parseMatches: (text: string) => Match[];

    class PhoneMatch

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

      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).

    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: () => string;
    • Returns a string name for the type of match that this class represents. For the case of PhoneMatch, returns 'phone'.

      {String}

    class PhoneMatcher

    class PhoneMatcher extends Matcher {}
    • Autolinker.matcher.Phone Autolinker.matcher.Matcher

      Matcher to find Phone number matches in an input string.

      See this class's superclass (Autolinker.matcher.Matcher) for more details.

    property matcherRegex

    protected matcherRegex: RegExp;
    • The regular expression to match Phone numbers. Example matches:

      (123) 456-7890 123 456 7890 123-456-7890 +18004441234,,;,10226420346# +1 (800) 444 1234 10226420346# 1-800-444-1234,1022,64,20346#

      This regular expression has the following capturing groups:

      1 or 2. The prefixed '+' sign, if there is one.

      {RegExp} matcherRegex

    method parseMatches

    parseMatches: (text: string) => Match[];

    method testMatch

    protected testMatch: (text: string) => boolean;

      class UrlMatch

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

        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 protocolPrepended

      protocolPrepended: boolean;
      • {Boolean} protocolPrepended

        Will be set to true if the 'http://' protocol has been prepended to the (because the did not have a protocol)

      property protocolRelativeRegex

      protocolRelativeRegex: RegExp;
      • {RegExp} protocolRelativeRegex

        The regular expression used to remove the protocol-relative '//' from the string, for purposes of . A protocol-relative URL is, for example, "//yahoo.com"

      property schemePrefixRegex

      schemePrefixRegex: RegExp;
      • {RegExp} schemePrefixRegex

        A regular expression used to remove the 'http://' or 'https://' from URLs.

      property wwwPrefixRegex

      wwwPrefixRegex: RegExp;
      • {RegExp} wwwPrefixRegex

        A regular expression used to remove the 'www.' from URLs.

      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: () => string;
      • 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: () => UrlMatchTypeOptions;
      • 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"}

      class UrlMatcher

      class UrlMatcher extends Matcher {}
      • Autolinker.matcher.Url Autolinker.matcher.Matcher

        Matcher to find URL matches in an input string.

        See this class's superclass (Autolinker.matcher.Matcher) for more details.

      constructor

      constructor(cfg: UrlMatcherConfig);
      • constructor

        Parameter cfg

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

      property decodePercentEncoding

      protected decodePercentEncoding: boolean;
      • {Boolean} decodePercentEncoding (required) Autolinker#decodePercentEncoding

      property matcherRegex

      protected matcherRegex: RegExp;
      • {RegExp} matcherRegex

        The regular expression to match URLs with an optional scheme, port number, path, query string, and hash anchor.

        Example matches:

        http://google.com www.google.com google.com/path/to/file?q1=1&q2=2#myAnchor

        This regular expression will have the following capturing groups:

        1. Group that matches a scheme-prefixed URL (i.e. 'http://google.com'). This is used to match scheme URLs with just a single word, such as 'http://localhost', where we won't double check that the domain name has at least one dot ('.') in it. 2. Group that matches a 'www.' prefixed URL. This is only matched if the 'www.' text was not prefixed by a scheme (i.e.: not prefixed by 'http://', 'ftp:', etc.) 3. A protocol-relative ('//') match for the case of a 'www.' prefixed URL. Will be an empty string if it is not a protocol-relative match. We need to know the character before the '//' in order to determine if it is a valid match or the // was in a string we don't want to auto-link. 4. Group that matches a known TLD (top level domain), when a scheme or 'www.'-prefixed domain is not matched. 5. A protocol-relative ('//') match for the case of a known TLD prefixed URL. Will be an empty string if it is not a protocol-relative match. See #3 for more info.

      property stripPrefix

      protected stripPrefix: Required<StripPrefixConfigObj>;
      • {Object} stripPrefix (required)

        The Object form of .

      property stripTrailingSlash

      protected stripTrailingSlash: boolean;
      • {Boolean} stripTrailingSlash (required) Autolinker#stripTrailingSlash

      property wordCharRegExp

      protected wordCharRegExp: RegExp;
      • A regular expression to use to check the character before a protocol-relative URL match. We don't want to match a protocol-relative URL if it is part of another word.

        For example, we want to match something like "Go to: //google.com", but we don't want to match something like "abc//google.com"

        This regular expression is used to test the character before the '//'.

        {RegExp} wordCharRegExp

      method matchHasInvalidCharAfterTld

      protected matchHasInvalidCharAfterTld: (
      urlMatch: string,
      schemeUrlMatch: string
      ) => number;
      • Determine if there's an invalid character after the TLD in a URL. Valid characters after TLD are ':/?#'. Exclude scheme matched URLs from this check.

        Parameter urlMatch

        The matched URL, if there was one. Will be an empty string if the match is not a URL match.

        Parameter schemeUrlMatch

        The match URL string for a scheme match. Ex: 'http://yahoo.com'. This is used to match something like 'http://localhost', where we won't double check that the domain name has at least one '.' in it. {Number} the position where the invalid character was found. If no such character was found, returns -1

      method matchHasUnbalancedClosingParen

      protected matchHasUnbalancedClosingParen: (matchStr: string) => boolean;
      • Determines if a match found has an unmatched closing parenthesis, square bracket or curly bracket. If so, the symbol will be removed from the match itself, and appended after the generated anchor tag.

        A match may have an extra closing parenthesis at the end of the match because the regular expression must include parenthesis for URLs such as "wikipedia.com/something_(disambiguation)", which 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 return true.

        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 closing square bracket should not be part of the URL itself, and this method will return true.

        Parameter matchStr

        The full match string from the . {Boolean} true if there is an unbalanced closing parenthesis or square bracket at the end of the matchStr, false otherwise.

      method parseMatches

      parseMatches: (text: string) => Match[];

      Interfaces

      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?: any;

                    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 MatchConfig {}

                                              property email

                                              email: string;

                                                interface HashtagMatchConfig

                                                interface HashtagMatchConfig extends MatchConfig {}

                                                  property hashtag

                                                  hashtag: string;

                                                    property serviceName

                                                    serviceName: string;

                                                      interface HashtagMatcherConfig

                                                      interface HashtagMatcherConfig extends MatcherConfig {}

                                                        property serviceName

                                                        serviceName: HashtagServices;

                                                          interface HtmlTagCfg

                                                          interface HtmlTagCfg {}

                                                            property attrs

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

                                                              property innerHtml

                                                              innerHtml?: string;

                                                                property innerHTML

                                                                innerHTML?: string;

                                                                  property tagName

                                                                  tagName?: string;

                                                                    interface MatchConfig

                                                                    interface MatchConfig {}

                                                                      property matchedText

                                                                      matchedText: string;

                                                                        property offset

                                                                        offset: number;

                                                                          property tagBuilder

                                                                          tagBuilder: AnchorTagBuilder;

                                                                            interface MatcherConfig

                                                                            interface MatcherConfig {}

                                                                              property tagBuilder

                                                                              tagBuilder: AnchorTagBuilder;

                                                                                interface MentionMatchConfig

                                                                                interface MentionMatchConfig extends MatchConfig {}

                                                                                  property mention

                                                                                  mention: string;

                                                                                    property serviceName

                                                                                    serviceName: MentionServices;

                                                                                      interface MentionMatcherConfig

                                                                                      interface MentionMatcherConfig extends MatcherConfig {}

                                                                                        property serviceName

                                                                                        serviceName: MentionServices;

                                                                                          interface PhoneMatchConfig

                                                                                          interface PhoneMatchConfig extends MatchConfig {}

                                                                                            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 MatchConfig {}

                                                                                                              property decodePercentEncoding

                                                                                                              decodePercentEncoding: boolean;

                                                                                                                property protocolRelativeMatch

                                                                                                                protocolRelativeMatch: boolean;

                                                                                                                  property protocolUrlMatch

                                                                                                                  protocolUrlMatch: boolean;

                                                                                                                    property stripPrefix

                                                                                                                    stripPrefix: Required<StripPrefixConfigObj>;

                                                                                                                      property stripTrailingSlash

                                                                                                                      stripTrailingSlash: boolean;

                                                                                                                        property url

                                                                                                                        url: string;

                                                                                                                          property urlMatchType

                                                                                                                          urlMatchType: UrlMatchTypeOptions;

                                                                                                                            interface UrlMatcherConfig

                                                                                                                            interface UrlMatcherConfig extends MatcherConfig {}

                                                                                                                              property decodePercentEncoding

                                                                                                                              decodePercentEncoding: boolean;

                                                                                                                                property stripPrefix

                                                                                                                                stripPrefix: Required<StripPrefixConfigObj>;

                                                                                                                                  property stripTrailingSlash

                                                                                                                                  stripTrailingSlash: boolean;

                                                                                                                                    interface UrlsConfigObj

                                                                                                                                    interface UrlsConfigObj {}

                                                                                                                                      property schemeMatches

                                                                                                                                      schemeMatches?: boolean;

                                                                                                                                        property tldMatches

                                                                                                                                        tldMatches?: boolean;

                                                                                                                                          property wwwMatches

                                                                                                                                          wwwMatches?: boolean;

                                                                                                                                            Type Aliases

                                                                                                                                            type HashtagConfig

                                                                                                                                            type HashtagConfig = false | HashtagServices;

                                                                                                                                              type HashtagServices

                                                                                                                                              type HashtagServices = 'twitter' | 'facebook' | 'instagram';

                                                                                                                                                type MentionConfig

                                                                                                                                                type MentionConfig = false | MentionServices;

                                                                                                                                                  type MentionServices

                                                                                                                                                  type MentionServices = 'twitter' | 'instagram' | 'soundcloud';

                                                                                                                                                    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 UrlMatchTypeOptions

                                                                                                                                                            type UrlMatchTypeOptions = 'scheme' | 'www' | 'tld';

                                                                                                                                                              type UrlsConfig

                                                                                                                                                              type UrlsConfig = boolean | UrlsConfigObj;

                                                                                                                                                                Package Files (16)

                                                                                                                                                                Dependencies (1)

                                                                                                                                                                Dev Dependencies (39)

                                                                                                                                                                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>