You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Replacement.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /* eslint-disable no-useless-escape */
  20. //URLs starting with http://, https://, or ftp://
  21. replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
  22. replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>');
  23. //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
  24. replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  25. replacedText = replacedText.replace(replacePattern2, '$1<a href="https://$2" target="_blank" rel="noopener noreferrer">$2</a>');
  26. //Change email addresses to mailto: links.
  27. replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
  28. replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
  29. /* eslint-enable no-useless-escape */
  30. return replacedText;
  31. }
  32. /**
  33. * Replaces common smiley strings with images
  34. */
  35. function smilify(body) {
  36. if(!body) {
  37. return body;
  38. }
  39. for(var smiley in regexes) {
  40. if(regexes.hasOwnProperty(smiley)) {
  41. body = body.replace(regexes[smiley],
  42. '<img class="smiley" src="images/smileys/' + smiley + '.svg">');
  43. }
  44. }
  45. return body;
  46. }