通用评论

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. function isFunction(functionToCheck) {
  15. return functionToCheck && {}.toString.call(functionToCheck) === "[object Function]";
  16. }
  17. function isUrl(userInput) {
  18. var regexp = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
  19. var res = userInput.match(regexp);
  20. if (res === null) return false;else return true;
  21. }
  22. /**
  23. * 将对象数组转换为对象
  24. * @param {array} array Array of Objects
  25. * @param {string} keyField string
  26. */
  27. function arrayToObject(array, keyField) {
  28. return array.reduce(function (obj, item) {
  29. obj[item[keyField]] = item;
  30. return obj;
  31. }, {});
  32. }
  33. /**
  34. * HTML 编码
  35. * 将 < > 等字符串进行编码
  36. * @param {string} str 文本
  37. */
  38. function htmlEncode(str) {
  39. if (!str) return "";
  40. // /[\u00A0-\u9999<>\&]/gim // 中文和 HTML 字符
  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. return htmlEncode(content).replace(_constant.REGEXP, function (a, b) {
  52. var src = a.slice(1, -1);
  53. if (isUrl(src)) {
  54. return "<img src=\"" + src + "\" alt=\"" + src + "\" style=\"max-width: 300px\" />";
  55. }
  56. var emojiObejct = arrayToObject(_emoji2.default, "title");
  57. var value = emojiObejct[src].value;
  58. return "<img src=\"" + _emoji.prefixUrl + value + "." + _emoji.ext + "\" alt=\"" + value + "\" />";
  59. });
  60. }
  61. //# sourceMappingURL=helper.js.map