通用评论

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. 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. return htmlEncode(newContent).replace(_constant.REGEXP, function (a, b) {
  58. var src = a.slice(1, -1);
  59. // 兼容旧的评
  60. // 因为旧的评论用 [img url] 方式存储的
  61. if (isUrl(src)) {
  62. return "<br/><img src=\"" + src + "\" alt=\"" + src + "\" style=\"max-width: 100%\" />";
  63. }
  64. // 如果不存在对应的表情, 则返回原文
  65. var emoji = emojiObejct[src];
  66. if (emoji) {
  67. return "<img src=\"" + _emoji.prefixUrl + emoji.value + "." + emoji.ext + "\" alt=\"" + emoji.title + "\" />";
  68. }
  69. return "[" + src + "]";
  70. }).replace(/\n/g, "<br />");
  71. }
  72. //# sourceMappingURL=helper.js.map