From 84fe5f0dd252ee73d5b638d6f132f5506f979bdd Mon Sep 17 00:00:00 2001 From: Jerod Santo Date: Thu, 14 Mar 2024 10:48:54 -0500 Subject: [PATCH] =?UTF-8?q?Set=20News=20img=20widths=20as=20html=20attribu?= =?UTF-8?q?tes=20(=F0=9F=91=80=20Outlook)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/changelog/kits/html_kit.ex | 38 +++++++++++++----- ...shed.html.eex => news_published.html.heex} | 39 +++++++++++-------- test/changelog/kits/html_kit_test.exs | 34 ++++++++++++++-- 3 files changed, 80 insertions(+), 31 deletions(-) rename lib/changelog_web/templates/email/{news_published.html.eex => news_published.html.heex} (71%) diff --git a/lib/changelog/kits/html_kit.ex b/lib/changelog/kits/html_kit.ex index 6b724714c6..4f49157faa 100644 --- a/lib/changelog/kits/html_kit.ex +++ b/lib/changelog/kits/html_kit.ex @@ -38,6 +38,20 @@ defmodule Changelog.HtmlKit do end end + def put_img_width(html, width) when is_binary(width) do + case Floki.parse_document(html) do + {:ok, document} -> + document + |> Floki.attr("img", "width", fn _ -> width end) + |> Floki.raw_html() + + {:error, _string} -> + html + end + end + + def put_img_width(html, width), do: put_img_width(html, Integer.to_string(width)) + def put_ugc({:safe, html}), do: put_ugc(html) def put_ugc(html), do: put_a_rel(html, "ugc") @@ -51,9 +65,10 @@ defmodule Changelog.HtmlKit do {:ok, document} -> document |> Floki.attr("a", "href", fn href -> - merge_href_query_params(href, %{utm_source: value}) + merge_href_query_params(href, %{utm_source: value}) end) |> Floki.raw_html(encode: false) + {:error, _string} -> html end @@ -62,15 +77,18 @@ defmodule Changelog.HtmlKit do defp merge_href_query_params(href, params) do uri = URI.parse(href) - new_uri = cond do - is_nil(uri.query) -> - uri |> Map.put(:query, URI.encode_query(params)) - String.match?(uri.query, ~r/utm_/) -> - uri - true -> - new_query = uri.query |> URI.decode_query() |> Map.merge(params) - uri |> Map.put(:query, URI.encode_query(new_query)) - end + new_uri = + cond do + is_nil(uri.query) -> + uri |> Map.put(:query, URI.encode_query(params)) + + String.match?(uri.query, ~r/utm_/) -> + uri + + true -> + new_query = uri.query |> URI.decode_query() |> Map.merge(params) + uri |> Map.put(:query, URI.encode_query(new_query)) + end URI.to_string(new_uri) end diff --git a/lib/changelog_web/templates/email/news_published.html.eex b/lib/changelog_web/templates/email/news_published.html.heex similarity index 71% rename from lib/changelog_web/templates/email/news_published.html.eex rename to lib/changelog_web/templates/email/news_published.html.heex index 1a7159307d..8eab32724a 100644 --- a/lib/changelog_web/templates/email/news_published.html.eex +++ b/lib/changelog_web/templates/email/news_published.html.heex @@ -8,32 +8,32 @@ - "> - + + - + - + - "> + - - - "> + + + - + PodcastView.apple_id(@episode.podcast)}> - "> - "> + +

<%= @episode.email_teaser %>

<%= if !@person do %>
- <%= form_tag Routes.person_path(Endpoint, :subscribe, "news"), method: "get" do %> + <%= form_tag ~p"/subscribe/news", method: "get" do %> You are viewing issue #<%= @episode.slug %> of the Changelog News(letter). Pop in your in your inbox every Monday. <% end %>
@@ -51,7 +51,7 @@ - "> +

<%= link("View on Web", to: url(~p"/#{@episode.podcast.slug}/#{@episode.slug}/email"), title: "View this newsletter in your web browser") %>    ·    <%= link("Audio Edition", to: url(~p"/#{@episode.podcast.slug}/#{@episode.slug}"), title: "View the companion audio edition") %> @@ -68,13 +68,18 @@

- <%= @episode.email_content |> SharedHelpers.md_to_html() |> HtmlKit.put_utm_source("changelog-news") |> raw() %> + <%= + @episode.email_content + |> SharedHelpers.md_to_html() + |> HtmlKit.put_utm_source("changelog-news") + |> HtmlKit.put_img_width("630") + |> raw() %>
- "> + @@ -84,7 +89,7 @@ diff --git a/test/changelog/kits/html_kit_test.exs b/test/changelog/kits/html_kit_test.exs index 2bafcc0a75..f84d8bcc7f 100644 --- a/test/changelog/kits/html_kit_test.exs +++ b/test/changelog/kits/html_kit_test.exs @@ -93,6 +93,24 @@ defmodule Changelog.HtmlKitTest do end end + describe "put_img_width/2" do + test "does nothing if there are no img tags" do + assert HtmlKit.put_img_width("", 500) == "" + end + + test "it adds width to a single img" do + a = ~s(

) + b = ~s(

) + assert HtmlKit.put_img_width(a, 500) == b + end + + test "it adds width to multiple img tags" do + a = ~s( () + b = ~s( () + assert HtmlKit.put_img_width(a, "250") == b + end + end + describe "put_utm_source/2" do test "it does nothing if there are no anchors" do assert HtmlKit.put_utm_source("", "changelog-news") == "" @@ -105,14 +123,22 @@ defmodule Changelog.HtmlKitTest do end test "it adds utm_source to multiple anchors" do - a = ~s(ohai thereobai there) - b = ~s(ohai thereobai there) + a = + ~s(ohai thereobai there) + + b = + ~s(ohai thereobai there) + assert HtmlKit.put_utm_source(a, "test") == b end test "it skips anchors that are have utm" do - a = ~s(ohai thereobai there) - b = ~s(ohai thereobai there) + a = + ~s(ohai thereobai there) + + b = + ~s(ohai thereobai there) + assert HtmlKit.put_utm_source(a, "test") == b end