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