"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");
var _emoji = require("./emoji");
var _emoji2 = _interopRequireDefault(_emoji);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var emojiObejct = arrayToObject(_emoji2.default, "title");
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;
}
/**
* 将对象数组转换为对象
* @param {array} array Array of Objects
* @param {string} keyField string
*/
function arrayToObject(array, keyField) {
return array.reduce(function (obj, item) {
obj[item[keyField]] = item;
return obj;
}, {});
}
/**
* HTML 编码
* 将 < > 等字符串进行编码
* @param {string} str 文本
*/
function htmlEncode(str) {
if (!str) return "";
return str.replace(/[<>]/gim, function (i) {
return "" + i.charCodeAt(0) + ";";
});
}
/**
* 渲染编辑器
* [x] =>
* @param {strig} content
*/
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);
// 兼容旧的评
// 因为旧的评论用 [img url] 方式存储的
if (isUrl(src)) {
return "";
}
// 如果不存在对应的表情, 则返回原文
var emoji = emojiObejct[src];
if (emoji) {
return "";
}
return "[" + src + "]";
}).replace(innerUrl, function (a, b) {
var protocol = /^(https?:)?\/\//;
var hasProtocol = protocol.test(a);
var url = hasProtocol ? a : "//" + a;
// target="_blank" 存在安全性问题
// return `${a}`;
return "" + a + "";
}).replace(/\n/g, "
");
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;
}
//# sourceMappingURL=helper.js.map