mention-match.js 4.0 KB

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