123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.isFunction = isFunction;
- exports.isUrl = isUrl;
- exports.arrayToObject = arrayToObject;
- exports.htmlEncode = htmlEncode;
- exports.renderContent = renderContent;
- exports.addImageProcess = addImageProcess;
-
- var _constant = require("./constant");
-
-
-
-
-
- function isFunction(functionToCheck) {
- return functionToCheck && {}.toString.call(functionToCheck) === "[object Function]";
- }
-
- function isUrl(userInput) {
-
- var regexp = /^((http(s)?:)?\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
- var res = userInput.match(regexp);
- if (res === null) return false;else return true;
- }
-
-
- function arrayToObject(array, keyField) {
- return array.reduce(function (obj, item) {
- obj[item[keyField]] = item;
- return obj;
- }, {});
- }
-
-
- function htmlEncode(str) {
- if (!str) return "";
- return str.replace(/[<>]/gim, function (i) {
- return "&#" + i.charCodeAt(0) + ";";
- });
- }
-
-
- function renderContent(content, onClick) {
- var newContent = content;
- if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
- newContent = newContent.split(_constant.IMAGE_SPLIT);
- newContent.pop();
- newContent = newContent.join("");
- }
-
- 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;
- var data = htmlEncode(newContent).replace(_constant.REGEXP, function (a, b) {
- var src = a.slice(1, -1);
-
-
-
- if (isUrl(src)) {
- return "<a href=\"" + src + "\" rel=\"noopener noreferrer\" target=\"_blank\"><img class=\"comment-img\" src=\"" + src + "\" alt=\"" + src + "\" /></a>";
- }
-
-
- if (sessionStorage.getItem("emojiMap") && JSON.parse(sessionStorage.getItem("emojiMap"))) {
- var emoji = JSON.parse(sessionStorage.getItem("emojiMap"))[src];
- if (emoji) {
- return "<img class=\"comment-emoji\" src=\"" + emoji + "\" alt=\"" + emoji.title + "\" style=\"width:28px\"/>";
- }
- }
- return "[" + src + "]";
- }).replace(innerUrl, function (a, b) {
- var protocol = /^(https?:)?\/\//;
- var hasProtocol = protocol.test(a);
- var url = hasProtocol ? a : "//" + a;
-
-
- return "<a href=\"" + url + "\">" + a + "</a>";
- }).replace(/\n/g, "<br />");
- return data;
- }
-
- function addImageProcess(url) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
-
- if (options.small) {
- return url + _constant.IMAGE_PROCESS_SMALL;
- }
- if (options.large) {
- return url + _constant.IMAGE_PROCESS_LARGE;
- }
- return url + _constant.IMAGE_PROCESS;
- }
|