almost solved basepath problem
parent
1aa20d896e
commit
13194630ac
@ -0,0 +1,34 @@
|
|||||||
|
---
|
||||||
|
import type { JSX } from 'astro/jsx-runtime'
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
href: string
|
||||||
|
} & JSX.AnchorHTMLAttributes
|
||||||
|
|
||||||
|
const { href, ...rest } = Astro.props
|
||||||
|
|
||||||
|
const basepath = Astro.site?.pathname || ''
|
||||||
|
|
||||||
|
// assert link starts with http, ./ or /
|
||||||
|
if (!href.startsWith('://') && !href.startsWith('./') && !href.startsWith('/')) {
|
||||||
|
throw new Error(`Invalid href: ${href}. It must be an external link or start with './', or '/'`)
|
||||||
|
}
|
||||||
|
---
|
||||||
|
|
||||||
|
{
|
||||||
|
href.includes('://') || href.startsWith('./') ? (
|
||||||
|
<>
|
||||||
|
<Fragment set:html={`<!-- External or relative link -->`} />
|
||||||
|
<a href={href} {...rest}>
|
||||||
|
<slot />
|
||||||
|
</a>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Fragment set:html={`<!-- Internal link, Basepath: "${basepath}/" -->`} />
|
||||||
|
<a href={basepath + (href.startsWith(basepath) ? href.slice(basepath.length) : href)} {...rest}>
|
||||||
|
<slot />
|
||||||
|
</a>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue