通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. var value = emojiObejct[src] ? emojiObejct[src].value : src;
  65. return "<img src=\"" + _emoji.prefixUrl + value + "." + _emoji.ext + "\" alt=\"" + value + "\" />";
  66. });
  67. }
  68. //# sourceMappingURL=helper.js.map