Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions apps/web/lib/url-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const isValidUrl = (url: string): boolean => {
*/
export const normalizeUrl = (url: string): string => {
if (!url.trim()) return ""
if (url.startsWith("http://") || url.startsWith("https://")) {
if (/^https?:\/\//i.test(url)) {
return url
}
return `https://${url}`
Expand Down Expand Up @@ -120,25 +120,52 @@ export const extractUrls = (
return { urls, duplicates }
}

const parseWebUrl = (url: string): URL | null => {
const trimmed = url.trim()
if (!trimmed) return null

try {
const parsed = new URL(trimmed)
return parsed.protocol === "http:" || parsed.protocol === "https:"
? parsed
: null
} catch {
try {
return new URL(`https://${trimmed}`)
} catch {
return null
}
}
}

const hostnameMatches = (hostname: string, domain: string): boolean => {
const normalizedHostname = hostname.toLowerCase()
return (
normalizedHostname === domain || normalizedHostname.endsWith(`.${domain}`)
)
}

/**
* Checks if a URL is a Twitter/X URL.
*/
export const isTwitterUrl = (url: string): boolean => {
const normalizedUrl = url.toLowerCase()
const parsed = parseWebUrl(url)
Comment thread
anirudh5harma marked this conversation as resolved.
if (!parsed) return false
return (
normalizedUrl.includes("twitter.com") || normalizedUrl.includes("x.com")
hostnameMatches(parsed.hostname, "twitter.com") ||
hostnameMatches(parsed.hostname, "x.com")
)
}

/**
* Checks if a URL is a LinkedIn profile URL (not a company page).
*/
export const isLinkedInProfileUrl = (url: string): boolean => {
const normalizedUrl = url.toLowerCase()
return (
normalizedUrl.includes("linkedin.com/in/") &&
!normalizedUrl.includes("linkedin.com/company/")
)
const parsed = parseWebUrl(url)
if (!parsed || !hostnameMatches(parsed.hostname, "linkedin.com")) return false

const [section, handle] = parsed.pathname.split("/").filter(Boolean)
return section?.toLowerCase() === "in" && Boolean(handle)
}

/**
Expand Down