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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* jshint -W101 */
  2. var Smileys = require("./smileys.json");
  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. var regexs = Smileys.regexs;
  38. for(var smiley in regexs) {
  39. if(regexs.hasOwnProperty(smiley)) {
  40. body = body.replace(regexs[smiley],
  41. '<img class="smiley" src="images/smileys/' + smiley + '.svg">');
  42. }
  43. }
  44. return body;
  45. }