Debian Codegraph @

node/autolinker/dist/commonjs/matcher/mention-matcher.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.MentionMatcher = void 0;
4 var tslib_1 = require("tslib");
5 var matcher_1 = require("./matcher");
6 var regex_lib_1 = require("../regex-lib");
7 var 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
13 var 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
14 var 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
15 var 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
18 var 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
19 var 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 */
26 var 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 "[email protected]" 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));
99 exports.MentionMatcher = MentionMatcher;
100 //# sourceMappingURL=mention-matcher.js.map