diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index f47e12bdaef8..798108a17082 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -459,11 +459,9 @@ defmodule PlausibleWeb.Email do def base_email(), do: base_email(%{layout: "base_email.html"}) def base_email(%{layout: layout}) do - mailer_from = Application.get_env(:plausible, :mailer_email) - new_email() |> put_param("TrackOpens", false) - |> from(mailer_from) + |> from(mailer_email_from()) |> maybe_put_layout(layout) end @@ -481,10 +479,10 @@ defmodule PlausibleWeb.Email do end defp textify(html) do - html - |> Floki.parse_fragment!() + Floki.parse_fragment!(html) |> traverse_and_textify() |> Floki.text() + |> collapse_whitespace() end defp traverse_and_textify([head | tail]) do @@ -492,45 +490,49 @@ defmodule PlausibleWeb.Email do end defp traverse_and_textify(text) when is_binary(text) do - IO.inspect(text, label: "before") - trimmed = String.replace_leading(text, "\n", "") - trimmed = String.replace_trailing(trimmed, "\n", "\s") - IO.inspect(trimmed, label: "after") - - # if String.ends_with?(text, ["\n", "\s"]) do - # trimmed <> "\s" - # else - # trimmed - # end + String.replace(text, "\n", "\s") end - defp traverse_and_textify(node) when is_tuple(node) do - with {elem, attrs, children} <- maybe_textify_link(node) do - {elem, attrs, traverse_and_textify(children)} + defp traverse_and_textify({"a" = tag, attrs, children}) do + href = with {"href", href} <- List.keyfind(attrs, "href", 0), do: href + children = traverse_and_textify(children) + + if href do + text = Floki.text(children) + + if text == href do + # avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer + text + else + IO.iodata_to_binary([text, " (", href, ?)]) + end + else + {tag, attrs, children} end end - defp traverse_and_textify([] = empty), do: empty + defp traverse_and_textify({tag, attrs, children}) do + {tag, attrs, traverse_and_textify(children)} + end + + defp traverse_and_textify(other), do: other - defp maybe_textify_link(node) do - case node do - {"a", attrs, children} -> - {"href", href} = List.keyfind!(attrs, "href", 0) - text = Floki.text(children) + # this is slow but easy to understand + defp collapse_whitespace(<>) do + collapse_whitespace(<>) + end - text_and_link = - if text == href do - # avoids rendering "http://localhost:8000 (http://localhost:8000)" - # e.g. in base_email footer - text - else - IO.iodata_to_binary([text, " (", href, ?)]) - end + defp collapse_whitespace(<>) do + collapse_whitespace(<>) + end - {"p", attrs, [text_and_link]} + defp collapse_whitespace(<>) do + collapse_whitespace(<>) + end - _ -> - node - end + defp collapse_whitespace(<>) do + c <> collapse_whitespace(rest) end + + defp collapse_whitespace(<<>> = empty), do: empty end diff --git a/test/plausible_web/email_test.exs b/test/plausible_web/email_test.exs index 1b6a94252241..abb24bd865db 100644 --- a/test/plausible_web/email_test.exs +++ b/test/plausible_web/email_test.exs @@ -336,12 +336,11 @@ defmodule PlausibleWeb.EmailTest do }) assert email.text_body == """ - Hey John,\s + Hey John, - We are building Plausible to provide a simple and ethical approach to tracking website visitors. - We're super excited to have you on board!\s + We are building Plausible to provide a simple and ethical approach to tracking website visitors. We're super excited to have you on board! - Here's how to get the most out of your Plausible experience:\s + Here's how to get the most out of your Plausible experience: * Enable email reports (https://plausible.io/docs/email-reports) and notifications for traffic spikes (https://plausible.io/docs/traffic-spikes) * Integrate with Search Console (https://plausible.io/docs/google-search-console-integration) to get keyword phrases people find your site with @@ -351,14 +350,14 @@ defmodule PlausibleWeb.EmailTest do * If you're concerned about adblockers, set up a proxy to bypass them (https://plausible.io/docs/proxy/introduction) - Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).\s + Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites). - Have a question, feedback or need some guidance? Do reply back to this email.\s + Have a question, feedback or need some guidance? Do reply back to this email. Regards, - The Plausible Team 💌\s + The Plausible Team 💌 - --\s + -- http://localhost:8000 {{{ pm:unsubscribe }}}\s\