docs: fix base path not added to hash links (#10235)
This commit is contained in:
@@ -3,6 +3,7 @@ export * from "./use-click-outside"
|
||||
export * from "./use-collapsible"
|
||||
export * from "./use-collapsible-code-lines"
|
||||
export * from "./use-copy"
|
||||
export * from "./use-heading-url"
|
||||
export * from "./use-current-learning-path"
|
||||
export * from "./use-is-external-link"
|
||||
export * from "./use-keyboard-shortcut"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
"use client"
|
||||
|
||||
import { usePathname } from "next/navigation"
|
||||
import { useIsBrowser, useSiteConfig } from "../../providers"
|
||||
import { useMemo } from "react"
|
||||
|
||||
type useHeadingUrlProps = {
|
||||
id: string
|
||||
}
|
||||
|
||||
export const useHeadingUrl = ({ id }: useHeadingUrlProps) => {
|
||||
const { isBrowser } = useIsBrowser()
|
||||
const {
|
||||
config: { basePath },
|
||||
} = useSiteConfig()
|
||||
const pathname = usePathname()
|
||||
const headingUrl = useMemo(() => {
|
||||
const hash = `#${id}`
|
||||
if (!isBrowser) {
|
||||
return hash
|
||||
}
|
||||
|
||||
const url = `${window.location.origin}${basePath}${pathname}`.replace(
|
||||
/\/$/,
|
||||
""
|
||||
)
|
||||
|
||||
return `${url}${hash}`
|
||||
}, [id, isBrowser, pathname])
|
||||
|
||||
return headingUrl
|
||||
}
|
||||
Reference in New Issue
Block a user