[Posts] Show an icon for non-matches urls

This commit is contained in:
Earlopain 2024-02-27 15:48:52 +01:00
parent dbd5f8399b
commit 2d7e507555
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
5 changed files with 41 additions and 19 deletions

View File

@ -1,7 +1,6 @@
# frozen_string_literal: true
module LinkHelper
NONE = "empty"
DECORATABLE_DOMAINS = [
"e621.net",
#
@ -135,22 +134,29 @@ module LinkHelper
def favicon_for_link(path)
hostname = hostname_for_link(path)
tag.img(
class: "link-decoration",
src: asset_pack_path("static/#{hostname}.png"),
data: {
hostname: hostname,
},
)
if hostname
tag.img(
class: "link-decoration",
src: asset_pack_path("static/#{hostname}.png"),
data: {
hostname: hostname,
},
)
else
tag.i(
class: "fa-solid fa-globe link-decoration",
data: { hostname: "none" },
)
end
end
def hostname_for_link(path)
begin
uri = URI.parse(path)
rescue URI::InvalidURIError
return NONE
return nil
end
return NONE unless uri.host
return nil unless uri.host
hostname = uri.host.delete_prefix("www.")
@ -164,9 +170,7 @@ module LinkHelper
if hostname.count(".") > 1
_removed, remaining_hostname = hostname.split(".", 2)
return remaining_hostname if DECORATABLE_DOMAINS.include?(remaining_hostname)
return DECORATABLE_ALIASES[remaining_hostname] if DECORATABLE_ALIASES[remaining_hostname]
DECORATABLE_ALIASES[remaining_hostname]
end
NONE
end
end

View File

@ -1,5 +1,5 @@
img.link-decoration {
vertical-align: sub;
.link-decoration {
vertical-align: middle;
min-height: 16px;
height: 1em;
margin-right: 4px;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 B

View File

@ -4,15 +4,15 @@ require "test_helper"
class LinkHelperTest < ActionView::TestCase
test "for a non-handled url" do
assert_equal(LinkHelper::NONE, hostname_for_link("https://example.com"))
assert_nil(hostname_for_link("https://example.com"))
end
test "for a invalid url" do
assert_equal(LinkHelper::NONE, hostname_for_link("https:example.com"))
assert_nil(hostname_for_link("https:example.com"))
end
test "for a non-url" do
assert_equal(LinkHelper::NONE, hostname_for_link("text"))
assert_nil(hostname_for_link("text"))
end
test "for a normal domain" do
@ -31,9 +31,18 @@ class LinkHelperTest < ActionView::TestCase
assert_equal("inkbunny.net", hostname_for_link("https://qb.ib.metapix.net"))
end
test "it returns an image if a hostname is found" do
assert_match("furaffinity.net.png", favicon_for_link("https://furaffinity.net"))
end
test "it returns a fontawesome icon if no hostname is found" do
pp favicon_for_link("https://example.com")
assert_match("globe", favicon_for_link("https://example.com"))
end
test "all listed images exist" do
favicon_folder = Rails.public_path.join("images/favicons")
all_domains = LinkHelper::DECORATABLE_DOMAINS + LinkHelper::DECORATABLE_ALIASES.values + [LinkHelper::NONE]
all_domains = LinkHelper::DECORATABLE_DOMAINS + LinkHelper::DECORATABLE_ALIASES.values
all_domains.each do |domain|
assert(favicon_folder.join("#{domain}.png").exist?, "missing #{domain}")
end

View File

@ -116,4 +116,13 @@ class ActionDispatch::IntegrationTest
end
end
module ActionView
class TestCase
# Stub webpacker method so these tests don't compile assets
def asset_pack_path(name, **_options)
name
end
end
end
Rails.application.load_seed