@@ -459,11 +459,9 @@ defmodule PlausibleWeb.Email do
459
459
def base_email ( ) , do: base_email ( % { layout: "base_email.html" } )
460
460
461
461
def base_email ( % { layout: layout } ) do
462
- mailer_from = Application . get_env ( :plausible , :mailer_email )
463
-
464
462
new_email ( )
465
463
|> put_param ( "TrackOpens" , false )
466
- |> from ( mailer_from )
464
+ |> from ( mailer_email_from ( ) )
467
465
|> maybe_put_layout ( layout )
468
466
end
469
467
@@ -481,56 +479,60 @@ defmodule PlausibleWeb.Email do
481
479
end
482
480
483
481
defp textify ( html ) do
484
- html
485
- |> Floki . parse_fragment! ( )
482
+ Floki . parse_fragment! ( html )
486
483
|> traverse_and_textify ( )
487
484
|> Floki . text ( )
485
+ |> collapse_whitespace ( )
488
486
end
489
487
490
488
defp traverse_and_textify ( [ head | tail ] ) do
491
489
[ traverse_and_textify ( head ) | traverse_and_textify ( tail ) ]
492
490
end
493
491
494
492
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 " )
505
494
end
506
495
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 }
510
511
end
511
512
end
512
513
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
514
519
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
520
524
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
529
528
530
- { "p" , attrs , [ text_and_link ] }
529
+ defp collapse_whitespace ( << ?\n , ?\s , rest :: bytes >> ) do
530
+ collapse_whitespace ( << ?\n , rest :: bytes >> )
531
+ end
531
532
532
- _ ->
533
- node
534
- end
533
+ defp collapse_whitespace ( << c :: 1 - bytes , rest :: bytes >> ) do
534
+ c <> collapse_whitespace ( rest )
535
535
end
536
+
537
+ defp collapse_whitespace ( << >> = empty ) , do: empty
536
538
end
0 commit comments