From bab66c80795b9f604e052136db2a9516a5a0d324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Diedrich?= Date: Mon, 12 Aug 2024 16:17:00 +0200 Subject: [PATCH] fix attachment links --- lib/something_erlang/awful_api/thread.ex | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/something_erlang/awful_api/thread.ex b/lib/something_erlang/awful_api/thread.ex index 93ff4d1..6f45439 100644 --- a/lib/something_erlang/awful_api/thread.ex +++ b/lib/something_erlang/awful_api/thread.ex @@ -111,7 +111,11 @@ defmodule SomethingErlang.AwfulApi.Thread do if class == "sa-smilie" do {"img", attrs, []} else - t_attrs = List.keyreplace(attrs, "class", 0, {"class", "img-responsive"}) + t_attrs = + attrs + |> fix_attachment_links() + |> List.keyreplace("class", 0, {"class", "img-responsive"}) + {"img", [{"loading", "lazy"} | t_attrs], []} end end @@ -194,4 +198,15 @@ defmodule SomethingErlang.AwfulApi.Thread do {"src", src} ], []} ]} + + defp fix_attachment_links(attrs) do + {"src", src} = List.keyfind(attrs, "src", 0) + + if String.starts_with?(src, "attachment.php") do + List.keyreplace(attrs, "src", 0, {"src", "https://forums.somethingawful.com/" <> src}) + |> List.keydelete("alt", 0) + else + attrs + end + end end