UNPKG

4.69 kBJavaScriptView Raw
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("@[_" + regex_lib_1.alphaNumericAndMarksCharsStr + "]{1,50}(?![_" + regex_lib_1.alphaNumericAndMarksCharsStr + "])", 'g'); // lookahead used to make sure we don't match something above 50 characters
14var instagramRegex = new RegExp("@[_." + regex_lib_1.alphaNumericAndMarksCharsStr + "]{1,30}(?![_" + regex_lib_1.alphaNumericAndMarksCharsStr + "])", 'g'); // lookahead used to make sure we don't match something above 30 characters
15var soundcloudRegex = new RegExp("@[-_." + regex_lib_1.alphaNumericAndMarksCharsStr + "]{1,50}(?![-_" + regex_lib_1.alphaNumericAndMarksCharsStr + "])", 'g'); // lookahead used to make sure we don't match something above 50 characters
16var nonWordCharRegex = new RegExp('[^' + regex_lib_1.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 */
23var MentionMatcher = /** @class */ (function (_super) {
24 tslib_1.__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', or 'soundcloud'
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 };
53 /**
54 * The regular expression to use to check the character before a username match to
55 * make sure we didn't accidentally match an email address.
56 *
57 * For example, the string "asdf@asdf.com" should not match "@asdf" as a username.
58 *
59 * @private
60 * @property {RegExp} nonWordCharRegex
61 */
62 _this.nonWordCharRegex = nonWordCharRegex;
63 _this.serviceName = cfg.serviceName;
64 return _this;
65 }
66 /**
67 * @inheritdoc
68 */
69 MentionMatcher.prototype.parseMatches = function (text) {
70 var serviceName = this.serviceName, matcherRegex = this.matcherRegexes[this.serviceName], nonWordCharRegex = this.nonWordCharRegex, tagBuilder = this.tagBuilder, matches = [], match;
71 if (!matcherRegex) {
72 return matches;
73 }
74 while ((match = matcherRegex.exec(text)) !== null) {
75 var offset = match.index, prevChar = text.charAt(offset - 1);
76 // If we found the match at the beginning of the string, or we found the match
77 // and there is a whitespace char in front of it (meaning it is not an email
78 // address), then it is a username match.
79 if (offset === 0 || nonWordCharRegex.test(prevChar)) {
80 var matchedText = match[0].replace(/\.+$/g, ''), // strip off trailing .
81 mention = matchedText.slice(1); // strip off the '@' character at the beginning
82 matches.push(new mention_match_1.MentionMatch({
83 tagBuilder: tagBuilder,
84 matchedText: matchedText,
85 offset: offset,
86 serviceName: serviceName,
87 mention: mention
88 }));
89 }
90 }
91 return matches;
92 };
93 return MentionMatcher;
94}(matcher_1.Matcher));
95exports.MentionMatcher = MentionMatcher;
96
97//# sourceMappingURL=mention-matcher.js.map