소스 검색

Made links in alerts clickable.

Kris 9 년 전
부모
커밋
0bb301ae19
1개의 변경된 파일36개의 추가작업 그리고 3개의 파일을 삭제
  1. 36 3
      app/client/scripts/helpers.js

+ 36 - 3
app/client/scripts/helpers.js

@@ -57,7 +57,12 @@ Template.admin.helpers({
 
 Template.alerts.helpers({
     alerts: function () {
-        return Alerts.find({active: true});
+        var alerts = Alerts.find({active: true}).fetch();
+        alerts = alerts.map(function(alert) {
+            alert.description = replaceURLWithHTMLLinksBlank(alert.description);
+            return alert;
+        });
+        return alerts;
     }
 });
 
@@ -230,7 +235,7 @@ Template.news.helpers({
     articles: function() {
         var articles =  News.find().fetch().reverse();
         articles = articles.map(function(article) {
-            article.content = replaceURLWithHTMLLinks(article.content);
+            article.content = replaceURLWithHTMLLinksBlank(article.content);
             return article;
         });
         return articles;
@@ -682,4 +687,32 @@ function replaceURLWithHTMLLinks(text) {
         }
         return lParens + "<a style='font-size: 18px; padding: 0; display: inline;' target='_blank' href='" + url + "'>" + url + "</a>" + rParens;
     });
-}
+}
+
+function replaceURLWithHTMLLinksBlank(text) {
+    var re = /(\(.*?)?\b((?:https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_()|!:,.;]*[-a-z0-9+&@#\/%=~_()|])/ig;
+    return text.replace(re, function(match, lParens, url) {
+        var rParens = '';
+        lParens = lParens || '';
+
+        // Try to strip the same number of right parens from url
+        // as there are left parens.  Here, lParenCounter must be
+        // a RegExp object.  You cannot use a literal
+        //     while (/\(/g.exec(lParens)) { ... }
+        // because an object is needed to store the lastIndex state.
+        var lParenCounter = /\(/g;
+        while (lParenCounter.exec(lParens)) {
+            var m;
+            // We want m[1] to be greedy, unless a period precedes the
+            // right parenthesis.  These tests cannot be simplified as
+            //     /(.*)(\.?\).*)/.exec(url)
+            // because if (.*) is greedy then \.? never gets a chance.
+            if (m = /(.*)(\.\).*)/.exec(url) ||
+                    /(.*)(\).*)/.exec(url)) {
+                url = m[1];
+                rParens = m[2] + rParens;
+            }
+        }
+        return lParens + "<a href='" + url + "'>" + url + "</a>" + rParens;
+    });
+}