From 2128925a3b7bd172afc7b6e0b24dae75dfbc055c Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Sun, 22 Aug 2021 13:50:41 -0700 Subject: [PATCH] Add targets to any link in an HTML email. This allows you to click on the email links and they open in a new tab instead of within the iframe. Fixes #31 (#56) --- src/breeze_carbon/actions/emails/render.cr | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/breeze_carbon/actions/emails/render.cr b/src/breeze_carbon/actions/emails/render.cr index 69f7481..317748c 100644 --- a/src/breeze_carbon/actions/emails/render.cr +++ b/src/breeze_carbon/actions/emails/render.cr @@ -12,8 +12,10 @@ class BreezeCarbon::Emails::Render < Breeze::BreezeAction end else if html_body = email.html_body + pared_markup = parse_body_and_replace_links(html_body) + send_text_response( - html_body, + pared_markup, content_type: "text/html", status: 200 ) @@ -23,6 +25,23 @@ class BreezeCarbon::Emails::Render < Breeze::BreezeAction end end + # Scan all links in the email, and add the target _blank + # This is so you can click on the links, and they don't + # open up in the iframe. + private def parse_body_and_replace_links(body : String) + body.scan(/]+)>/) do |match| + if data = match[1]? + if data.includes?(%(target=)) + # target is already included + else + body = body.sub("