forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1export function sanitizeWebsiteForDisplay(website: string): string {
2 return website.replace(/^https?:\/\//i, '').replace(/\/$/, '')
3}
4
5export function sanitizeWebsiteForLink(website: string): string {
6 const normalized = website.toLowerCase()
7 return !normalized.startsWith('javascript')
8 ? normalized
9 : `unsafe-${website}`
10}
11
12export function isValidWebsiteFormat(website: string): boolean {
13 const trimmedWebsite = website?.trim() || ''
14
15 if (!trimmedWebsite || trimmedWebsite.length === 0) {
16 return true
17 }
18
19 const normalizedWebsite = trimmedWebsite.toLowerCase()
20
21 if ('https://'.startsWith(normalizedWebsite)) {
22 return true
23 }
24
25 if (!normalizedWebsite.match(/^https:\/\/.+/)) {
26 return false
27 }
28
29 const domainMatch = normalizedWebsite.match(/^https:\/\/([^/\s]+)/)
30 if (!domainMatch) {
31 return false
32 }
33
34 const domain = domainMatch[1]
35
36 // Check for valid domain structure:
37 // - Must contain at least one dot
38 // - Must have a valid TLD (at least 2 characters after the last dot)
39 // - Cannot be just a single word without extension
40 const domainPattern =
41 /^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/
42
43 return domainPattern.test(domain)
44}