通用评论

helper.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isFunction = isFunction;
  6. exports.isUrl = isUrl;
  7. exports.arrayToObject = arrayToObject;
  8. exports.htmlEncode = htmlEncode;
  9. exports.renderContent = renderContent;
  10. exports.addImageProcess = addImageProcess;
  11. var _constant = require("./constant");
  12. var _emoji = require("./emoji");
  13. var _emoji2 = _interopRequireDefault(_emoji);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. var emojiObejct = arrayToObject(_emoji2.default, "title");
  16. function isFunction(functionToCheck) {
  17. return functionToCheck && {}.toString.call(functionToCheck) === "[object Function]";
  18. }
  19. function isUrl(userInput) {
  20. // 需完整匹配
  21. var regexp = /^((http(s)?:)?\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
  22. var res = userInput.match(regexp);
  23. if (res === null) return false;else return true;
  24. }
  25. /**
  26. * 将对象数组转换为对象
  27. * @param {array} array Array of Objects
  28. * @param {string} keyField string
  29. */
  30. function arrayToObject(array, keyField) {
  31. return array.reduce(function (obj, item) {
  32. obj[item[keyField]] = item;
  33. return obj;
  34. }, {});
  35. }
  36. /**
  37. * HTML 编码
  38. * 将 < > 等字符串进行编码
  39. * @param {string} str 文本
  40. */
  41. function htmlEncode(str) {
  42. if (!str) return "";
  43. return str.replace(/[<>]/gim, function (i) {
  44. return "&#" + i.charCodeAt(0) + ";";
  45. });
  46. }
  47. /**
  48. * 渲染编辑器
  49. * [x] => <img src="x" />
  50. * @param {strig} content
  51. */
  52. function renderContent(content, onClick) {
  53. var newContent = content;
  54. if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
  55. newContent = newContent.split(_constant.IMAGE_SPLIT);
  56. newContent.pop();
  57. newContent = newContent.join("");
  58. }
  59. // 不包含在标签内的链接
  60. var innerUrl = /((http(s)?:)?\/\/)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)(?![^<>]*>|[^"]*?<\/a)/g;
  61. var data = htmlEncode(newContent).replace(_constant.REGEXP, function (a, b) {
  62. var src = a.slice(1, -1);
  63. // 兼容旧的评
  64. // 因为旧的评论用 [img url] 方式存储的
  65. if (isUrl(src)) {
  66. return "<a href=\"" + src + "\" rel=\"noopener noreferrer\" target=\"_blank\"><img class=\"comment-img\" src=\"" + src + "\" alt=\"" + src + "\" /></a>";
  67. }
  68. // 如果不存在对应的表情, 则返回原文
  69. var emoji = emojiObejct[src];
  70. if (emoji) {
  71. return "<img class=\"comment-emoji\" src=\"" + _emoji.prefixUrl + emoji.value + "." + emoji.ext + "\" alt=\"" + emoji.title + "\" />";
  72. }
  73. return "[" + src + "]";
  74. }).replace(innerUrl, function (a, b) {
  75. var protocol = /^(https?:)?\/\//;
  76. var hasProtocol = protocol.test(a);
  77. var url = hasProtocol ? a : "//" + a;
  78. // target="_blank" 存在安全性问题
  79. // return `<a href="${url}" target="_blank" rel="noopener noreferrer" >${a}</a>`;
  80. return "<a href=\"" + url + "\">" + a + "</a>";
  81. }).replace(/\n/g, "<br />");
  82. return data;
  83. }
  84. function addImageProcess(url) {
  85. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  86. if (options.small) {
  87. return url + _constant.IMAGE_PROCESS_SMALL;
  88. }
  89. if (options.large) {
  90. return url + _constant.IMAGE_PROCESS_LARGE;
  91. }
  92. return url + _constant.IMAGE_PROCESS;
  93. }
  94. //# sourceMappingURL=helper.js.map