Skip to content

Commit

Permalink
Add targets to any link in an HTML email. This allows you to click on…
Browse files Browse the repository at this point in the history
… the email links and they open in a new tab instead of within the iframe. Fixes #31 (#56)
  • Loading branch information
jwoertink authored Aug 22, 2021
1 parent 3eddf0a commit 2128925
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/breeze_carbon/actions/emails/render.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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(/<a\s*([^>]+)>/) do |match|
if data = match[1]?
if data.includes?(%(target=))
# target is already included
else
body = body.sub("<a #{data}", %(<a #{data} target="_blank"))
end
end
end

body
end

private def render_missing_template_error(email : Carbon::Email, template : String)
plain_text(<<-MESSAGE, status: 400)
The email #{email.class} does not have
Expand Down

0 comments on commit 2128925

Please sign in to comment.