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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var Smileys = require("./smileys.json");
  2. /**
  3. * Processes links and smileys in "body"
  4. */
  5. function processReplacements(body)
  6. {
  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. function linkify(inputText)
  18. {
  19. var replacedText, replacePattern1, replacePattern2, replacePattern3;
  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">$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="http://$2" target="_blank">$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. return replacedText;
  30. }
  31. /**
  32. * Replaces common smiley strings with images
  33. */
  34. function smilify(body)
  35. {
  36. if(!body) {
  37. return body;
  38. }
  39. var regexs = Smileys["regexs"];
  40. for(var smiley in regexs) {
  41. if(regexs.hasOwnProperty(smiley)) {
  42. body = body.replace(regexs[smiley],
  43. '<img class="smiley" src="images/smileys/' + smiley + '.svg">');
  44. }
  45. }
  46. return body;
  47. }
  48. module.exports = {
  49. processReplacements: processReplacements,
  50. linkify: linkify
  51. };