mention-match.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MentionMatch = void 0;
  4. var tslib_1 = require("tslib");
  5. var abstract_match_1 = require("./abstract-match");
  6. /**
  7. * @class Autolinker.match.Mention
  8. * @extends Autolinker.match.AbstractMatch
  9. *
  10. * Represents a Mention match found in an input string which should be Autolinked.
  11. *
  12. * See this class's superclass ({@link Autolinker.match.Match}) for more details.
  13. */
  14. var MentionMatch = /** @class */ (function (_super) {
  15. (0, tslib_1.__extends)(MentionMatch, _super);
  16. /**
  17. * @method constructor
  18. * @param {Object} cfg The configuration properties for the Match
  19. * instance, specified in an Object (map).
  20. */
  21. function MentionMatch(cfg) {
  22. var _this = _super.call(this, cfg) || this;
  23. /**
  24. * @public
  25. * @property {'mention'} type
  26. *
  27. * A string name for the type of match that this class represents. Can be
  28. * used in a TypeScript discriminating union to type-narrow from the
  29. * `Match` type.
  30. */
  31. _this.type = 'mention';
  32. /**
  33. * @cfg {String} serviceName
  34. *
  35. * The service to point mention matches to. See {@link Autolinker#mention}
  36. * for available values.
  37. */
  38. _this.serviceName = 'twitter'; // default value just to get the above doc comment in the ES5 output and documentation generator
  39. /**
  40. * @cfg {String} mention (required)
  41. *
  42. * The Mention that was matched, without the '@' character.
  43. */
  44. _this.mention = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
  45. _this.mention = cfg.mention;
  46. _this.serviceName = cfg.serviceName;
  47. return _this;
  48. }
  49. /**
  50. * Returns a string name for the type of match that this class represents.
  51. * For the case of MentionMatch, returns 'mention'.
  52. *
  53. * @return {String}
  54. */
  55. MentionMatch.prototype.getType = function () {
  56. return 'mention';
  57. };
  58. /**
  59. * Returns the mention, without the '@' character.
  60. *
  61. * @return {String}
  62. */
  63. MentionMatch.prototype.getMention = function () {
  64. return this.mention;
  65. };
  66. /**
  67. * Returns the configured {@link #serviceName} to point the mention to.
  68. * Ex: 'instagram', 'twitter', 'soundcloud'.
  69. *
  70. * @return {String}
  71. */
  72. MentionMatch.prototype.getServiceName = function () {
  73. return this.serviceName;
  74. };
  75. /**
  76. * Returns the anchor href that should be generated for the match.
  77. *
  78. * @return {String}
  79. */
  80. MentionMatch.prototype.getAnchorHref = function () {
  81. switch (this.serviceName) {
  82. case 'twitter':
  83. return 'https://twitter.com/' + this.mention;
  84. case 'instagram':
  85. return 'https://instagram.com/' + this.mention;
  86. case 'soundcloud':
  87. return 'https://soundcloud.com/' + this.mention;
  88. case 'tiktok':
  89. return 'https://www.tiktok.com/@' + this.mention;
  90. default:
  91. // Shouldn't happen because Autolinker's constructor should block any invalid values, but just in case.
  92. throw new Error('Unknown service name to point mention to: ' + this.serviceName);
  93. }
  94. };
  95. /**
  96. * Returns the anchor text that should be generated for the match.
  97. *
  98. * @return {String}
  99. */
  100. MentionMatch.prototype.getAnchorText = function () {
  101. return '@' + this.mention;
  102. };
  103. /**
  104. * Returns the CSS class suffixes that should be used on a tag built with
  105. * the match. See {@link Autolinker.match.Match#getCssClassSuffixes} for
  106. * details.
  107. *
  108. * @return {String[]}
  109. */
  110. MentionMatch.prototype.getCssClassSuffixes = function () {
  111. var cssClassSuffixes = _super.prototype.getCssClassSuffixes.call(this), serviceName = this.getServiceName();
  112. if (serviceName) {
  113. cssClassSuffixes.push(serviceName);
  114. }
  115. return cssClassSuffixes;
  116. };
  117. return MentionMatch;
  118. }(abstract_match_1.AbstractMatch));
  119. exports.MentionMatch = MentionMatch;
  120. //# sourceMappingURL=mention-match.js.map