通用评论

1
  1. {"version":3,"sources":["../src/helper.js"],"names":["isFunction","isUrl","arrayToObject","htmlEncode","renderContent","addImageProcess","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","options","small","IMAGE_PROCESS_SMALL","large","IMAGE_PROCESS_LARGE","IMAGE_PROCESS"],"mappings":";;;;;QAWgBA,U,GAAAA,U;QAMAC,K,GAAAA,K;QAaAC,a,GAAAA,a;QAYAC,U,GAAAA,U;QAYAC,a,GAAAA,a;QAuCAC,e,GAAAA,e;;AA7FhB;;AAOA;;;;;;AAEA,IAAMC,cAAcJ,cAAcK,eAAd,EAAqB,OAArB,CAApB;;AAEO,SAASP,UAAT,CAAoBQ,eAApB,EAAqC;AAC1C,SACEA,mBAAmB,GAAGC,QAAH,CAAYC,IAAZ,CAAiBF,eAAjB,MAAsC,mBAD3D;AAGD;;AAEM,SAASP,KAAT,CAAeU,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,SAASX,aAAT,CAAuBa,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,SAASf,UAAT,CAAoBiB,GAApB,EAAyB;AAC9B,MAAI,CAACA,GAAL,EAAU,OAAO,EAAP;AACV,SAAOA,IAAIC,OAAJ,CAAY,SAAZ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L,CAAb;AA2BA,SAAOY,IAAP;AACD;;AAEM,SAAS5B,eAAT,CAAyByC,GAAzB,EAA4C;AAAA,MAAdC,OAAc,uEAAJ,EAAI;;AACjD,MAAIA,QAAQC,KAAZ,EAAmB;AACjB,WAAOF,MAAMG,6BAAb;AACD;AACD,MAAIF,QAAQG,KAAZ,EAAmB;AACjB,WAAOJ,MAAMK,6BAAb;AACD;AACD,SAAOL,MAAMM,uBAAb;AACD","file":"helper.js","sourcesContent":["import {\n REGEXP,\n IMAGE_SPLIT,\n IMAGE_PROCESS_SMALL,\n IMAGE_PROCESS_LARGE,\n IMAGE_PROCESS\n} from \"./constant\";\nimport emoji, { prefixUrl } from \"./emoji\";\n\nconst emojiObejct = arrayToObject(emoji, \"title\");\n\nexport function isFunction(functionToCheck) {\n return (\n functionToCheck && {}.toString.call(functionToCheck) === \"[object Function]\"\n );\n}\n\nexport function isUrl(userInput) {\n // 需完整匹配\n const regexp = /^((http(s)?:)?\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;\n var res = userInput.match(regexp);\n if (res === null) return false;\n else return true;\n}\n\n/**\n * 将对象数组转换为对象\n * @param {array} array Array of Objects\n * @param {string} keyField string\n */\nexport function arrayToObject(array, keyField) {\n return array.reduce((obj, item) => {\n obj[item[keyField]] = item;\n return obj;\n }, {});\n}\n\n/**\n * HTML 编码\n * 将 < > 等字符串进行编码\n * @param {string} str 文本\n */\nexport function htmlEncode(str) {\n if (!str) return \"\";\n return str.replace(/[<>]/gim, function(i) {\n return \"&#\" + i.charCodeAt(0) + \";\";\n });\n}\n\n/**\n * 渲染编辑器\n * [x] => <img src=\"x\" />\n * @param {strig} content\n */\nexport function renderContent(content, onClick) {\n let newContent = content;\n if (newContent.indexOf(IMAGE_SPLIT) !== -1) {\n newContent = newContent.split(IMAGE_SPLIT);\n newContent.pop();\n newContent = newContent.join(\"\");\n }\n // 不包含在标签内的链接\n 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;\n const data = htmlEncode(newContent)\n .replace(REGEXP, function(a, b) {\n const src = a.slice(1, -1);\n\n // 兼容旧的评\n // 因为旧的评论用 [img url] 方式存储的\n if (isUrl(src)) {\n return `<a href=\"${src}\" rel=\"noopener noreferrer\" target=\"_blank\"><img class=\"comment-img\" src=\"${src}\" alt=\"${src}\" /></a>`;\n }\n // 如果不存在对应的表情, 则返回原文\n const emoji = emojiObejct[src];\n if (emoji) {\n return `<img class=\"comment-emoji\" src=\"${prefixUrl}${emoji.value}.${\n emoji.ext\n }\" alt=\"${emoji.title}\" />`;\n }\n return `[${src}]`;\n })\n .replace(innerUrl, function(a, b) {\n const protocol = /^(https?:)?\\/\\//;\n const hasProtocol = protocol.test(a);\n const url = hasProtocol ? a : `//${a}`;\n // target=\"_blank\" 存在安全性问题\n // return `<a href=\"${url}\" target=\"_blank\" rel=\"noopener noreferrer\" >${a}</a>`;\n return `<a href=\"${url}\">${a}</a>`;\n })\n .replace(/\\n/g, \"<br />\");\n return data;\n}\n\nexport function addImageProcess(url, options = {}) {\n if (options.small) {\n return url + IMAGE_PROCESS_SMALL;\n }\n if (options.large) {\n return url + IMAGE_PROCESS_LARGE;\n }\n return url + IMAGE_PROCESS;\n}\n"]}