--- import { z } from 'astro/zod' import { Icon } from 'astro-icon/components' import { networksBySlug } from '../constants/networks' import { cn } from '../lib/cn' import type { HTMLAttributes } from 'astro/types' type Props = Omit, 'href' | 'rel' | 'target'> & { url: string referral: string | null enableMinWidth?: boolean } const { url: baseUrl, referral, class: className, enableMinWidth = false, ...htmlProps } = Astro.props function makeLink(url: string, referral: string | null) { const hostname = new URL(url).hostname const urlWithReferral = url + (referral ?? '') const onionMatch = /^(?:https?:\/\/)?(.{0,10}).*?(.{0,10})(\.onion)$/.exec(hostname) if (onionMatch) { return { type: 'onion' as const, url: urlWithReferral, textBits: onionMatch.length ? [ { style: 'normal' as const, text: onionMatch[1] ?? '', }, { style: 'irrelevant' as const, text: '...', }, { style: 'normal' as const, text: onionMatch[2] ?? '', }, { style: 'irrelevant' as const, text: onionMatch[3] ?? '', }, ] : [ { style: 'normal' as const, text: hostname, }, ], icon: networksBySlug.onion.icon, } } const i2pMatch = /^(?:https?:\/\/)?(.{0,10}).*?(.{0,8})((?:\.b32)?\.i2p)$/.exec(hostname) if (i2pMatch) { return { type: 'i2p' as const, url: urlWithReferral, textBits: i2pMatch.length ? [ { style: 'normal' as const, text: i2pMatch[1] ?? '', }, { style: 'irrelevant' as const, text: '...', }, { style: 'normal' as const, text: i2pMatch[2] ?? '', }, { style: 'irrelevant' as const, text: i2pMatch[3] ?? '', }, ] : [ { style: 'normal' as const, text: hostname, }, ], icon: networksBySlug.i2p.icon, } } return { type: 'clearnet' as const, url: urlWithReferral, textBits: [ { style: 'normal' as const, text: hostname.replace(/^www\./, ''), }, ], icon: networksBySlug.clearnet.icon, } } const link = makeLink(baseUrl, referral) if (!z.string().url().safeParse(link.url).success) { console.error(`Invalid service URL with referral: ${link.url}`) } --- { link.textBits.map((textBit) => ( {textBit.text} )) }