通用评论

helper.js 3.3KB

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