Skip to content

Commit 1f7ce52

Browse files
committed
cleanup
1 parent 553ebd8 commit 1f7ce52

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

lib/plausible_web/email.ex

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,9 @@ defmodule PlausibleWeb.Email do
459459
def base_email(), do: base_email(%{layout: "base_email.html"})
460460

461461
def base_email(%{layout: layout}) do
462-
mailer_from = Application.get_env(:plausible, :mailer_email)
463-
464462
new_email()
465463
|> put_param("TrackOpens", false)
466-
|> from(mailer_from)
464+
|> from(mailer_email_from())
467465
|> maybe_put_layout(layout)
468466
end
469467

@@ -481,56 +479,60 @@ defmodule PlausibleWeb.Email do
481479
end
482480

483481
defp textify(html) do
484-
html
485-
|> Floki.parse_fragment!()
482+
Floki.parse_fragment!(html)
486483
|> traverse_and_textify()
487484
|> Floki.text()
485+
|> collapse_whitespace()
488486
end
489487

490488
defp traverse_and_textify([head | tail]) do
491489
[traverse_and_textify(head) | traverse_and_textify(tail)]
492490
end
493491

494492
defp traverse_and_textify(text) when is_binary(text) do
495-
IO.inspect(text, label: "before")
496-
trimmed = String.replace_leading(text, "\n", "")
497-
trimmed = String.replace_trailing(trimmed, "\n", "\s")
498-
IO.inspect(trimmed, label: "after")
499-
500-
# if String.ends_with?(text, ["\n", "\s"]) do
501-
# trimmed <> "\s"
502-
# else
503-
# trimmed
504-
# end
493+
String.replace(text, "\n", "\s")
505494
end
506495

507-
defp traverse_and_textify(node) when is_tuple(node) do
508-
with {elem, attrs, children} <- maybe_textify_link(node) do
509-
{elem, attrs, traverse_and_textify(children)}
496+
defp traverse_and_textify({"a" = tag, attrs, children}) do
497+
href = with {"href", href} <- List.keyfind(attrs, "href", 0), do: href
498+
children = traverse_and_textify(children)
499+
500+
if href do
501+
text = Floki.text(children)
502+
503+
if text == href do
504+
# avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer
505+
text
506+
else
507+
IO.iodata_to_binary([text, " (", href, ?)])
508+
end
509+
else
510+
{tag, attrs, children}
510511
end
511512
end
512513

513-
defp traverse_and_textify([] = empty), do: empty
514+
defp traverse_and_textify({tag, attrs, children}) do
515+
{tag, attrs, traverse_and_textify(children)}
516+
end
517+
518+
defp traverse_and_textify(other), do: other
514519

515-
defp maybe_textify_link(node) do
516-
case node do
517-
{"a", attrs, children} ->
518-
{"href", href} = List.keyfind!(attrs, "href", 0)
519-
text = Floki.text(children)
520+
# this is slow but easy to understand
521+
defp collapse_whitespace(<<?\s, ?\s, rest::bytes>>) do
522+
collapse_whitespace(<<?\s, rest::bytes>>)
523+
end
520524

521-
text_and_link =
522-
if text == href do
523-
# avoids rendering "http://localhost:8000 (http://localhost:8000)"
524-
# e.g. in base_email footer
525-
text
526-
else
527-
IO.iodata_to_binary([text, " (", href, ?)])
528-
end
525+
defp collapse_whitespace(<<?\s, ?\n, rest::bytes>>) do
526+
collapse_whitespace(<<?\n, rest::bytes>>)
527+
end
529528

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

532-
_ ->
533-
node
534-
end
533+
defp collapse_whitespace(<<c::1-bytes, rest::bytes>>) do
534+
c <> collapse_whitespace(rest)
535535
end
536+
537+
defp collapse_whitespace(<<>> = empty), do: empty
536538
end

test/plausible_web/email_test.exs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,11 @@ defmodule PlausibleWeb.EmailTest do
336336
})
337337

338338
assert email.text_body == """
339-
Hey John,\s
339+
Hey John,
340340
341-
We are building Plausible to provide a simple and ethical approach to tracking website visitors.
342-
We're super excited to have you on board!\s
341+
We are building Plausible to provide a simple and ethical approach to tracking website visitors. We're super excited to have you on board!
343342
344-
Here's how to get the most out of your Plausible experience:\s
343+
Here's how to get the most out of your Plausible experience:
345344
346345
* Enable email reports (https://plausible.io/docs/email-reports) and notifications for traffic spikes (https://plausible.io/docs/traffic-spikes)
347346
* 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
351350
* If you're concerned about adblockers, set up a proxy to bypass them (https://plausible.io/docs/proxy/introduction)
352351
353352
354-
Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).\s
353+
Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).
355354
356-
Have a question, feedback or need some guidance? Do reply back to this email.\s
355+
Have a question, feedback or need some guidance? Do reply back to this email.
357356
358357
Regards,
359-
The Plausible Team 💌\s
358+
The Plausible Team 💌
360359
361-
--\s
360+
--
362361
363362
http://localhost:8000
364363
{{{ pm:unsubscribe }}}\s\

0 commit comments

Comments
 (0)