Переработал обработчик иконок для гиперссылок
This commit is contained in:
parent
418892b3a4
commit
df54426a75
5 changed files with 143 additions and 104 deletions
65
src/lib/util/IconResolver.ts
Normal file
65
src/lib/util/IconResolver.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
const icons: Record<string, string> = {
|
||||
none: 'material-symbols:link',
|
||||
'steamcommunity.com': 'simple-icons:steam',
|
||||
'twitter.com': 'simple-icons:x',
|
||||
'x.com': 'simple-icons:x',
|
||||
'github.com': 'simple-icons:github',
|
||||
'youtube.com': 'simple-icons:youtube',
|
||||
'itch.io': 'simple-icons:itchdotio',
|
||||
'discord.gg': 'simple-icons:discord',
|
||||
'gamebanana.com': 'simple-icons:gamebanana',
|
||||
'bsky.app': 'simple-icons:bluesky',
|
||||
'bsky.social': 'simple-icons:bluesky',
|
||||
// https://хамяк.рф
|
||||
'xn--80auf8a2c.xn--p1ai': 'fluent-emoji-high-contrast:hamster',
|
||||
'teasanctuary.ru': '/icons/tea-sanctuary-white.svg',
|
||||
'hl.teasanctuary.ru': '/icons/half-life.svg',
|
||||
'git.teasanctuary.ru': 'devicon-plain:git',
|
||||
localhost: '/icons/tea-sanctuary-white.svg',
|
||||
email: 'material-symbols:alternate-email'
|
||||
};
|
||||
|
||||
// Особые случаи, когда одним доменом второго уровня не ограничишься (например, randomtrash.itch.io)
|
||||
const specialResolvers: Record<string, (url: URL) => string> = {
|
||||
'teasanctuary.ru': (url) => {
|
||||
// Домены третьего уровня и выше
|
||||
const prefix = url.hostname.split('.').toReversed();
|
||||
prefix.shift();
|
||||
prefix.shift();
|
||||
if (prefix[0] === "hl") {
|
||||
return 'hl.teasanctuary.ru';
|
||||
}
|
||||
if (prefix[0] === "git" || url.pathname.split('/')[1] == "git") {
|
||||
return 'git.teasanctuary.ru';
|
||||
}
|
||||
return 'teasanctuary.ru';
|
||||
},
|
||||
// Игнорируем имя пользователя
|
||||
'bsky.app': (url) => 'bsky.app',
|
||||
'bsky.social': (url) => 'bsky.social',
|
||||
'itch.io': (url) => 'itch.io'
|
||||
}
|
||||
|
||||
function getIconFromUrl(url: URL): string | undefined {
|
||||
const href = url.href;
|
||||
if (href.startsWith('mailto:'))
|
||||
return 'email';
|
||||
|
||||
const hostname = url.hostname;
|
||||
const secondLevel = hostname.match(/(([A-Za-z0-9\-])+\.([A-Za-z0-9\-])+)$/)?.at(0) ?? '';
|
||||
if (specialResolvers[secondLevel])
|
||||
return icons[specialResolvers[secondLevel](url)];
|
||||
|
||||
return icons[hostname];
|
||||
}
|
||||
|
||||
export function tryGetIcon(link: string): string {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(link);
|
||||
} catch {
|
||||
return icons['none'];
|
||||
}
|
||||
|
||||
return getIconFromUrl(url) ?? icons['none'];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue