通用评论

1
  1. {"version":3,"sources":["../src/helper.js"],"names":["isFunction","isUrl","arrayToObject","htmlEncode","renderContent","emojiObejct","emoji","functionToCheck","toString","call","userInput","regexp","res","match","array","keyField","reduce","obj","item","str","replace","i","charCodeAt","content","onClick","newContent","indexOf","IMAGE_SPLIT","split","pop","join","innerUrl","data","REGEXP","a","b","src","slice","prefixUrl","value","ext","title","protocol","hasProtocol","test","url"],"mappings":";;;;;QAKgBA,U,GAAAA,U;QAMAC,K,GAAAA,K;QAaAC,a,GAAAA,a;QAYAC,U,GAAAA,U;QAYAC,a,GAAAA,a;;AAhDhB;;AACA;;;;;;AAEA,IAAMC,cAAcH,cAAcI,eAAd,EAAqB,OAArB,CAApB;;AAEO,SAASN,UAAT,CAAoBO,eAApB,EAAqC;AAC1C,SACEA,mBAAmB,GAAGC,QAAH,CAAYC,IAAZ,CAAiBF,eAAjB,MAAsC,mBAD3D;AAGD;;AAEM,SAASN,KAAT,CAAeS,SAAf,EAA0B;AAC/B;AACA,MAAMC,SAAS,sGAAf;AACA,MAAIC,MAAMF,UAAUG,KAAV,CAAgBF,MAAhB,CAAV;AACA,MAAIC,QAAQ,IAAZ,EAAkB,OAAO,KAAP,CAAlB,KACK,OAAO,IAAP;AACN;;AAED;;;;;AAKO,SAASV,aAAT,CAAuBY,KAAvB,EAA8BC,QAA9B,EAAwC;AAC7C,SAAOD,MAAME,MAAN,CAAa,UAACC,GAAD,EAAMC,IAAN,EAAe;AACjCD,QAAIC,KAAKH,QAAL,CAAJ,IAAsBG,IAAtB;AACA,WAAOD,GAAP;AACD,GAHM,EAGJ,EAHI,CAAP;AAID;;AAED;;;;;AAKO,SAASd,UAAT,CAAoBgB,GAApB,EAAyB;AAC9B,MAAI,CAACA,GAAL,EAAU,OAAO,EAAP;AACV,SAAOA,IAAIC,OAAJ,CAAY,SAAZ,EAAuB,UAAUC,CAAV,EAAa;AACzC,WAAO,OAAOA,EAAEC,UAAF,CAAa,CAAb,CAAP,GAAyB,GAAhC;A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import emoji, { prefixUrl } from \"./emoji\";\r\n\r\nconst emojiObejct = arrayToObject(emoji, \"title\");\r\n\r\nexport function isFunction(functionToCheck) {\r\n return (\r\n functionToCheck && {}.toString.call(functionToCheck) === \"[object Function]\"\r\n );\r\n}\r\n\r\nexport function isUrl(userInput) {\r\n // 需完整匹配\r\n const regexp = /^((http(s)?:)?\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;\r\n var res = userInput.match(regexp);\r\n if (res === null) return false;\r\n else return true;\r\n}\r\n\r\n/**\r\n * 将对象数组转换为对象\r\n * @param {array} array Array of Objects\r\n * @param {string} keyField string\r\n */\r\nexport function arrayToObject(array, keyField) {\r\n return array.reduce((obj, item) => {\r\n obj[item[keyField]] = item;\r\n return obj;\r\n }, {});\r\n}\r\n\r\n/**\r\n * HTML 编码\r\n * 将 < > 等字符串进行编码\r\n * @param {string} str 文本\r\n */\r\nexport function htmlEncode(str) {\r\n if (!str) return \"\";\r\n return str.replace(/[<>]/gim, function (i) {\r\n return \"&#\" + i.charCodeAt(0) + \";\";\r\n });\r\n}\r\n\r\n/**\r\n * 渲染编辑器\r\n * [x] => <img src=\"x\" />\r\n * @param {strig} content\r\n */\r\nexport function renderContent(content, onClick) {\r\n let newContent = content;\r\n if (newContent.indexOf(IMAGE_SPLIT) !== -1) {\r\n newContent = newContent.split(IMAGE_SPLIT);\r\n newContent.pop();\r\n newContent = newContent.join(\"\");\r\n }\r\n // 不包含在标签内的链接\r\n const innerUrl = /((http(s)?:)?\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&//=]*)(?![^<>]*>|[^\"]*?<\\/a)/g;\r\n const data = htmlEncode(newContent)\r\n .replace(REGEXP, function (a, b) {\r\n const src = a.slice(1, -1);\r\n\r\n // 兼容旧的评\r\n // 因为旧的评论用 [img url] 方式存储的\r\n if (isUrl(src)) {\r\n return `<a href=\"${src}\" rel=\"noopener noreferrer\" target=\"_blank\"><img class=\"comment-img\" src=\"${src}\" alt=\"${src}\" /></a>`;\r\n }\r\n // 如果不存在对应的表情, 则返回原文\r\n const emoji = emojiObejct[src];\r\n if (emoji) {\r\n return `<img class=\"comment-emoji\" src=\"${prefixUrl}${emoji.value}.${emoji.ext}\" alt=\"${\r\n emoji.title\r\n }\" />`;\r\n }\r\n return `[${src}]`;\r\n })\r\n .replace(innerUrl, function (a, b) {\r\n const protocol = /^(https?:)?\\/\\//;\r\n const hasProtocol = protocol.test(a);\r\n const url = hasProtocol ? a : `//${a}`;\r\n // target=\"_blank\" 存在安全性问题\r\n // return `<a href=\"${url}\" target=\"_blank\" rel=\"noopener noreferrer\" >${a}</a>`;\r\n return `<a href=\"${url}\">${a}</a>`;\r\n })\r\n .replace(/\\n/g, \"<br />\");\r\n return data;\r\n}\r\n"]}