feat: simplify external link display
This commit is contained in:
parent
f23493742b
commit
21663711f8
5 changed files with 36 additions and 24 deletions
|
|
@ -1,6 +1,9 @@
|
|||
import { cn } from '@/lib/utils'
|
||||
import { useMemo } from 'react'
|
||||
|
||||
export default function ExternalLink({ url, className }: { url: string; className?: string }) {
|
||||
const displayUrl = useMemo(() => getDisplayUrl(url), [url])
|
||||
|
||||
return (
|
||||
<a
|
||||
className={cn('text-primary hover:underline', className)}
|
||||
|
|
@ -8,8 +11,35 @@ export default function ExternalLink({ url, className }: { url: string; classNam
|
|||
target="_blank"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
rel="noreferrer"
|
||||
title={url}
|
||||
>
|
||||
{url}
|
||||
{displayUrl}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
const getDisplayUrl = (url: string, maxLength: number = 30) => {
|
||||
try {
|
||||
const urlObj = new URL(url)
|
||||
let domain = urlObj.hostname
|
||||
const path = urlObj.pathname
|
||||
|
||||
if (domain.startsWith('www.')) {
|
||||
domain = domain.slice(4)
|
||||
}
|
||||
|
||||
if (!path || path === '/') {
|
||||
return domain
|
||||
}
|
||||
|
||||
const displayUrl = domain + path
|
||||
|
||||
if (displayUrl.length > maxLength) {
|
||||
return domain + path.slice(0, maxLength - domain.length - 3) + '...'
|
||||
}
|
||||
|
||||
return displayUrl
|
||||
} catch {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue