Debian Codegraph @

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

raw
1 import { __extends } from "tslib";
2 import { Matcher } from './matcher';
3 import { alphaNumericAndMarksCharRe, alphaNumericAndMarksCharsStr } from '../regex-lib';
4 import { HashtagMatch } from '../match/hashtag-match';
5 import { throwUnhandledCaseError } from '../utils';
6 // For debugging: search for other "For debugging" lines
7 // import CliTable from 'cli-table';
8 var hashtagTextCharRe = new RegExp("[_".concat(alphaNumericAndMarksCharsStr, "]"));
9 /**
10 * @class Autolinker.matcher.Hashtag
11 * @extends Autolinker.matcher.Matcher
12 *
13 * Matcher to find HashtagMatch matches in an input string.
14 */
15 var HashtagMatcher = /** @class */ (function (_super) {
16 __extends(HashtagMatcher, _super);
17 /**
18 * @method constructor
19 * @param {Object} cfg The configuration properties for the Match instance,
20 * specified in an Object (map).
21 */
22 function HashtagMatcher(cfg) {
23 var _this = _super.call(this, cfg) || this;
24 /**
25 * @cfg {String} service
26 *
27 * A string for the service name to have hashtags (ex: "#myHashtag")
28 * auto-linked to. The currently-supported values are:
29 *
30 * - 'twitter'
31 * - 'facebook'
32 * - 'instagram'
33 * - 'tiktok'
34 */
35 _this.serviceName = 'twitter'; // default value just to get the above doc comment in the ES5 output and documentation generator
36 _this.serviceName = cfg.serviceName;
37 return _this;
38 }
39 /**
40 * @inheritdoc
41 */
42 HashtagMatcher.prototype.parseMatches = function (text) {
43 var tagBuilder = this.tagBuilder;
44 var serviceName = this.serviceName;
45 var matches = [];
46 var len = text.length;
47 var charIdx = 0, hashCharIdx = -1, state = 0 /* None */;
48 // For debugging: search for other "For debugging" lines
49 // const table = new CliTable( {
50 // head: [ 'charIdx', 'char', 'state', 'charIdx', 'currentEmailAddress.idx', 'hasDomainDot' ]
51 // } );
52 while (charIdx < len) {
53 var char = text.charAt(charIdx);
54 // For debugging: search for other "For debugging" lines
55 // table.push(
56 // [ charIdx, char, State[ state ], charIdx, currentEmailAddress.idx, currentEmailAddress.hasDomainDot ]
57 // );
58 switch (state) {
59 case 0 /* None */:
60 stateNone(char);
61 break;
62 case 1 /* NonHashtagWordChar */:
63 stateNonHashtagWordChar(char);
64 break;
65 case 2 /* HashtagHashChar */:
66 stateHashtagHashChar(char);
67 break;
68 case 3 /* HashtagTextChar */:
69 stateHashtagTextChar(char);
70 break;
71 default:
72 throwUnhandledCaseError(state);
73 }
74 // For debugging: search for other "For debugging" lines
75 // table.push(
76 // [ charIdx, char, State[ state ], charIdx, currentEmailAddress.idx, currentEmailAddress.hasDomainDot ]
77 // );
78 charIdx++;
79 }
80 // Capture any valid match at the end of the string
81 captureMatchIfValid();
82 // For debugging: search for other "For debugging" lines
83 //console.log( '\n' + table.toString() );
84 return matches;
85 // Handles the state when we're not in a hashtag or any word
86 function stateNone(char) {
87 if (char === '#') {
88 state = 2 /* HashtagHashChar */;
89 hashCharIdx = charIdx;
90 }
91 else if (alphaNumericAndMarksCharRe.test(char)) {
92 state = 1 /* NonHashtagWordChar */;
93 }
94 else {
95 // not a hashtag character ('#') or word char, stay in State.None
96 }
97 }
98 // Handles the state when we've encountered a word character but are not
99 // in a hashtag. This is used to distinguish between a standalone
100 // hashtag such as '#Stuff' vs a hash char that is part of a word like
101 // 'asdf#stuff' (the latter of which would not be a match)
102 function stateNonHashtagWordChar(char) {
103 if (alphaNumericAndMarksCharRe.test(char)) {
104 // continue in NonHashtagWordChar state
105 }
106 else {
107 state = 0 /* None */;
108 }
109 }
110 // Handles the state when we've just encountered a '#' character
111 function stateHashtagHashChar(char) {
112 if (hashtagTextCharRe.test(char)) {
113 // '#' char with valid hash text char following
114 state = 3 /* HashtagTextChar */;
115 }
116 else if (alphaNumericAndMarksCharRe.test(char)) {
117 state = 1 /* NonHashtagWordChar */;
118 }
119 else {
120 state = 0 /* None */;
121 }
122 }
123 // Handles the state when we're currently in the hash tag's text chars
124 function stateHashtagTextChar(char) {
125 if (hashtagTextCharRe.test(char)) {
126 // Continue reading characters in the HashtagText state
127 }
128 else {
129 captureMatchIfValid();
130 hashCharIdx = -1;
131 if (alphaNumericAndMarksCharRe.test(char)) {
132 state = 1 /* NonHashtagWordChar */;
133 }
134 else {
135 state = 0 /* None */;
136 }
137 }
138 }
139 /*
140 * Captures the current hashtag as a HashtagMatch if it's valid.
141 */
142 function captureMatchIfValid() {
143 if (hashCharIdx > -1 && charIdx - hashCharIdx <= 140) {
144 // Max length of 140 for a hashtag ('#' char + 139 word chars)
145 var matchedText = text.slice(hashCharIdx, charIdx);
146 var match = new HashtagMatch({
147 tagBuilder: tagBuilder,
148 matchedText: matchedText,
149 offset: hashCharIdx,
150 serviceName: serviceName,
151 hashtag: matchedText.slice(1),
152 });
153 matches.push(match);
154 }
155 }
156 };
157 return HashtagMatcher;
158 }(Matcher));
159 export { HashtagMatcher };
160 export var hashtagServices = ['twitter', 'facebook', 'instagram', 'tiktok'];
161 //# sourceMappingURL=hashtag-matcher.js.map