comments.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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: /(\s|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig,
  22. plainToRich: function(content) {
  23. content = this.formatLinksRich(content);
  24. return content;
  25. },
  26. richToPlain: function(content) {
  27. content = this.formatLinksPlain(content);
  28. return content;
  29. },
  30. formatLinksRich: function(content) {
  31. return content.replace(this.urlRegex, function(_, leadingSpace, protocol, url, trailingSpace) {
  32. var linkText = url;
  33. if(!protocol) {
  34. protocol = 'https://';
  35. } else if (protocol === 'http://'){
  36. linkText = protocol + url;
  37. }
  38. return leadingSpace + '<a class="external" target="_blank" rel="noopener noreferrer" href="' + protocol + url + '">' + linkText + '</a>' + trailingSpace;
  39. });
  40. },
  41. formatLinksPlain: function(content) {
  42. var $content = $('<div></div>').html(content);
  43. $content.find('a').each(function () {
  44. var $this = $(this);
  45. $this.html($this.attr('href'));
  46. });
  47. return $content.html();
  48. }
  49. };
  50. })(OCP);