2024-01-27 01:34:51 +08:00

20 lines
566 B
TypeScript

import { withBase } from 'vuepress/client'
import { PATHNAME_PROTOCOL_RE, isExternal } from './shared.js'
export function normalizeLink(url: string): string {
if (isExternal(url))
return url.replace(PATHNAME_PROTOCOL_RE, '')
const { pathname, search, hash } = new URL(url, 'http://example.com')
const normalizedPath
= pathname.endsWith('/') || pathname.endsWith('.html')
? url
: url.replace(
/(?:(^\.+)\/)?.*$/,
`$1${pathname.replace(/(\.md)?$/, '.html')}${search}${hash}`,
)
return withBase(normalizedPath)
}