UNPKG

autolinker

Version:

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

1 lines 8.32 kB
{"version":3,"file":"abstract-match.js","sourceRoot":"","sources":["../../../src/match/abstract-match.ts"],"names":[],"mappings":";;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH;IAqCI;;;;;OAKG;IACH,uBAAY,GAAwB;QAhCpC;;;;;WAKG;QACH,mHAAmH;QAC3G,MAAC,GAAG,IAAI,CAAC;QAKjB;;;;WAIG;QACgB,gBAAW,GAAW,EAAE,CAAC,CAAC,gGAAgG;QAE7I;;;;WAIG;QACK,WAAM,GAAW,CAAC,CAAC,CAAC,gGAAgG;QASxH,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC;QACjC,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC,WAAW,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;IAC7B,CAAC;IAYD;;;;OAIG;IACI,sCAAc,GAArB;QACI,OAAO,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,iCAAS,GAAT,UAAU,MAAc;QACpB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED;;;;;OAKG;IACI,iCAAS,GAAhB;QACI,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAkBD;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,2CAAmB,GAA1B;QACI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACI,gCAAQ,GAAf;QACI,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;IACL,oBAAC;AAAD,CAAC,AAxKD,IAwKC;AAxKqB,sCAAa","sourcesContent":["import { AnchorTagBuilder } from '../anchor-tag-builder';\nimport { HtmlTag } from '../html-tag';\nimport { MatchType } from './match';\n\n/**\n * @abstract\n * @class Autolinker.match.AbstractMatch\n *\n * Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a\n * {@link Autolinker#replaceFn replaceFn}, and may be used to query for details about the match.\n *\n * For example:\n *\n * var input = \"...\"; // string with URLs, Email Addresses, and Mentions (Twitter, Instagram, Soundcloud)\n *\n * var linkedText = Autolinker.link( input, {\n * replaceFn : function( match ) {\n * console.log( \"href = \", match.getAnchorHref() );\n * console.log( \"text = \", match.getAnchorText() );\n *\n * switch( match.getType() ) {\n * case 'url' :\n * console.log( \"url: \", match.getUrl() );\n *\n * case 'email' :\n * console.log( \"email: \", match.getEmail() );\n *\n * case 'mention' :\n * console.log( \"mention: \", match.getMention() );\n * }\n * }\n * } );\n *\n * See the {@link Autolinker} class for more details on using the {@link Autolinker#replaceFn replaceFn}.\n */\nexport abstract class AbstractMatch {\n /**\n * @public\n * @property {'url'/'email'/'hashtag'/'mention'/'phone'} type\n *\n * A string name for the type of match that this class represents. Can be\n * used in a TypeScript discriminating union to type-narrow from the\n * `Match` type.\n */\n public abstract readonly type: MatchType;\n\n /**\n * @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)\n *\n * Reference to the AnchorTagBuilder instance to use to generate an anchor\n * tag for the Match.\n */\n // @ts-expect-error Property used just to get the above doc comment into the ES5 output and documentation generator\n private _ = null;\n\n // Actual property for the above jsdoc comment\n private readonly tagBuilder: AnchorTagBuilder;\n\n /**\n * @cfg {String} matchedText (required)\n *\n * The original text that was matched by the {@link Autolinker.matcher.Matcher}.\n */\n protected readonly matchedText: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @cfg {Number} offset (required)\n *\n * The offset of where the match was made in the input string.\n */\n private offset: number = 0; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @member Autolinker.match.Match\n * @method constructor\n * @param {Object} cfg The configuration properties for the Match\n * instance, specified in an Object (map).\n */\n constructor(cfg: AbstractMatchConfig) {\n this.tagBuilder = cfg.tagBuilder;\n this.matchedText = cfg.matchedText;\n this.offset = cfg.offset;\n }\n\n /**\n * Returns a string name for the type of match that this class represents.\n *\n * @deprecated Use {@link #type} instead which can assist in type-narrowing\n * for TypeScript.\n * @abstract\n * @return {String}\n */\n public abstract getType(): MatchType;\n\n /**\n * Returns the original text that was matched.\n *\n * @return {String}\n */\n public getMatchedText(): string {\n return this.matchedText;\n }\n\n /**\n * Sets the {@link #offset} of where the match was made in the input string.\n *\n * A {@link Autolinker.matcher.Matcher} will be fed only HTML text nodes,\n * and will therefore set an original offset that is relative to the HTML\n * text node itself. However, we want this offset to be relative to the full\n * HTML input string, and thus if using {@link Autolinker#parse} (rather\n * than calling a {@link Autolinker.matcher.Matcher} directly), then this\n * offset is corrected after the Matcher itself has done its job.\n *\n * @private\n * @param {Number} offset\n */\n setOffset(offset: number): void {\n this.offset = offset;\n }\n\n /**\n * Returns the offset of where the match was made in the input string. This\n * is the 0-based index of the match.\n *\n * @return {Number}\n */\n public getOffset(): number {\n return this.offset;\n }\n\n /**\n * Returns the anchor href that should be generated for the match.\n *\n * @abstract\n * @return {String}\n */\n public abstract getAnchorHref(): string;\n\n /**\n * Returns the anchor text that should be generated for the match.\n *\n * @abstract\n * @return {String}\n */\n public abstract getAnchorText(): string;\n\n /**\n * Returns the CSS class suffix(es) for this match.\n *\n * A CSS class suffix is appended to the {@link Autolinker#className} in\n * the {@link Autolinker.AnchorTagBuilder} when a match is translated into\n * an anchor tag.\n *\n * For example, if {@link Autolinker#className} was configured as 'myLink',\n * and this method returns `[ 'url' ]`, the final class name of the element\n * will become: 'myLink myLink-url'.\n *\n * The match may provide multiple CSS class suffixes to be appended to the\n * {@link Autolinker#className} in order to facilitate better styling\n * options for different match criteria. See {@link Autolinker.match.Mention}\n * for an example.\n *\n * By default, this method returns a single array with the match's\n * {@link #getType type} name, but may be overridden by subclasses.\n *\n * @return {String[]}\n */\n public getCssClassSuffixes(): string[] {\n return [this.type];\n }\n\n /**\n * Builds and returns an {@link Autolinker.HtmlTag} instance based on the\n * Match.\n *\n * This can be used to easily generate anchor tags from matches, and either\n * return their HTML string, or modify them before doing so.\n *\n * Example Usage:\n *\n * var tag = match.buildTag();\n * tag.addClass( 'cordova-link' );\n * tag.setAttr( 'target', '_system' );\n *\n * tag.toAnchorString(); // <a href=\"http://google.com\" class=\"cordova-link\" target=\"_system\">Google</a>\n *\n * Example Usage in {@link Autolinker#replaceFn}:\n *\n * var html = Autolinker.link( \"Test google.com\", {\n * replaceFn : function( match ) {\n * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance\n * tag.setAttr( 'rel', 'nofollow' );\n *\n * return tag;\n * }\n * } );\n *\n * // generated html:\n * // Test <a href=\"http://google.com\" target=\"_blank\" rel=\"nofollow\">google.com</a>\n */\n public buildTag(): HtmlTag {\n return this.tagBuilder.build(this);\n }\n}\n\nexport interface AbstractMatchConfig {\n tagBuilder: AnchorTagBuilder;\n matchedText: string;\n offset: number;\n}\n"]}