您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Replacement.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* jshint -W101 */
  2. import { regexes } from './smileys';
  3. /**
  4. * Processes links and smileys in "body"
  5. */
  6. export function processReplacements(body) {
  7. //make links clickable
  8. body = linkify(body);
  9. //add smileys
  10. body = smilify(body);
  11. return body;
  12. }
  13. /**
  14. * Finds and replaces all links in the links in "body"
  15. * with their <a href=""></a>
  16. */
  17. export function linkify(inputText) {
  18. var replacedText, replacePattern1, replacePattern2, replacePattern3;
  19. //URLs starting with http://, https://, or ftp://
  20. replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
  21. replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
  22. //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
  23. replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  24. replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
  25. //Change email addresses to mailto: links.
  26. replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
  27. replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
  28. return replacedText;
  29. }
  30. /**
  31. * Replaces common smiley strings with images
  32. */
  33. function smilify(body) {
  34. if(!body) {
  35. return body;
  36. }
  37. for(var smiley in regexes) {
  38. if(regexes.hasOwnProperty(smiley)) {
  39. body = body.replace(regexes[smiley],
  40. '<img class="smiley" src="images/smileys/' + smiley + '.svg">');
  41. }
  42. }
  43. return body;
  44. }