Ссылки по самому сайту теперь не открывают новую вкладку

This commit is contained in:
Иван Кузьменко 2025-07-12 11:35:34 +03:00
parent e6cc0ef138
commit 778219127c
3 changed files with 30 additions and 12 deletions

View file

@ -1,65 +0,0 @@
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'];
}