wxDiscode.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // HTML 支持的数学符号
  2. function strNumDiscode(str) {
  3. str = str.replace(/∀|∀|∀/g, "∀");
  4. str = str.replace(/∂|∂|∂/g, "∂");
  5. str = str.replace(/∃|∃|∃/g, "∃");
  6. str = str.replace(/∅|∅|∅/g, "∅");
  7. str = str.replace(/∇|∇|∇/g, "∇");
  8. str = str.replace(/∈|∈|∈/g, "∈");
  9. str = str.replace(/∉|∉|∉/g, "∉");
  10. str = str.replace(/∋|∋|∋/g, "∋");
  11. str = str.replace(/∏|∏|∏/g, "∏");
  12. str = str.replace(/∑|∑|∑/g, "∑");
  13. str = str.replace(/−|−|−/g, "−");
  14. str = str.replace(/∗|∗|∗/g, "∗");
  15. str = str.replace(/√|√|√/g, "√");
  16. str = str.replace(/∝|∝|∝/g, "∝");
  17. str = str.replace(/∞|∞|∞/g, "∞");
  18. str = str.replace(/∠|∠|∠/g, "∠");
  19. str = str.replace(/∧|∧|∧/g, "∧");
  20. str = str.replace(/∨|∨|∨/g, "∨");
  21. str = str.replace(/∩|∩|∩/g, "∩");
  22. str = str.replace(/∪|∪|∪/g, "∪");
  23. str = str.replace(/∫|∫|∫/g, "∫");
  24. str = str.replace(/∴|∴|∴/g, "∴");
  25. str = str.replace(/∼|∼|∼/g, "∼");
  26. str = str.replace(/≅|≅|≅/g, "≅");
  27. str = str.replace(/≈|≈|≈/g, "≈");
  28. str = str.replace(/≠|≠|≠/g, "≠");
  29. str = str.replace(/≤|≤|≤/g, "≤");
  30. str = str.replace(/≥|≥|≥/g, "≥");
  31. str = str.replace(/⊂|⊂|⊂/g, "⊂");
  32. str = str.replace(/⊃|⊃|⊃/g, "⊃");
  33. str = str.replace(/⊄|⊄|⊄/g, "⊄");
  34. str = str.replace(/⊆|⊆|⊆/g, "⊆");
  35. str = str.replace(/⊇|⊇|⊇/g, "⊇");
  36. str = str.replace(/⊕|⊕|⊕/g, "⊕");
  37. str = str.replace(/⊗|⊗|⊗/g, "⊗");
  38. str = str.replace(/⊥|⊥|⊥/g, "⊥");
  39. str = str.replace(/⋅|⋅|⋅/g, "⋅");
  40. return str;
  41. }
  42. // HTML 支持的希腊字母
  43. function strGreeceDiscode(str) {
  44. str = str.replace(/Α|Α|Α/g, "Α");
  45. str = str.replace(/Β|Β|Β/g, "Β");
  46. str = str.replace(/Γ|Γ|Γ/g, "Γ");
  47. str = str.replace(/Δ|Δ|Δ/g, "Δ");
  48. str = str.replace(/Ε|Ε|Ε/g, "Ε");
  49. str = str.replace(/Ζ|Ζ|Ζ/g, "Ζ");
  50. str = str.replace(/Η|Η|Η/g, "Η");
  51. str = str.replace(/Θ|Θ|Θ/g, "Θ");
  52. str = str.replace(/Ι|Ι|Ι/g, "Ι");
  53. str = str.replace(/Κ|Κ|Κ/g, "Κ");
  54. str = str.replace(/Λ|Λ|Λ/g, "Λ");
  55. str = str.replace(/Μ|Μ|Μ/g, "Μ");
  56. str = str.replace(/Ν|Ν|Ν/g, "Ν");
  57. str = str.replace(/Ξ|Ν|Ν/g, "Ν");
  58. str = str.replace(/Ο|Ο|Ο/g, "Ο");
  59. str = str.replace(/Π|Π|Π/g, "Π");
  60. str = str.replace(/Ρ|Ρ|Ρ/g, "Ρ");
  61. str = str.replace(/Σ|Σ|Σ/g, "Σ");
  62. str = str.replace(/Τ|Τ|Τ/g, "Τ");
  63. str = str.replace(/Υ|Υ|Υ/g, "Υ");
  64. str = str.replace(/Φ|Φ|Φ/g, "Φ");
  65. str = str.replace(/Χ|Χ|Χ/g, "Χ");
  66. str = str.replace(/Ψ|Ψ|Ψ/g, "Ψ");
  67. str = str.replace(/Ω|Ω|Ω/g, "Ω");
  68. str = str.replace(/α|α|α/g, "α");
  69. str = str.replace(/β|β|β/g, "β");
  70. str = str.replace(/γ|γ|γ/g, "γ");
  71. str = str.replace(/δ|δ|δ/g, "δ");
  72. str = str.replace(/ε|ε|ε/g, "ε");
  73. str = str.replace(/ζ|ζ|ζ/g, "ζ");
  74. str = str.replace(/η|η|η/g, "η");
  75. str = str.replace(/θ|θ|θ/g, "θ");
  76. str = str.replace(/ι|ι|ι/g, "ι");
  77. str = str.replace(/κ|κ|κ/g, "κ");
  78. str = str.replace(/λ|λ|λ/g, "λ");
  79. str = str.replace(/μ|μ|μ/g, "μ");
  80. str = str.replace(/ν|ν|ν/g, "ν");
  81. str = str.replace(/ξ|ξ|ξ/g, "ξ");
  82. str = str.replace(/ο|ο|ο/g, "ο");
  83. str = str.replace(/π|π|π/g, "π");
  84. str = str.replace(/ρ|ρ|ρ/g, "ρ");
  85. str = str.replace(/ς|ς|ς/g, "ς");
  86. str = str.replace(/σ|σ|σ/g, "σ");
  87. str = str.replace(/τ|τ|τ/g, "τ");
  88. str = str.replace(/υ|υ|υ/g, "υ");
  89. str = str.replace(/φ|φ|φ/g, "φ");
  90. str = str.replace(/χ|χ|χ/g, "χ");
  91. str = str.replace(/ψ|ψ|ψ/g, "ψ");
  92. str = str.replace(/ω|ω|ω/g, "ω");
  93. str = str.replace(/ϑ|ϑ|ϑ/g, "ϑ");
  94. str = str.replace(/ϒ|ϒ|ϒ/g, "ϒ");
  95. str = str.replace(/ϖ|ϖ|ϖ/g, "ϖ");
  96. str = str.replace(/·|·|·/g, "·");
  97. return str;
  98. }
  99. function strcharacterDiscode(str) {
  100. // 加入常用解析
  101. // str = str.replace(/ | | /g, " ");
  102. // str = str.replace(/ | | /g, ' ');
  103. // str = str.replace(/&#12288;|&#x3000;/g, '<span class=\'spaceshow\'> </span>');
  104. // str = str.replace(/&emsp;|&#8195;|&#x2003;/g, '&emsp;');
  105. // str = str.replace(/&quot;|&#34;|&#x22;/g, "\"");
  106. // str = str.replace(/&apos;|&#39;|&#x27;/g, "&apos;");
  107. // str = str.replace(/&acute;|&#180;|&#xB4;/g, "´");
  108. // str = str.replace(/&times;|&#215;|&#xD7;/g, "×");
  109. // str = str.replace(/&divide;|&#247;|&#xF7;/g, "÷");
  110. // str = str.replace(/&amp;|&#38;|&#x26;/g, '&amp;');
  111. // str = str.replace(/&lt;|&#60;|&#x3c;/g, '&lt;');
  112. // str = str.replace(/&gt;|&#62;|&#x3e;/g, '&gt;');
  113. str = str.replace(/&nbsp;|&#32;|&#x20;/g, "<span class='spaceshow'> </span>");
  114. str = str.replace(
  115. /&ensp;|&#8194;|&#x2002;/g,
  116. "<span class='spaceshow'> </span>",
  117. );
  118. str = str.replace(/&#12288;|&#x3000;/g, "<span class='spaceshow'> </span>");
  119. str = str.replace(
  120. /&emsp;|&#8195;|&#x2003;/g,
  121. "<span class='spaceshow'> </span>",
  122. );
  123. str = str.replace(/&quot;|&#34;|&#x22;/g, '"');
  124. str = str.replace(/&quot;|&#39;|&#x27;/g, "'");
  125. str = str.replace(/&acute;|&#180;|&#xB4;/g, "´");
  126. str = str.replace(/&times;|&#215;|&#xD7;/g, "×");
  127. str = str.replace(/&divide;|&#247;|&#xF7;/g, "÷");
  128. str = str.replace(/&amp;|&#38;|&#x26;/g, "&");
  129. str = str.replace(/&lt;|&#60;|&#x3c;/g, "<");
  130. str = str.replace(/&gt;|&#62;|&#x3e;/g, ">");
  131. return str;
  132. }
  133. // HTML 支持的其他实体
  134. function strOtherDiscode(str) {
  135. str = str.replace(/&OElig;|&#338;|&#x152;/g, "Œ");
  136. str = str.replace(/&oelig;|&#339;|&#x153;/g, "œ");
  137. str = str.replace(/&Scaron;|&#352;|&#x160;/g, "Š");
  138. str = str.replace(/&scaron;|&#353;|&#x161;/g, "š");
  139. str = str.replace(/&Yuml;|&#376;|&#x178;/g, "Ÿ");
  140. str = str.replace(/&fnof;|&#402;|&#x192;/g, "ƒ");
  141. str = str.replace(/&circ;|&#710;|&#x2c6;/g, "ˆ");
  142. str = str.replace(/&tilde;|&#732;|&#x2dc;/g, "˜");
  143. str = str.replace(
  144. /&thinsp;|$#8201;|&#x2009;/g,
  145. "<span class='spaceshow'> </span>",
  146. );
  147. str = str.replace(
  148. /&zwnj;|&#8204;|&#x200C;/g,
  149. "<span class='spaceshow'>‌</span>",
  150. );
  151. str = str.replace(
  152. /&zwj;|$#8205;|&#x200D;/g,
  153. "<span class='spaceshow'>‍</span>",
  154. );
  155. str = str.replace(
  156. /&lrm;|$#8206;|&#x200E;/g,
  157. "<span class='spaceshow'>‎</span>",
  158. );
  159. str = str.replace(
  160. /&rlm;|&#8207;|&#x200F;/g,
  161. "<span class='spaceshow'>‏</span>",
  162. );
  163. str = str.replace(/&ndash;|&#8211;|&#x2013;/g, "–");
  164. str = str.replace(/&mdash;|&#8212;|&#x2014;/g, "—");
  165. str = str.replace(/&lsquo;|&#8216;|&#x2018;/g, "‘");
  166. str = str.replace(/&rsquo;|&#8217;|&#x2019;/g, "’");
  167. str = str.replace(/&sbquo;|&#8218;|&#x201a;/g, "‚");
  168. str = str.replace(/&ldquo;|&#8220;|&#x201c;/g, "“");
  169. str = str.replace(/&rdquo;|&#8221;|&#x201d;/g, "”");
  170. str = str.replace(/&bdquo;|&#8222;|&#x201e;/g, "„");
  171. str = str.replace(/&dagger;|&#8224;|&#x2020;/g, "†");
  172. str = str.replace(/&Dagger;|&#8225;|&#x2021;/g, "‡");
  173. str = str.replace(/&bull;|&#8226;|&#x2022;/g, "•");
  174. str = str.replace(/&hellip;|&#8230;|&#x2026;/g, "…");
  175. str = str.replace(/&permil;|&#8240;|&#x2030;/g, "‰");
  176. str = str.replace(/&prime;|&#8242;|&#x2032;/g, "′");
  177. str = str.replace(/&Prime;|&#8243;|&#x2033;/g, "″");
  178. str = str.replace(/&lsaquo;|&#8249;|&#x2039;/g, "‹");
  179. str = str.replace(/&rsaquo;|&#8250;|&#x203a;/g, "›");
  180. str = str.replace(/&oline;|&#8254;|&#x203e;/g, "‾");
  181. str = str.replace(/&euro;|&#8364;|&#x20ac;/g, "€");
  182. str = str.replace(/&trade;|&#8482;|&#x2122;/g, "™");
  183. str = str.replace(/&larr;|&#8592;|&#x2190;/g, "←");
  184. str = str.replace(/&uarr;|&#8593;|&#x2191;/g, "↑");
  185. str = str.replace(/&rarr;|&#8594;|&#x2192;/g, "→");
  186. str = str.replace(/&darr;|&#8595;|&#x2193;/g, "↓");
  187. str = str.replace(/&harr;|&#8596;|&#x2194;/g, "↔");
  188. str = str.replace(/&crarr;|&#8629;|&#x21b5;/g, "↵");
  189. str = str.replace(/&lceil;|&#8968;|&#x2308;/g, "⌈");
  190. str = str.replace(/&rceil;|&#8969;|&#x2309;/g, "⌉");
  191. str = str.replace(/&lfloor;|&#8970;|&#x230a;/g, "⌊");
  192. str = str.replace(/&rfloor;|&#8971;|&#x230b;/g, "⌋");
  193. str = str.replace(/&loz;|&#9674;|&#x25ca;/g, "◊");
  194. str = str.replace(/&spades;|&#9824;|&#x2660;/g, "♠");
  195. str = str.replace(/&clubs;|&#9827;|&#x2663;/g, "♣");
  196. str = str.replace(/&hearts;|&#9829;|&#x2665;/g, "♥");
  197. str = str.replace(/&diams;|&#9830;|&#x2666;/g, "♦");
  198. return str;
  199. }
  200. function strDiscode(str) {
  201. str = strNumDiscode(str);
  202. str = strGreeceDiscode(str);
  203. str = strcharacterDiscode(str);
  204. str = strOtherDiscode(str);
  205. return str;
  206. }
  207. function urlToHttpUrl(url, domain) {
  208. if (/^\/\//.test(url)) {
  209. return `https:${url}`;
  210. } else if (/^\//.test(url)) {
  211. return `https://${domain}${url}`;
  212. }
  213. return url;
  214. }
  215. export default {
  216. strDiscode,
  217. urlToHttpUrl,
  218. };