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 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var Smileys = require("./smileys.json");
  17. /**
  18. * Processes links and smileys in "body"
  19. */
  20. function processReplacements(body)
  21. {
  22. //make links clickable
  23. body = linkify(body);
  24. //add smileys
  25. body = smilify(body);
  26. return body;
  27. }
  28. /**
  29. * Finds and replaces all links in the links in "body"
  30. * with their <a href=""></a>
  31. */
  32. function linkify(inputText)
  33. {
  34. var replacedText, replacePattern1, replacePattern2, replacePattern3;
  35. //URLs starting with http://, https://, or ftp://
  36. replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
  37. replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
  38. //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
  39. replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  40. replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
  41. //Change email addresses to mailto:: links.
  42. replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
  43. replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
  44. return replacedText;
  45. }
  46. /**
  47. * Replaces common smiley strings with images
  48. */
  49. function smilify(body)
  50. {
  51. if(!body) {
  52. return body;
  53. }
  54. var regexs = Smileys["regexs"];
  55. for(var smiley in regexs) {
  56. if(regexs.hasOwnProperty(smiley)) {
  57. body = body.replace(regexs[smiley],
  58. '<img class="smiley" src="images/smileys/' + smiley + '.svg">');
  59. }
  60. }
  61. return body;
  62. }
  63. module.exports = {
  64. processReplacements: processReplacements,
  65. linkify: linkify
  66. };