summaryrefslogtreecommitdiffstats
path: root/core/js
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2018-01-02 11:20:15 +0100
committerArthur Schiwon <blizzz@arthur-schiwon.de>2018-01-03 13:11:20 +0100
commit282567fcedf34d2b7043f90cd5b7ce18d4753720 (patch)
tree210d8c24fd4b691913ea2de8e24a2e536f5c5fbe /core/js
parent02b092f35830b916c4fc61413600f5ca86d1630a (diff)
downloadnextcloud-server-282567fcedf34d2b7043f90cd5b7ce18d4753720.tar.gz
nextcloud-server-282567fcedf34d2b7043f90cd5b7ce18d4753720.zip
format links in comments
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'core/js')
-rw-r--r--core/js/core.json1
-rw-r--r--core/js/merged-template-prepend.json1
-rw-r--r--core/js/public/comments.js60
3 files changed, 62 insertions, 0 deletions
diff --git a/core/js/core.json b/core/js/core.json
index 15e406bf2d2..6cf6a2489ed 100644
--- a/core/js/core.json
+++ b/core/js/core.json
@@ -45,6 +45,7 @@
"eventsource.js",
"config.js",
"public/appconfig.js",
+ "public/comments.js",
"multiselect.js",
"oc-requesttoken.js",
"setupchecks.js",
diff --git a/core/js/merged-template-prepend.json b/core/js/merged-template-prepend.json
index 0de1da0bf62..f4ef511bc78 100644
--- a/core/js/merged-template-prepend.json
+++ b/core/js/merged-template-prepend.json
@@ -6,6 +6,7 @@
"octemplate.js",
"eventsource.js",
"public/appconfig.js",
+ "public/comments.js",
"config.js",
"oc-requesttoken.js",
"apps.js",
diff --git a/core/js/public/comments.js b/core/js/public/comments.js
new file mode 100644
index 00000000000..6de7ff7d38a
--- /dev/null
+++ b/core/js/public/comments.js
@@ -0,0 +1,60 @@
+/**
+ * @copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
+ *
+ * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ */
+
+(function(OCP) {
+ "use strict";
+
+ OCP.Comments = {
+
+ /*
+ * Detects links:
+ * Either the http(s) protocol is given or two strings, basically limited to ascii with the last
+ * word being at least one digit long,
+ * followed by at least another character
+ *
+ * The downside: anything not ascii is excluded. Not sure how common it is in areas using different
+ * alphabets… the upside: fake domains with similar looking characters won't be formatted as links
+ */
+ urlRegex: /(\b(https?:\/\/|([-A-Z0-9+_])*\.([-A-Z])+)[-A-Z0-9+&@#\/%?=~_|!:,.;()]*[-A-Z0-9+&@#\/%=~_|()])/ig,
+ protocolRegex: /^https:\/\//,
+
+ plainToRich: function(content) {
+ content = this.formatLinksRich(content);
+ return content;
+ },
+
+ richToPlain: function(content) {
+ content = this.formatLinksPlain(content);
+ return content;
+ },
+
+ formatLinksRich: function(content) {
+ var self = this;
+ return content.replace(this.urlRegex, function(url) {
+ var hasProtocol = (url.indexOf('https://') !== -1) || (url.indexOf('http://') !== -1);
+ if(!hasProtocol) {
+ url = 'https://' + url;
+ }
+
+ var linkText = url.replace(self.protocolRegex, '');
+ return '<a class="external" href="' + url + '">' + linkText + '</a>';
+ });
+ },
+
+ formatLinksPlain: function(content) {
+ var $content = $('<div></div>').html(content);
+ $content.find('a').each(function () {
+ var $this = $(this);
+ $this.html($this.attr('href'));
+ });
+ return $content.html();
+ }
+
+ };
+})(OCP);