import Link from 'next/link'
import { siteConfig } from '@/lib/config'
// 过滤 标签不能识别的 props
const filterDOMProps = props => {
const { passHref, legacyBehavior, ...rest } = props
return rest
}
const SmartLink = ({ href, children, ...rest }) => {
const LINK = siteConfig('LINK')
// 获取 URL 字符串用于判断是否是外链
let urlString = ''
if (typeof href === 'string') {
urlString = href
} else if (
typeof href === 'object' &&
href !== null &&
typeof href.pathname === 'string'
) {
urlString = href.pathname
}
const isExternal = urlString.startsWith('http') && !urlString.startsWith(LINK)
if (isExternal) {
// 对于外部链接,必须是 string 类型
const externalUrl =
typeof href === 'string' ? href : new URL(href.pathname, LINK).toString()
return (
{children}
)
}
// 内部链接(可为对象形式)
return (
{children}
)
}
export default SmartLink