通用评论

helper.js 3.1KB

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