source: node_modules/autolinker/dist/commonjs/matcher/mention-matcher.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.1 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.MentionMatcher = void 0;
4var tslib_1 = require("tslib");
5var matcher_1 = require("./matcher");
6var regex_lib_1 = require("../regex-lib");
7var mention_match_1 = require("../match/mention-match");
8// RegExp objects which are shared by all instances of MentionMatcher. These are
9// here to avoid re-instantiating the RegExp objects if `Autolinker.link()` is
10// called multiple times, thus instantiating MentionMatcher and its RegExp
11// objects each time (which is very expensive - see https://github.com/gregjacobs/Autolinker.js/issues/314).
12// See descriptions of the properties where they are used for details about them
13var twitterRegex = new RegExp("@[_".concat(regex_lib_1.alphaNumericAndMarksCharsStr, "]{1,50}(?![_").concat(regex_lib_1.alphaNumericAndMarksCharsStr, "])"), 'g'); // lookahead used to make sure we don't match something above 50 characters
14var instagramRegex = new RegExp("@[_.".concat(regex_lib_1.alphaNumericAndMarksCharsStr, "]{1,30}(?![_").concat(regex_lib_1.alphaNumericAndMarksCharsStr, "])"), 'g'); // lookahead used to make sure we don't match something above 30 characters
15var soundcloudRegex = new RegExp("@[-_.".concat(regex_lib_1.alphaNumericAndMarksCharsStr, "]{1,50}(?![-_").concat(regex_lib_1.alphaNumericAndMarksCharsStr, "])"), 'g'); // lookahead used to make sure we don't match something above 50 characters
16// TikTok usernames are 1-24 characters containing letters, numbers, underscores
17// and periods, but cannot end in a period: https://support.tiktok.com/en/getting-started/setting-up-your-profile/changing-your-username
18var tiktokRegex = new RegExp("@[_.".concat(regex_lib_1.alphaNumericAndMarksCharsStr, "]{1,23}[_").concat(regex_lib_1.alphaNumericAndMarksCharsStr, "](?![_").concat(regex_lib_1.alphaNumericAndMarksCharsStr, "])"), 'g'); // lookahead used to make sure we don't match something above 24 characters
19var nonWordCharRegex = new RegExp('[^' + regex_lib_1.alphaNumericAndMarksCharsStr + ']');
20/**
21 * @class Autolinker.matcher.Mention
22 * @extends Autolinker.matcher.Matcher
23 *
24 * Matcher to find/replace username matches in an input string.
25 */
26var MentionMatcher = /** @class */ (function (_super) {
27 (0, tslib_1.__extends)(MentionMatcher, _super);
28 /**
29 * @method constructor
30 * @param {Object} cfg The configuration properties for the Match instance,
31 * specified in an Object (map).
32 */
33 function MentionMatcher(cfg) {
34 var _this = _super.call(this, cfg) || this;
35 /**
36 * @cfg {'twitter'/'instagram'/'soundcloud'} protected
37 *
38 * The name of service to link @mentions to.
39 *
40 * Valid values are: 'twitter', 'instagram', 'soundcloud', or 'tiktok'
41 */
42 _this.serviceName = 'twitter'; // default value just to get the above doc comment in the ES5 output and documentation generator
43 /**
44 * Hash of regular expression to match username handles. Example match:
45 *
46 * @asdf
47 *
48 * @private
49 * @property {Object} matcherRegexes
50 */
51 _this.matcherRegexes = {
52 twitter: twitterRegex,
53 instagram: instagramRegex,
54 soundcloud: soundcloudRegex,
55 tiktok: tiktokRegex,
56 };
57 /**
58 * The regular expression to use to check the character before a username match to
59 * make sure we didn't accidentally match an email address.
60 *
61 * For example, the string "asdf@asdf.com" should not match "@asdf" as a username.
62 *
63 * @private
64 * @property {RegExp} nonWordCharRegex
65 */
66 _this.nonWordCharRegex = nonWordCharRegex;
67 _this.serviceName = cfg.serviceName;
68 return _this;
69 }
70 /**
71 * @inheritdoc
72 */
73 MentionMatcher.prototype.parseMatches = function (text) {
74 var serviceName = this.serviceName, matcherRegex = this.matcherRegexes[this.serviceName], nonWordCharRegex = this.nonWordCharRegex, tagBuilder = this.tagBuilder, matches = [], match;
75 if (!matcherRegex) {
76 return matches;
77 }
78 while ((match = matcherRegex.exec(text)) !== null) {
79 var offset = match.index, prevChar = text.charAt(offset - 1);
80 // If we found the match at the beginning of the string, or we found the match
81 // and there is a whitespace char in front of it (meaning it is not an email
82 // address), then it is a username match.
83 if (offset === 0 || nonWordCharRegex.test(prevChar)) {
84 var matchedText = match[0].replace(/\.+$/g, ''), // strip off trailing .
85 mention = matchedText.slice(1); // strip off the '@' character at the beginning
86 matches.push(new mention_match_1.MentionMatch({
87 tagBuilder: tagBuilder,
88 matchedText: matchedText,
89 offset: offset,
90 serviceName: serviceName,
91 mention: mention,
92 }));
93 }
94 }
95 return matches;
96 };
97 return MentionMatcher;
98}(matcher_1.Matcher));
99exports.MentionMatcher = MentionMatcher;
100//# sourceMappingURL=mention-matcher.js.map
Note: See TracBrowser for help on using the repository browser.