1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.isFunction = isFunction;
- exports.isUrl = isUrl;
- exports.arrayToObject = arrayToObject;
- exports.htmlEncode = htmlEncode;
- exports.renderContent = renderContent;
-
- var _constant = require("./constant");
-
- var _emoji = require("./emoji");
-
- var _emoji2 = _interopRequireDefault(_emoji);
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- 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 "";
- // /[\u00A0-\u9999<>\&]/gim // 中文和 HTML 字符
- return str.replace(/[<>\&]/gim, function (i) {
- return "&#" + i.charCodeAt(0) + ";";
- });
- }
-
- /**
- * 渲染编辑器
- * [x] => <img src="x" />
- * @param {strig} content
- */
- function renderContent(content, onClick) {
- return htmlEncode(content).replace(_constant.REGEXP, function (a, b) {
- var src = a.slice(1, -1);
- if (isUrl(src)) {
- return "<img src=\"" + src + "\" alt=\"" + src + "\" style=\"max-width: 300px\" />";
- }
- var emojiObejct = arrayToObject(_emoji2.default, "title");
- var value = emojiObejct[src].value;
- return "<img src=\"" + _emoji.prefixUrl + value + "." + _emoji.ext + "\" alt=\"" + value + "\" />";
- });
- }
- //# sourceMappingURL=helper.js.map
|