comments.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  3. *
  4. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later. See the COPYING file.
  8. */
  9. (function(OCP) {
  10. "use strict";
  11. OCP.Comments = {
  12. /*
  13. * Detects links:
  14. * Either the http(s) protocol is given or two strings, basically limited to ascii with the last
  15. * word being at least one digit long,
  16. * followed by at least another character
  17. *
  18. * The downside: anything not ascii is excluded. Not sure how common it is in areas using different
  19. * alphabets… the upside: fake domains with similar looking characters won't be formatted as links
  20. */
  21. urlRegex: /(\b(https?:\/\/|([-A-Z0-9+_])*\.([-A-Z])+)[-A-Z0-9+&@#\/%?=~_|!:,.;()]*[-A-Z0-9+&@#\/%=~_|()])/ig,
  22. protocolRegex: /^https:\/\//,
  23. plainToRich: function(content) {
  24. content = this.formatLinksRich(content);
  25. return content;
  26. },
  27. richToPlain: function(content) {
  28. content = this.formatLinksPlain(content);
  29. return content;
  30. },
  31. formatLinksRich: function(content) {
  32. var self = this;
  33. return content.replace(this.urlRegex, function(url) {
  34. var hasProtocol = (url.indexOf('https://') !== -1) || (url.indexOf('http://') !== -1);
  35. if(!hasProtocol) {
  36. url = 'https://' + url;
  37. }
  38. var linkText = url.replace(self.protocolRegex, '');
  39. return '<a class="external" target="_blank" rel="noopener noreferrer" href="' + url + '">' + linkText + '</a>';
  40. });
  41. },
  42. formatLinksPlain: function(content) {
  43. var $content = $('<div></div>').html(content);
  44. $content.find('a').each(function () {
  45. var $this = $(this);
  46. $this.html($this.attr('href'));
  47. });
  48. return $content.html();
  49. }
  50. };
  51. })(OCP);