Skip to content

Commit 5087dcb

Browse files
committed
fix: normalize website URLs from GitHub profile
1 parent 7734db8 commit 5087dcb

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/algora/accounts/schemas/user.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ defmodule Algora.Accounts.User do
327327
bio: meta["bio"],
328328
location: meta["location"],
329329
avatar_url: meta["avatar_url"],
330-
website_url: meta["blog"],
330+
website_url: Algora.Util.normalize_url(meta["blog"]),
331331
github_url: meta["html_url"]
332332
}
333333

lib/algora/shared/util.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ defmodule Algora.Util do
168168
|> String.replace(~r/\/(issues|pull|discussions)\//, "#")
169169
end
170170

171+
def normalize_url(nil), do: nil
172+
173+
def normalize_url(url) when is_binary(url) do
174+
url = String.trim(url)
175+
176+
# Add https:// if no scheme present
177+
url = if String.contains?(url, "://"), do: url, else: "https://" <> url
178+
179+
case URI.parse(url) do
180+
%URI{scheme: scheme, host: host} when not is_nil(scheme) and scheme != "" and not is_nil(host) and host != "" -> url
181+
_ -> nil
182+
end
183+
end
184+
171185
def get_gravatar_url(email, opts \\ []) do
172186
default = Keyword.get(opts, :default, "")
173187
size = Keyword.get(opts, :size, 460)

0 commit comments

Comments
 (0)