通用评论

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