Debian Codegraph @

src/matcher/Hashtag.js

raw symbols SCIP
1 /*global Autolinker */
2 /**
3 * @class Autolinker.matcher.Hashtag
4 * @extends Autolinker.matcher.Matcher
5 *
6 * Matcher to find Hashtag matches in an input string.
7 */
8 Autolinker.matcher.Hashtag = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
9
10 /**
11 * @cfg {String} serviceName
12 *
13 * The service to point hashtag matches to. See {@link Autolinker#hashtag}
14 * for available values.
15 */
16
17
18 /**
19 * The regular expression to match Hashtags. Example match:
20 *
21 * #asdf
22 *
23 * @private
24 * @property {RegExp} matcherRegex
25 */
26 matcherRegex : new RegExp( '#[_' + Autolinker.RegexLib.alphaNumericCharsStr + ']{1,139}', 'g' ),
27
28 /**
29 * The regular expression to use to check the character before a username match to
30 * make sure we didn't accidentally match an email address.
31 *
32 * For example, the string "[email protected]" should not match "@asdf" as a username.
33 *
34 * @private
35 * @property {RegExp} nonWordCharRegex
36 */
37 nonWordCharRegex : new RegExp( '[^' + Autolinker.RegexLib.alphaNumericCharsStr + ']' ),
38
39
40 /**
41 * @constructor
42 * @param {Object} cfg The configuration properties for the Match instance,
43 * specified in an Object (map).
44 */
45 constructor : function( cfg ) {
46 Autolinker.matcher.Matcher.prototype.constructor.call( this, cfg );
47
48 this.serviceName = cfg.serviceName;
49 },
50
51
52 /**
53 * @inheritdoc
54 */
55 parseMatches : function( text ) {
56 var matcherRegex = this.matcherRegex,
57 nonWordCharRegex = this.nonWordCharRegex,
58 serviceName = this.serviceName,
59 tagBuilder = this.tagBuilder,
60 matches = [],
61 match;
62
63 while( ( match = matcherRegex.exec( text ) ) !== null ) {
64 var offset = match.index,
65 prevChar = text.charAt( offset - 1 );
66
67 // If we found the match at the beginning of the string, or we found the match
68 // and there is a whitespace char in front of it (meaning it is not a '#' char
69 // in the middle of a word), then it is a hashtag match.
70 if( offset === 0 || nonWordCharRegex.test( prevChar ) ) {
71 var matchedText = match[ 0 ],
72 hashtag = match[ 0 ].slice( 1 ); // strip off the '#' character at the beginning
73
74 matches.push( new Autolinker.match.Hashtag( {
75 tagBuilder : tagBuilder,
76 matchedText : matchedText,
77 offset : offset,
78 serviceName : serviceName,
79 hashtag : hashtag
80 } ) );
81 }
82 }
83
84 return matches;
85 }
86
87 } );