Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Dec 18, 2024
1 parent 553ebd8 commit 306c3f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
74 changes: 38 additions & 36 deletions lib/plausible_web/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -481,56 +479,60 @@ 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
[traverse_and_textify(head) | traverse_and_textify(tail)]
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(<<?\s, ?\s, rest::bytes>>) do
collapse_whitespace(<<?\s, rest::bytes>>)
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(<<?\s, ?\n, rest::bytes>>) do
collapse_whitespace(<<?\n, rest::bytes>>)
end

{"p", attrs, [text_and_link]}
defp collapse_whitespace(<<?\n, ?\s, rest::bytes>>) do
collapse_whitespace(<<?\n, rest::bytes>>)
end

_ ->
node
end
defp collapse_whitespace(<<c::1-bytes, rest::bytes>>) do
c <> collapse_whitespace(rest)
end

defp collapse_whitespace(<<>> = empty), do: empty
end
19 changes: 9 additions & 10 deletions test/plausible_web/email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -351,17 +350,17 @@ 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\
http://localhost:4002
{{{ pm:unsubscribe }}}\
"""
end
end
Expand Down

0 comments on commit 306c3f5

Please sign in to comment.