Debian Codegraph @

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

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