Add existing to tracked

This commit is contained in:
Jay
2026-08-11 09:53:42 -04:00
parent afe07f3055
commit ffd6e3d73c
8531 changed files with 4396230 additions and 0 deletions
+179
View File
@@ -0,0 +1,179 @@
import { AnchorTagBuilder } from '../anchor-tag-builder';
import { HtmlTag } from '../html-tag';
import { MatchType } from './match';
/**
* @abstract
* @class Autolinker.match.AbstractMatch
*
* Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a
* {@link Autolinker#replaceFn replaceFn}, 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 {@link Autolinker} class for more details on using the {@link Autolinker#replaceFn replaceFn}.
*/
export declare abstract class AbstractMatch {
/**
* @public
* @property {'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.
*/
abstract readonly type: MatchType;
/**
* @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)
*
* Reference to the AnchorTagBuilder instance to use to generate an anchor
* tag for the Match.
*/
private _;
private readonly tagBuilder;
/**
* @cfg {String} matchedText (required)
*
* The original text that was matched by the {@link Autolinker.matcher.Matcher}.
*/
protected readonly matchedText: string;
/**
* @cfg {Number} offset (required)
*
* The offset of where the match was made in the input string.
*/
private offset;
/**
* @member Autolinker.match.Match
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
constructor(cfg: AbstractMatchConfig);
/**
* Returns a string name for the type of match that this class represents.
*
* @deprecated Use {@link #type} instead which can assist in type-narrowing
* for TypeScript.
* @abstract
* @return {String}
*/
abstract getType(): MatchType;
/**
* Returns the original text that was matched.
*
* @return {String}
*/
getMatchedText(): string;
/**
* Sets the {@link #offset} of where the match was made in the input string.
*
* A {@link 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 {@link Autolinker#parse} (rather
* than calling a {@link Autolinker.matcher.Matcher} directly), then this
* offset is corrected after the Matcher itself has done its job.
*
* @private
* @param {Number} offset
*/
setOffset(offset: number): void;
/**
* Returns the offset of where the match was made in the input string. This
* is the 0-based index of the match.
*
* @return {Number}
*/
getOffset(): number;
/**
* Returns the anchor href that should be generated for the match.
*
* @abstract
* @return {String}
*/
abstract getAnchorHref(): string;
/**
* Returns the anchor text that should be generated for the match.
*
* @abstract
* @return {String}
*/
abstract getAnchorText(): string;
/**
* Returns the CSS class suffix(es) for this match.
*
* A CSS class suffix is appended to the {@link Autolinker#className} in
* the {@link Autolinker.AnchorTagBuilder} when a match is translated into
* an anchor tag.
*
* For example, if {@link 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
* {@link Autolinker#className} in order to facilitate better styling
* options for different match criteria. See {@link Autolinker.match.Mention}
* for an example.
*
* By default, this method returns a single array with the match's
* {@link #getType type} name, but may be overridden by subclasses.
*
* @return {String[]}
*/
getCssClassSuffixes(): string[];
/**
* Builds and returns an {@link 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(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
*
* Example Usage in {@link Autolinker#replaceFn}:
*
* var html = Autolinker.link( "Test google.com", {
* replaceFn : function( match ) {
* var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
* tag.setAttr( 'rel', 'nofollow' );
*
* return tag;
* }
* } );
*
* // generated html:
* // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
*/
buildTag(): HtmlTag;
}
export interface AbstractMatchConfig {
tagBuilder: AnchorTagBuilder;
matchedText: string;
offset: number;
}
+159
View File
@@ -0,0 +1,159 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractMatch = void 0;
/**
* @abstract
* @class Autolinker.match.AbstractMatch
*
* Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a
* {@link Autolinker#replaceFn replaceFn}, 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 {@link Autolinker} class for more details on using the {@link Autolinker#replaceFn replaceFn}.
*/
var AbstractMatch = /** @class */ (function () {
/**
* @member Autolinker.match.Match
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
function AbstractMatch(cfg) {
/**
* @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)
*
* Reference to the AnchorTagBuilder instance to use to generate an anchor
* tag for the Match.
*/
// @ts-expect-error Property used just to get the above doc comment into the ES5 output and documentation generator
this._ = null;
/**
* @cfg {String} matchedText (required)
*
* The original text that was matched by the {@link Autolinker.matcher.Matcher}.
*/
this.matchedText = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {Number} offset (required)
*
* The offset of where the match was made in the input string.
*/
this.offset = 0; // default value just to get the above doc comment in the ES5 output and documentation generator
this.tagBuilder = cfg.tagBuilder;
this.matchedText = cfg.matchedText;
this.offset = cfg.offset;
}
/**
* Returns the original text that was matched.
*
* @return {String}
*/
AbstractMatch.prototype.getMatchedText = function () {
return this.matchedText;
};
/**
* Sets the {@link #offset} of where the match was made in the input string.
*
* A {@link 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 {@link Autolinker#parse} (rather
* than calling a {@link Autolinker.matcher.Matcher} directly), then this
* offset is corrected after the Matcher itself has done its job.
*
* @private
* @param {Number} offset
*/
AbstractMatch.prototype.setOffset = function (offset) {
this.offset = offset;
};
/**
* Returns the offset of where the match was made in the input string. This
* is the 0-based index of the match.
*
* @return {Number}
*/
AbstractMatch.prototype.getOffset = function () {
return this.offset;
};
/**
* Returns the CSS class suffix(es) for this match.
*
* A CSS class suffix is appended to the {@link Autolinker#className} in
* the {@link Autolinker.AnchorTagBuilder} when a match is translated into
* an anchor tag.
*
* For example, if {@link 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
* {@link Autolinker#className} in order to facilitate better styling
* options for different match criteria. See {@link Autolinker.match.Mention}
* for an example.
*
* By default, this method returns a single array with the match's
* {@link #getType type} name, but may be overridden by subclasses.
*
* @return {String[]}
*/
AbstractMatch.prototype.getCssClassSuffixes = function () {
return [this.type];
};
/**
* Builds and returns an {@link 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(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
*
* Example Usage in {@link Autolinker#replaceFn}:
*
* var html = Autolinker.link( "Test google.com", {
* replaceFn : function( match ) {
* var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
* tag.setAttr( 'rel', 'nofollow' );
*
* return tag;
* }
* } );
*
* // generated html:
* // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
*/
AbstractMatch.prototype.buildTag = function () {
return this.tagBuilder.build(this);
};
return AbstractMatch;
}());
exports.AbstractMatch = AbstractMatch;
//# sourceMappingURL=abstract-match.js.map
File diff suppressed because one or more lines are too long
+60
View File
@@ -0,0 +1,60 @@
import { AbstractMatchConfig, AbstractMatch } from './abstract-match';
/**
* @class Autolinker.match.Email
* @extends Autolinker.match.AbstractMatch
*
* Represents a Email match found in an input string which should be Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
*/
export declare class EmailMatch extends AbstractMatch {
/**
* @public
* @property {'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.
*/
readonly type: "email";
/**
* @cfg {String} email (required)
*
* The email address that was matched.
*/
private readonly email;
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
constructor(cfg: EmailMatchConfig);
/**
* Returns a string name for the type of match that this class represents.
* For the case of EmailMatch, returns 'email'.
*
* @return {String}
*/
getType(): 'email';
/**
* Returns the email address that was matched.
*
* @return {String}
*/
getEmail(): string;
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
getAnchorHref(): string;
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
getAnchorText(): string;
}
export interface EmailMatchConfig extends AbstractMatchConfig {
email: string;
}
+77
View File
@@ -0,0 +1,77 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailMatch = void 0;
var tslib_1 = require("tslib");
var abstract_match_1 = require("./abstract-match");
/**
* @class Autolinker.match.Email
* @extends Autolinker.match.AbstractMatch
*
* Represents a Email match found in an input string which should be Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
*/
var EmailMatch = /** @class */ (function (_super) {
tslib_1.__extends(EmailMatch, _super);
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
function EmailMatch(cfg) {
var _this = _super.call(this, cfg) || this;
/**
* @public
* @property {'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.
*/
_this.type = 'email';
/**
* @cfg {String} email (required)
*
* The email address that was matched.
*/
_this.email = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
_this.email = cfg.email;
return _this;
}
/**
* Returns a string name for the type of match that this class represents.
* For the case of EmailMatch, returns 'email'.
*
* @return {String}
*/
EmailMatch.prototype.getType = function () {
return 'email';
};
/**
* Returns the email address that was matched.
*
* @return {String}
*/
EmailMatch.prototype.getEmail = function () {
return this.email;
};
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
EmailMatch.prototype.getAnchorHref = function () {
return 'mailto:' + this.email;
};
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
EmailMatch.prototype.getAnchorText = function () {
return this.email;
};
return EmailMatch;
}(abstract_match_1.AbstractMatch));
exports.EmailMatch = EmailMatch;
//# sourceMappingURL=email-match.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"email-match.js","sourceRoot":"","sources":["../../../src/match/email-match.ts"],"names":[],"mappings":";;;;AAAA,mDAAsE;AAEtE;;;;;;;GAOG;AACH;IAAgC,sCAAa;IAkBzC;;;;OAIG;IACH,oBAAY,GAAqB;QAC7B,YAAA,MAAK,YAAC,GAAG,CAAC,SAAC;QAvBf;;;;;;;WAOG;QACa,UAAI,GAAG,OAAgB,CAAC;QAExC;;;;WAIG;QACc,WAAK,GAAW,EAAE,CAAC,CAAC,gGAAgG;QAUjI,KAAI,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC;;IAC3B,CAAC;IAED;;;;;OAKG;IACH,4BAAO,GAAP;QACI,OAAO,OAAO,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACH,6BAAQ,GAAR;QACI,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACH,kCAAa,GAAb;QACI,OAAO,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,kCAAa,GAAb;QACI,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IACL,iBAAC;AAAD,CAAC,AAjED,CAAgC,8BAAa,GAiE5C;AAjEY,gCAAU","sourcesContent":["import { AbstractMatchConfig, AbstractMatch } from './abstract-match';\n\n/**\n * @class Autolinker.match.Email\n * @extends Autolinker.match.AbstractMatch\n *\n * Represents a Email match found in an input string which should be Autolinked.\n *\n * See this class's superclass ({@link Autolinker.match.Match}) for more details.\n */\nexport class EmailMatch extends AbstractMatch {\n /**\n * @public\n * @property {'email'} 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 readonly type = 'email' as const;\n\n /**\n * @cfg {String} email (required)\n *\n * The email address that was matched.\n */\n private readonly email: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @method constructor\n * @param {Object} cfg The configuration properties for the Match\n * instance, specified in an Object (map).\n */\n constructor(cfg: EmailMatchConfig) {\n super(cfg);\n\n this.email = cfg.email;\n }\n\n /**\n * Returns a string name for the type of match that this class represents.\n * For the case of EmailMatch, returns 'email'.\n *\n * @return {String}\n */\n getType(): 'email' {\n return 'email';\n }\n\n /**\n * Returns the email address that was matched.\n *\n * @return {String}\n */\n getEmail() {\n return this.email;\n }\n\n /**\n * Returns the anchor href that should be generated for the match.\n *\n * @return {String}\n */\n getAnchorHref() {\n return 'mailto:' + this.email;\n }\n\n /**\n * Returns the anchor text that should be generated for the match.\n *\n * @return {String}\n */\n getAnchorText() {\n return this.email;\n }\n}\n\nexport interface EmailMatchConfig extends AbstractMatchConfig {\n email: string;\n}\n"]}
+86
View File
@@ -0,0 +1,86 @@
import { HashtagService } from '../parser/hashtag-utils';
import { AbstractMatch, AbstractMatchConfig } from './abstract-match';
/**
* @class Autolinker.match.Hashtag
* @extends Autolinker.match.AbstractMatch
*
* Represents a Hashtag match found in an input string which should be
* Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more
* details.
*/
export declare class HashtagMatch extends AbstractMatch {
/**
* @public
* @property {'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.
*/
readonly type: "hashtag";
/**
* @cfg {String} serviceName
*
* The service to point hashtag matches to. See {@link Autolinker#hashtag}
* for available values.
*/
private readonly serviceName;
/**
* @cfg {String} hashtag (required)
*
* The HashtagMatch that was matched, without the '#'.
*/
private readonly hashtag;
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
constructor(cfg: HashtagMatchConfig);
/**
* Returns a string name for the type of match that this class represents.
* For the case of HashtagMatch, returns 'hashtag'.
*
* @return {String}
*/
getType(): 'hashtag';
/**
* Returns the configured {@link #serviceName} to point the HashtagMatch to.
* Ex: 'facebook', 'twitter'.
*
* @return {String}
*/
getServiceName(): HashtagService;
/**
* Returns the matched hashtag, without the '#' character.
*
* @return {String}
*/
getHashtag(): string;
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
getAnchorHref(): string;
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
getAnchorText(): string;
/**
* Returns the CSS class suffixes that should be used on a tag built with
* the match. See {@link Autolinker.match.Match#getCssClassSuffixes} for
* details.
*
* @return {String[]}
*/
getCssClassSuffixes(): string[];
}
export interface HashtagMatchConfig extends AbstractMatchConfig {
serviceName: HashtagService;
hashtag: string;
}
+127
View File
@@ -0,0 +1,127 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HashtagMatch = void 0;
var tslib_1 = require("tslib");
var utils_1 = require("../utils");
var abstract_match_1 = require("./abstract-match");
/**
* @class Autolinker.match.Hashtag
* @extends Autolinker.match.AbstractMatch
*
* Represents a Hashtag match found in an input string which should be
* Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more
* details.
*/
var HashtagMatch = /** @class */ (function (_super) {
tslib_1.__extends(HashtagMatch, _super);
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
function HashtagMatch(cfg) {
var _this = _super.call(this, cfg) || this;
/**
* @public
* @property {'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.
*/
_this.type = 'hashtag';
/**
* @cfg {String} serviceName
*
* The service to point hashtag matches to. See {@link Autolinker#hashtag}
* for available values.
*/
_this.serviceName = 'twitter'; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {String} hashtag (required)
*
* The HashtagMatch that was matched, without the '#'.
*/
_this.hashtag = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
_this.serviceName = cfg.serviceName;
_this.hashtag = cfg.hashtag;
return _this;
}
/**
* Returns a string name for the type of match that this class represents.
* For the case of HashtagMatch, returns 'hashtag'.
*
* @return {String}
*/
HashtagMatch.prototype.getType = function () {
return 'hashtag';
};
/**
* Returns the configured {@link #serviceName} to point the HashtagMatch to.
* Ex: 'facebook', 'twitter'.
*
* @return {String}
*/
HashtagMatch.prototype.getServiceName = function () {
return this.serviceName;
};
/**
* Returns the matched hashtag, without the '#' character.
*
* @return {String}
*/
HashtagMatch.prototype.getHashtag = function () {
return this.hashtag;
};
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
HashtagMatch.prototype.getAnchorHref = function () {
var serviceName = this.serviceName, hashtag = this.hashtag;
switch (serviceName) {
case 'twitter':
return 'https://twitter.com/hashtag/' + hashtag;
case 'facebook':
return 'https://www.facebook.com/hashtag/' + hashtag;
case 'instagram':
return 'https://instagram.com/explore/tags/' + hashtag;
case 'tiktok':
return 'https://www.tiktok.com/tag/' + hashtag;
case 'youtube':
return 'https://youtube.com/hashtag/' + hashtag;
/* istanbul ignore next */
default:
// Should never happen because Autolinker's constructor should block any invalid values, but just in case
(0, utils_1.assertNever)(serviceName);
}
};
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
HashtagMatch.prototype.getAnchorText = function () {
return '#' + this.hashtag;
};
/**
* Returns the CSS class suffixes that should be used on a tag built with
* the match. See {@link Autolinker.match.Match#getCssClassSuffixes} for
* details.
*
* @return {String[]}
*/
HashtagMatch.prototype.getCssClassSuffixes = function () {
var cssClassSuffixes = _super.prototype.getCssClassSuffixes.call(this), serviceName = this.getServiceName();
if (serviceName) {
cssClassSuffixes.push(serviceName);
}
return cssClassSuffixes;
};
return HashtagMatch;
}(abstract_match_1.AbstractMatch));
exports.HashtagMatch = HashtagMatch;
//# sourceMappingURL=hashtag-match.js.map
File diff suppressed because one or more lines are too long
+7
View File
@@ -0,0 +1,7 @@
export * from './match';
export * from './email-match';
export * from './hashtag-match';
export * from './abstract-match';
export * from './mention-match';
export * from './phone-match';
export * from './url-match';
+11
View File
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("./match"), exports);
tslib_1.__exportStar(require("./email-match"), exports);
tslib_1.__exportStar(require("./hashtag-match"), exports);
tslib_1.__exportStar(require("./abstract-match"), exports);
tslib_1.__exportStar(require("./mention-match"), exports);
tslib_1.__exportStar(require("./phone-match"), exports);
tslib_1.__exportStar(require("./url-match"), exports);
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/match/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,wDAA8B;AAC9B,0DAAgC;AAChC,2DAAiC;AACjC,0DAAgC;AAChC,wDAA8B;AAC9B,sDAA4B","sourcesContent":["export * from './match';\nexport * from './email-match';\nexport * from './hashtag-match';\nexport * from './abstract-match';\nexport * from './mention-match';\nexport * from './phone-match';\nexport * from './url-match';\n"]}
+7
View File
@@ -0,0 +1,7 @@
import { EmailMatch } from './email-match';
import { HashtagMatch } from './hashtag-match';
import { MentionMatch } from './mention-match';
import { PhoneMatch } from './phone-match';
import { UrlMatch } from './url-match';
export type Match = EmailMatch | HashtagMatch | MentionMatch | PhoneMatch | UrlMatch;
export type MatchType = 'email' | 'hashtag' | 'mention' | 'phone' | 'url';
+3
View File
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=match.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"match.js","sourceRoot":"","sources":["../../../src/match/match.ts"],"names":[],"mappings":"","sourcesContent":["import { EmailMatch } from './email-match';\nimport { HashtagMatch } from './hashtag-match';\nimport { MentionMatch } from './mention-match';\nimport { PhoneMatch } from './phone-match';\nimport { UrlMatch } from './url-match';\n\nexport type Match = EmailMatch | HashtagMatch | MentionMatch | PhoneMatch | UrlMatch;\nexport type MatchType = 'email' | 'hashtag' | 'mention' | 'phone' | 'url';\n"]}
+84
View File
@@ -0,0 +1,84 @@
import { MentionService } from '../parser/mention-utils';
import { AbstractMatch, AbstractMatchConfig } from './abstract-match';
/**
* @class Autolinker.match.Mention
* @extends Autolinker.match.AbstractMatch
*
* Represents a Mention match found in an input string which should be Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
*/
export declare class MentionMatch extends AbstractMatch {
/**
* @public
* @property {'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.
*/
readonly type: "mention";
/**
* @cfg {String} serviceName
*
* The service to point mention matches to. See {@link Autolinker#mention}
* for available values.
*/
private readonly serviceName;
/**
* @cfg {String} mention (required)
*
* The Mention that was matched, without the '@' character.
*/
private readonly mention;
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
constructor(cfg: MentionMatchConfig);
/**
* Returns a string name for the type of match that this class represents.
* For the case of MentionMatch, returns 'mention'.
*
* @return {String}
*/
getType(): 'mention';
/**
* Returns the mention, without the '@' character.
*
* @return {String}
*/
getMention(): string;
/**
* Returns the configured {@link #serviceName} to point the mention to.
* Ex: 'instagram', 'twitter', 'soundcloud'.
*
* @return {String}
*/
getServiceName(): MentionService;
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
getAnchorHref(): string;
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
getAnchorText(): string;
/**
* Returns the CSS class suffixes that should be used on a tag built with
* the match. See {@link Autolinker.match.Match#getCssClassSuffixes} for
* details.
*
* @return {String[]}
*/
getCssClassSuffixes(): string[];
}
export interface MentionMatchConfig extends AbstractMatchConfig {
serviceName: MentionService;
mention: string;
}
+124
View File
@@ -0,0 +1,124 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MentionMatch = void 0;
var tslib_1 = require("tslib");
var utils_1 = require("../utils");
var abstract_match_1 = require("./abstract-match");
/**
* @class Autolinker.match.Mention
* @extends Autolinker.match.AbstractMatch
*
* Represents a Mention match found in an input string which should be Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
*/
var MentionMatch = /** @class */ (function (_super) {
tslib_1.__extends(MentionMatch, _super);
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
function MentionMatch(cfg) {
var _this = _super.call(this, cfg) || this;
/**
* @public
* @property {'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.
*/
_this.type = 'mention';
/**
* @cfg {String} serviceName
*
* The service to point mention matches to. See {@link Autolinker#mention}
* for available values.
*/
_this.serviceName = 'twitter'; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {String} mention (required)
*
* The Mention that was matched, without the '@' character.
*/
_this.mention = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
_this.mention = cfg.mention;
_this.serviceName = cfg.serviceName;
return _this;
}
/**
* Returns a string name for the type of match that this class represents.
* For the case of MentionMatch, returns 'mention'.
*
* @return {String}
*/
MentionMatch.prototype.getType = function () {
return 'mention';
};
/**
* Returns the mention, without the '@' character.
*
* @return {String}
*/
MentionMatch.prototype.getMention = function () {
return this.mention;
};
/**
* Returns the configured {@link #serviceName} to point the mention to.
* Ex: 'instagram', 'twitter', 'soundcloud'.
*
* @return {String}
*/
MentionMatch.prototype.getServiceName = function () {
return this.serviceName;
};
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
MentionMatch.prototype.getAnchorHref = function () {
switch (this.serviceName) {
case 'twitter':
return 'https://twitter.com/' + this.mention;
case 'instagram':
return 'https://instagram.com/' + this.mention;
case 'soundcloud':
return 'https://soundcloud.com/' + this.mention;
case 'tiktok':
return 'https://www.tiktok.com/@' + this.mention;
case 'youtube':
return 'https://youtube.com/@' + this.mention;
/* istanbul ignore next */
default:
// Should never happen because Autolinker's constructor should block any invalid values, but just in case.
(0, utils_1.assertNever)(this.serviceName);
}
};
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
MentionMatch.prototype.getAnchorText = function () {
return '@' + this.mention;
};
/**
* Returns the CSS class suffixes that should be used on a tag built with
* the match. See {@link Autolinker.match.Match#getCssClassSuffixes} for
* details.
*
* @return {String[]}
*/
MentionMatch.prototype.getCssClassSuffixes = function () {
var cssClassSuffixes = _super.prototype.getCssClassSuffixes.call(this), serviceName = this.getServiceName();
if (serviceName) {
cssClassSuffixes.push(serviceName);
}
return cssClassSuffixes;
};
return MentionMatch;
}(abstract_match_1.AbstractMatch));
exports.MentionMatch = MentionMatch;
//# sourceMappingURL=mention-match.js.map
File diff suppressed because one or more lines are too long
+88
View File
@@ -0,0 +1,88 @@
import { AbstractMatch, AbstractMatchConfig } from './abstract-match';
/**
* @class Autolinker.match.Phone
* @extends Autolinker.match.AbstractMatch
*
* Represents a Phone number match found in an input string which should be
* Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more
* details.
*/
export declare class PhoneMatch extends AbstractMatch {
/**
* @public
* @property {'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.
*/
readonly type: "phone";
/**
* @protected
* @property {String} number (required)
*
* The phone number that was matched, without any delimiter characters.
*
* Note: This is a string to allow for prefixed 0's.
*/
private readonly number;
/**
* @protected
* @property {Boolean} plusSign (required)
*
* `true` if the matched phone number started with a '+' sign. We'll include
* it in the `tel:` URL if so, as this is needed for international numbers.
*
* Ex: '+1 (123) 456 7879'
*/
private readonly plusSign;
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
constructor(cfg: PhoneMatchConfig);
/**
* Returns a string name for the type of match that this class represents.
* For the case of PhoneMatch, returns 'phone'.
*
* @return {String}
*/
getType(): 'phone';
/**
* 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.
*
* @return {String}
*/
getPhoneNumber(): string;
/**
* Alias of {@link #getPhoneNumber}, 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.
*
* @return {String}
*/
getNumber(): string;
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
getAnchorHref(): string;
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
getAnchorText(): string;
}
export interface PhoneMatchConfig extends AbstractMatchConfig {
number: string;
plusSign: boolean;
}
+107
View File
@@ -0,0 +1,107 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PhoneMatch = void 0;
var tslib_1 = require("tslib");
var abstract_match_1 = require("./abstract-match");
/**
* @class Autolinker.match.Phone
* @extends Autolinker.match.AbstractMatch
*
* Represents a Phone number match found in an input string which should be
* Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more
* details.
*/
var PhoneMatch = /** @class */ (function (_super) {
tslib_1.__extends(PhoneMatch, _super);
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
function PhoneMatch(cfg) {
var _this = _super.call(this, cfg) || this;
/**
* @public
* @property {'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.
*/
_this.type = 'phone';
/**
* @protected
* @property {String} number (required)
*
* The phone number that was matched, without any delimiter characters.
*
* Note: This is a string to allow for prefixed 0's.
*/
_this.number = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @protected
* @property {Boolean} plusSign (required)
*
* `true` if the matched phone number started with a '+' sign. We'll include
* it in the `tel:` URL if so, as this is needed for international numbers.
*
* Ex: '+1 (123) 456 7879'
*/
_this.plusSign = false; // default value just to get the above doc comment in the ES5 output and documentation generator
_this.number = cfg.number;
_this.plusSign = cfg.plusSign;
return _this;
}
/**
* Returns a string name for the type of match that this class represents.
* For the case of PhoneMatch, returns 'phone'.
*
* @return {String}
*/
PhoneMatch.prototype.getType = function () {
return 'phone';
};
/**
* 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.
*
* @return {String}
*/
PhoneMatch.prototype.getPhoneNumber = function () {
return this.number;
};
/**
* Alias of {@link #getPhoneNumber}, 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.
*
* @return {String}
*/
PhoneMatch.prototype.getNumber = function () {
return this.getPhoneNumber();
};
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
PhoneMatch.prototype.getAnchorHref = function () {
return 'tel:' + (this.plusSign ? '+' : '') + this.number;
};
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
PhoneMatch.prototype.getAnchorText = function () {
return this.matchedText;
};
return PhoneMatch;
}(abstract_match_1.AbstractMatch));
exports.PhoneMatch = PhoneMatch;
//# sourceMappingURL=phone-match.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"phone-match.js","sourceRoot":"","sources":["../../../src/match/phone-match.ts"],"names":[],"mappings":";;;;AAAA,mDAAsE;AAEtE;;;;;;;;;GASG;AACH;IAAgC,sCAAa;IAgCzC;;;;OAIG;IACH,oBAAY,GAAqB;QAC7B,YAAA,MAAK,YAAC,GAAG,CAAC,SAAC;QArCf;;;;;;;WAOG;QACa,UAAI,GAAG,OAAgB,CAAC;QAExC;;;;;;;WAOG;QACc,YAAM,GAAW,EAAE,CAAC,CAAC,gGAAgG;QAEtI;;;;;;;;WAQG;QACc,cAAQ,GAAY,KAAK,CAAC,CAAC,gGAAgG;QAUxI,KAAI,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;QACzB,KAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;;IACjC,CAAC;IAED;;;;;OAKG;IACH,4BAAO,GAAP;QACI,OAAO,OAAO,CAAC;IACnB,CAAC;IAED;;;;;;;OAOG;IACH,mCAAc,GAAd;QACI,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED;;;;;;;OAOG;IACH,8BAAS,GAAT;QACI,OAAO,IAAI,CAAC,cAAc,EAAE,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kCAAa,GAAb;QACI,OAAO,MAAM,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IAC7D,CAAC;IAED;;;;OAIG;IACH,kCAAa,GAAb;QACI,OAAO,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IACL,iBAAC;AAAD,CAAC,AA/FD,CAAgC,8BAAa,GA+F5C;AA/FY,gCAAU","sourcesContent":["import { AbstractMatch, AbstractMatchConfig } from './abstract-match';\n\n/**\n * @class Autolinker.match.Phone\n * @extends Autolinker.match.AbstractMatch\n *\n * Represents a Phone number match found in an input string which should be\n * Autolinked.\n *\n * See this class's superclass ({@link Autolinker.match.Match}) for more\n * details.\n */\nexport class PhoneMatch extends AbstractMatch {\n /**\n * @public\n * @property {'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 readonly type = 'phone' as const;\n\n /**\n * @protected\n * @property {String} number (required)\n *\n * The phone number that was matched, without any delimiter characters.\n *\n * Note: This is a string to allow for prefixed 0's.\n */\n private readonly number: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @protected\n * @property {Boolean} plusSign (required)\n *\n * `true` if the matched phone number started with a '+' sign. We'll include\n * it in the `tel:` URL if so, as this is needed for international numbers.\n *\n * Ex: '+1 (123) 456 7879'\n */\n private readonly plusSign: boolean = false; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @method constructor\n * @param {Object} cfg The configuration properties for the Match\n * instance, specified in an Object (map).\n */\n constructor(cfg: PhoneMatchConfig) {\n super(cfg);\n\n this.number = cfg.number;\n this.plusSign = cfg.plusSign;\n }\n\n /**\n * Returns a string name for the type of match that this class represents.\n * For the case of PhoneMatch, returns 'phone'.\n *\n * @return {String}\n */\n getType(): 'phone' {\n return 'phone';\n }\n\n /**\n * Returns the phone number that was matched as a string, without any\n * delimiter characters.\n *\n * Note: This is a string to allow for prefixed 0's.\n *\n * @return {String}\n */\n getPhoneNumber(): string {\n return this.number;\n }\n\n /**\n * Alias of {@link #getPhoneNumber}, returns the phone number that was\n * matched as a string, without any delimiter characters.\n *\n * Note: This is a string to allow for prefixed 0's.\n *\n * @return {String}\n */\n getNumber(): string {\n return this.getPhoneNumber();\n }\n\n /**\n * Returns the anchor href that should be generated for the match.\n *\n * @return {String}\n */\n getAnchorHref(): string {\n return 'tel:' + (this.plusSign ? '+' : '') + this.number;\n }\n\n /**\n * Returns the anchor text that should be generated for the match.\n *\n * @return {String}\n */\n getAnchorText(): string {\n return this.matchedText;\n }\n}\n\nexport interface PhoneMatchConfig extends AbstractMatchConfig {\n number: string;\n plusSign: boolean;\n}\n"]}
+121
View File
@@ -0,0 +1,121 @@
import { AbstractMatch, AbstractMatchConfig } from './abstract-match';
import type { StripPrefixConfigObj } from '../autolinker';
/**
* @class Autolinker.match.Url
* @extends Autolinker.match.AbstractMatch
*
* Represents a Url match found in an input string which should be Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
*/
export declare class UrlMatch extends AbstractMatch {
/**
* @public
* @property {'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.
*/
readonly type: "url";
/**
* @cfg {String} url (required)
*
* The url that was matched.
*/
private url;
/**
* @cfg {"scheme"/"www"/"tld"} urlMatchType (required)
*
* 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').
*/
private readonly urlMatchType;
/**
* @cfg {Boolean} protocolRelativeMatch (required)
*
* `true` if the URL is a protocol-relative match. A protocol-relative match
* is a URL that starts with '//', and will be either http:// or https://
* based on the protocol that the site is loaded under.
*/
private readonly protocolRelativeMatch;
/**
* @cfg {Object} stripPrefix (required)
*
* The Object form of {@link Autolinker#cfg-stripPrefix}.
*/
private readonly stripPrefix;
/**
* @cfg {Boolean} stripTrailingSlash (required)
* @inheritdoc Autolinker#cfg-stripTrailingSlash
*/
private readonly stripTrailingSlash;
/**
* @cfg {Boolean} decodePercentEncoding (required)
* @inheritdoc Autolinker#cfg-decodePercentEncoding
*/
private readonly decodePercentEncoding;
/**
* @private
* @property {Boolean} protocolPrepended
*
* Will be set to `true` if the 'http://' protocol has been prepended to the {@link #url} (because the
* {@link #url} did not have a protocol)
*/
private protocolPrepended;
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
constructor(cfg: UrlMatchConfig);
/**
* Returns a string name for the type of match that this class represents.
* For the case of UrlMatch, returns 'url'.
*
* @return {String}
*/
getType(): 'url';
/**
* 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').
*
* @return {"scheme"/"www"/"tld"}
*/
getUrlMatchType(): UrlMatchType;
/**
* Returns the url that was matched, assuming the protocol to be 'http://' if the original
* match was missing a protocol.
*
* @return {String}
*/
getUrl(): string;
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
getAnchorHref(): string;
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
getAnchorText(): string;
}
export interface UrlMatchConfig extends AbstractMatchConfig {
url: string;
urlMatchType: UrlMatchType;
protocolRelativeMatch: boolean;
stripPrefix: Required<StripPrefixConfigObj>;
stripTrailingSlash: boolean;
decodePercentEncoding: boolean;
}
export type UrlMatchType = 'scheme' | 'tld' | 'ipV4';
+280
View File
@@ -0,0 +1,280 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UrlMatch = void 0;
var tslib_1 = require("tslib");
var abstract_match_1 = require("./abstract-match");
var uri_utils_1 = require("../parser/uri-utils");
/**
* A regular expression used to remove the 'www.' from URLs.
*/
var wwwPrefixRegex = /^(https?:\/\/)?(?:www\.)?/i;
/**
* The regular expression used to remove the protocol-relative '//' from a URL
* string, for purposes of formatting the anchor text. A protocol-relative URL
* is, for example, "//yahoo.com"
*/
var protocolRelativeRegex = /^\/\//;
/**
* @class Autolinker.match.Url
* @extends Autolinker.match.AbstractMatch
*
* Represents a Url match found in an input string which should be Autolinked.
*
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
*/
var UrlMatch = /** @class */ (function (_super) {
tslib_1.__extends(UrlMatch, _super);
/**
* @method constructor
* @param {Object} cfg The configuration properties for the Match
* instance, specified in an Object (map).
*/
function UrlMatch(cfg) {
var _this = _super.call(this, cfg) || this;
/**
* @public
* @property {'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.
*/
_this.type = 'url';
/**
* @cfg {String} url (required)
*
* The url that was matched.
*/
_this.url = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {"scheme"/"www"/"tld"} urlMatchType (required)
*
* 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').
*/
_this.urlMatchType = 'scheme'; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {Boolean} protocolRelativeMatch (required)
*
* `true` if the URL is a protocol-relative match. A protocol-relative match
* is a URL that starts with '//', and will be either http:// or https://
* based on the protocol that the site is loaded under.
*/
_this.protocolRelativeMatch = false; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {Object} stripPrefix (required)
*
* The Object form of {@link Autolinker#cfg-stripPrefix}.
*/
_this.stripPrefix = {
scheme: true,
www: true,
}; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {Boolean} stripTrailingSlash (required)
* @inheritdoc Autolinker#cfg-stripTrailingSlash
*/
_this.stripTrailingSlash = true; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @cfg {Boolean} decodePercentEncoding (required)
* @inheritdoc Autolinker#cfg-decodePercentEncoding
*/
_this.decodePercentEncoding = true; // default value just to get the above doc comment in the ES5 output and documentation generator
/**
* @private
* @property {Boolean} protocolPrepended
*
* Will be set to `true` if the 'http://' protocol has been prepended to the {@link #url} (because the
* {@link #url} did not have a protocol)
*/
_this.protocolPrepended = false;
_this.urlMatchType = cfg.urlMatchType;
_this.url = cfg.url;
_this.protocolRelativeMatch = cfg.protocolRelativeMatch;
_this.stripPrefix = cfg.stripPrefix;
_this.stripTrailingSlash = cfg.stripTrailingSlash;
_this.decodePercentEncoding = cfg.decodePercentEncoding;
return _this;
}
/**
* Returns a string name for the type of match that this class represents.
* For the case of UrlMatch, returns 'url'.
*
* @return {String}
*/
UrlMatch.prototype.getType = function () {
return 'url';
};
/**
* 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').
*
* @return {"scheme"/"www"/"tld"}
*/
UrlMatch.prototype.getUrlMatchType = function () {
return this.urlMatchType;
};
/**
* Returns the url that was matched, assuming the protocol to be 'http://' if the original
* match was missing a protocol.
*
* @return {String}
*/
UrlMatch.prototype.getUrl = function () {
var url = this.url;
// if the url string doesn't begin with a scheme, assume 'http://'
if (!this.protocolRelativeMatch &&
this.urlMatchType !== 'scheme' &&
!this.protocolPrepended) {
url = this.url = 'http://' + url;
this.protocolPrepended = true;
}
return url;
};
/**
* Returns the anchor href that should be generated for the match.
*
* @return {String}
*/
UrlMatch.prototype.getAnchorHref = function () {
var url = this.getUrl();
return url.replace(/&amp;/g, '&'); // any &amp;'s in the URL should be converted back to '&' if they were displayed as &amp; in the source html
};
/**
* Returns the anchor text that should be generated for the match.
*
* @return {String}
*/
UrlMatch.prototype.getAnchorText = function () {
var anchorText = this.getMatchedText();
if (this.protocolRelativeMatch) {
// Strip off any protocol-relative '//' from the anchor text
anchorText = stripProtocolRelativePrefix(anchorText);
}
if (this.stripPrefix.scheme) {
anchorText = stripSchemePrefix(anchorText);
}
if (this.stripPrefix.www) {
anchorText = stripWwwPrefix(anchorText);
}
if (this.stripTrailingSlash) {
anchorText = removeTrailingSlash(anchorText); // remove trailing slash, if there is one
}
if (this.decodePercentEncoding) {
anchorText = removePercentEncoding(anchorText);
}
return anchorText;
};
return UrlMatch;
}(abstract_match_1.AbstractMatch));
exports.UrlMatch = UrlMatch;
// Utility Functionality
/**
* Strips the scheme prefix (such as "http://" or "https://") from the given
* `url`.
*
* @private
* @param {String} url The text of the anchor that is being generated, for
* which to strip off the url scheme.
* @return {String} The `url`, with the scheme stripped.
*/
function stripSchemePrefix(url) {
return url.replace(uri_utils_1.httpSchemePrefixRe, '');
}
/**
* Strips the 'www' prefix from the given `url`.
*
* @private
* @param {String} url The text of the anchor that is being generated, for
* which to strip off the 'www' if it exists.
* @return {String} The `url`, with the 'www' stripped.
*/
function stripWwwPrefix(url) {
// If the URL doesn't actually include 'www.' in it, skip running the
// .replace() regexp on it, which is fairly slow even just to check the
// string for the 'www.'s existence. Most URLs these days do not have 'www.'
// in it, so most of the time we skip running the .replace(). One other
// option in the future is to run a state machine on the `url` string
if (!url.includes('www.')) {
return url;
}
else {
return url.replace(wwwPrefixRegex, '$1'); // leave any scheme ($1), it one exists
}
}
/**
* Strips any protocol-relative '//' from the anchor text.
*
* @private
* @param {String} text The text of the anchor that is being generated, for which to strip off the
* protocol-relative prefix (such as stripping off "//")
* @return {String} The `anchorText`, with the protocol-relative prefix stripped.
*/
function stripProtocolRelativePrefix(text) {
return text.replace(protocolRelativeRegex, '');
}
/**
* Removes any trailing slash from the given `anchorText`, in preparation for the text to be displayed.
*
* @private
* @param {String} anchorText The text of the anchor that is being generated, for which to remove any trailing
* slash ('/') that may exist.
* @return {String} The `anchorText`, with the trailing slash removed.
*/
function removeTrailingSlash(anchorText) {
if (anchorText.charAt(anchorText.length - 1) === '/') {
anchorText = anchorText.slice(0, -1);
}
return anchorText;
}
/**
* Decodes percent-encoded characters from the given `anchorText`, in
* preparation for the text to be displayed.
*
* @private
* @param {String} anchorText The text of the anchor that is being
* generated, for which to decode any percent-encoded characters.
* @return {String} The `anchorText`, with the percent-encoded characters
* decoded.
*/
function removePercentEncoding(anchorText) {
// First, convert a few of the known % encodings to the corresponding
// HTML entities that could accidentally be interpretted as special
// HTML characters
// NOTE: This used to be written as 5 separate .replace() calls, but that
// was 25% slower than the current form below according to jsperf
var preProcessedEntityAnchorText = anchorText.replace(/%(?:22|26|27|3C|3E)/gi, function (match) {
if (match === '%22')
return '&quot;'; // %22: '"' char
if (match === '%26')
return '&amp;'; // %26: '&' char
if (match === '%27')
return '&#39;'; // %27: "'" char
if (match === '%3C' || match === '%3c')
return '&lt;'; // %3C: '<' char
/*if (match === '%3E' || match === '%3e')*/ return '&gt;'; // %3E: '>' char
});
// Now attempt to URL-decode the rest of the anchor text. However,
// decodeURIComponent() is a slow function. Only call it if we have
// remaining %-encoded entities. Adding this check added ~300 ops/sec to
// benchmark
if (preProcessedEntityAnchorText.includes('%')) {
try {
return decodeURIComponent(preProcessedEntityAnchorText);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (error) {
// Invalid % escape sequence in the anchor text, we'll simply return
// the preProcessedEntityAnchorText below
}
}
return preProcessedEntityAnchorText;
}
//# sourceMappingURL=url-match.js.map
File diff suppressed because one or more lines are too long