Skip to content

Commit 396f1e9

Browse files
committed
Handle different behavior from mochihtml vs Lexbor when adding butto styles to Markdown campaigns
1 parent d8a2340 commit 396f1e9

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

lib/keila/templates/html.ex

+29-21
Original file line numberDiff line numberDiff line change
@@ -163,35 +163,19 @@ defmodule Keila.Templates.Html do
163163
Apply markup transforms for improved email client compatibility.
164164
165165
## Transforms:
166-
- `h4 a` -> `div.keila-button a`
166+
- `h4 a` -> `div.keila-button a` (this is implemented twice because mochihtml strips the leading "\n" while Lexbor retains it)
167167
"""
168168
@spec apply_email_markup(t()) :: t()
169169
def apply_email_markup(html) do
170170
Floki.traverse_and_update(html, &do_apply_email_markup/1)
171171
end
172172

173173
defp do_apply_email_markup({"h4", _, [{"a", a_attrs, a_children}]}) do
174-
a_attrs =
175-
case Enum.find_index(a_attrs, &(elem(&1, 0) == "class")) do
176-
nil ->
177-
[{"class", "button-a"} | a_attrs]
178-
179-
i ->
180-
List.update_at(a_attrs, i, fn {"class", classes} ->
181-
{"class", classes <> " button-a"}
182-
end)
183-
end
174+
build_button_markup(a_attrs, a_children)
175+
end
184176

185-
{"table", [{"style", "width: 100%"}],
186-
[
187-
{"tr", [{"class", "block--button"}],
188-
[
189-
{"td", [{"class", "button-td"}],
190-
[
191-
{"a", a_attrs, a_children}
192-
]}
193-
]}
194-
]}
177+
defp do_apply_email_markup({"h4", _, ["\n", {"a", a_attrs, a_children}]}) do
178+
build_button_markup(a_attrs, a_children)
195179
end
196180

197181
defp do_apply_email_markup({"blockquote", _, content}) do
@@ -248,4 +232,28 @@ defmodule Keila.Templates.Html do
248232
end
249233

250234
defp do_apply_email_markup(other), do: other
235+
236+
defp build_button_markup(a_attrs, a_children) do
237+
a_attrs =
238+
case Enum.find_index(a_attrs, &(elem(&1, 0) == "class")) do
239+
nil ->
240+
[{"class", "button-a"} | a_attrs]
241+
242+
i ->
243+
List.update_at(a_attrs, i, fn {"class", classes} ->
244+
{"class", classes <> " button-a"}
245+
end)
246+
end
247+
248+
{"table", [{"style", "width: 100%"}],
249+
[
250+
{"tr", [{"class", "block--button"}],
251+
[
252+
{"td", [{"class", "button-td"}],
253+
[
254+
{"a", a_attrs, a_children}
255+
]}
256+
]}
257+
]}
258+
end
251259
end

0 commit comments

Comments
 (0)