diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex
index 456416fbd..395a0ac55 100644
--- a/lib/pleroma/formatter.ex
+++ b/lib/pleroma/formatter.ex
@@ -160,6 +160,7 @@ def add_links({subs, text}) do
links =
Regex.scan(@link_regex, text)
|> Enum.map(fn [url] -> {Ecto.UUID.generate(), url} end)
+ |> Enum.sort_by(fn ({_, url}) -> -String.length(url) end)
uuid_text =
links
diff --git a/test/formatter_test.exs b/test/formatter_test.exs
index 2cf1f3f8e..e89b36663 100644
--- a/test/formatter_test.exs
+++ b/test/formatter_test.exs
@@ -78,6 +78,13 @@ test "turning urls into links" do
"https://en.wikipedia.org/wiki/Duff's_device"
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
+
+ text = "https://pleroma.com https://pleroma.com/sucks"
+
+ expected =
+ "https://pleroma.com https://pleroma.com/sucks"
+
+ assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
end
end