From 54571268e514402102f52ce6a1576a1eb3b19d62 Mon Sep 17 00:00:00 2001 From: Alex Harrison Date: Thu, 9 Feb 2012 23:30:28 +0100 Subject: [PATCH 1/2] Added a post conversion hook to PageDown to add target="_blank" (fixes #2792) --- public/javascripts/app/helpers/text_formatter.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/public/javascripts/app/helpers/text_formatter.js b/public/javascripts/app/helpers/text_formatter.js index d977414f3..51bc72b4a 100644 --- a/public/javascripts/app/helpers/text_formatter.js +++ b/public/javascripts/app/helpers/text_formatter.js @@ -12,6 +12,11 @@ textFormatter.markdownify = function markdownify(text){ var converter = Markdown.getSanitizingConverter(); + + converter.hooks.chain("postConversion", function (text) { + return text.replace(/(\"(?:(?:http|https):\/\/)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?\")(\>)/g, '$1 target="_blank">') + }); + return converter.makeHtml(text) }; From 763f8465eb44dfa192d9e0c860b2fc7802d0496c Mon Sep 17 00:00:00 2001 From: Alex Harrison Date: Sun, 12 Feb 2012 22:43:52 +0100 Subject: [PATCH 2/2] Added test for adding target=_blank to autolinks --- spec/javascripts/app/helpers/text_formatter_spec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/javascripts/app/helpers/text_formatter_spec.js b/spec/javascripts/app/helpers/text_formatter_spec.js index 6d408c130..375130147 100644 --- a/spec/javascripts/app/helpers/text_formatter_spec.js +++ b/spec/javascripts/app/helpers/text_formatter_spec.js @@ -40,7 +40,9 @@ describe("app.helpers.textFormatter", function(){ var wrapper = $("
").html(formattedText); _.each(links, function(link) { - expect(wrapper.find("a[href='" + link + "']").text()).toContain(link) + var linkElement = wrapper.find("a[href='" + link + "']"); + expect(linkElement.text()).toContain(link); + expect(linkElement.attr("target")).toContain("_blank"); }) }) })