From e1ca1de7413a64ab87ae890425e0889812e2a612 Mon Sep 17 00:00:00 2001 From: Bob Karreman Date: Wed, 28 Nov 2012 15:11:50 +0100 Subject: [PATCH 1/2] Added support for www. links --- linkify.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/linkify.js b/linkify.js index 4d8013c..8e6fec6 100644 --- a/linkify.js +++ b/linkify.js @@ -51,9 +51,11 @@ function linkify(text) { ) # End $14. Other non-delimited URL. /imx'; */ - var url_pattern = /(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img; + var url_pattern = /(\()((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img; var url_replace = '$1$4$7$10$13$2$5$8$11$14$3$6$9$12'; - return text.replace(url_pattern, url_replace); + var output = text.replace(url_pattern, url_replace); + output = output.replace(/href="www\./img, 'href="http://www.'); + return output; } function linkify_html(text) { text = text.replace(/&apos;/g, '''); // IE does not handle ' entity! From fc1289d80eef349c23b7ebca121c699e287bf481 Mon Sep 17 00:00:00 2001 From: Bob Karreman Date: Fri, 30 Nov 2012 15:41:34 +0100 Subject: [PATCH 2/2] Add option to specifiy the link target and default to _self --- linkify.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/linkify.js b/linkify.js index 8e6fec6..6512dac 100644 --- a/linkify.js +++ b/linkify.js @@ -8,7 +8,7 @@ * * Usage: See demonstration page: linkify.html */ -function linkify(text) { +function linkify(text, provided_target) { /* Here is a commented version of the regex (in PHP string format): $url_pattern = '/# Rev:20100913_0900 github.com\/jmrware\/LinkifyURL # Match http & ftp URL that is not already linkified. @@ -51,8 +51,12 @@ function linkify(text) { ) # End $14. Other non-delimited URL. /imx'; */ + var target = '_self'; + if (provided_target) { + target = provided_target; + } var url_pattern = /(\()((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:(?:ht|f)tps?:\/\/|www\.)[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img; - var url_replace = '$1$4$7$10$13$2$5$8$11$14$3$6$9$12'; + var url_replace = '$1$4$7$10$13$2$5$8$11$14$3$6$9$12'; var output = text.replace(url_pattern, url_replace); output = output.replace(/href="www\./img, 'href="http://www.'); return output;