docs: fix base path not added to hash links (#10235)
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import clsx from "clsx"
|
import clsx from "clsx"
|
||||||
import React, { useMemo } from "react"
|
import React from "react"
|
||||||
import { CopyButton, Link } from "@/components"
|
import { CopyButton, Link } from "@/components"
|
||||||
import { useIsBrowser } from "../../../providers"
|
import { useHeadingUrl } from "../../.."
|
||||||
import { usePathname } from "next/navigation"
|
|
||||||
|
|
||||||
type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
||||||
id?: string
|
id?: string
|
||||||
@@ -12,16 +11,9 @@ type H2Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const H2 = ({ className, children, passRef, ...props }: H2Props) => {
|
export const H2 = ({ className, children, passRef, ...props }: H2Props) => {
|
||||||
const { isBrowser } = useIsBrowser()
|
const copyText = useHeadingUrl({
|
||||||
const pathname = usePathname()
|
id: props.id || "",
|
||||||
const copyText = useMemo(() => {
|
})
|
||||||
const hash = `#${props.id}`
|
|
||||||
if (!isBrowser) {
|
|
||||||
return hash
|
|
||||||
}
|
|
||||||
|
|
||||||
return `${window.location.origin}${pathname}${hash}`
|
|
||||||
}, [props.id, isBrowser, pathname])
|
|
||||||
return (
|
return (
|
||||||
<h2
|
<h2
|
||||||
className={clsx(
|
className={clsx(
|
||||||
|
|||||||
@@ -1,30 +1,16 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import clsx from "clsx"
|
import clsx from "clsx"
|
||||||
import React, { useMemo } from "react"
|
import React from "react"
|
||||||
import { CopyButton, Link } from "@/components"
|
import { CopyButton, Link } from "@/components"
|
||||||
import { useIsBrowser } from "../../../providers"
|
import { useHeadingUrl } from "../../.."
|
||||||
|
|
||||||
type H3Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
type H3Props = React.HTMLAttributes<HTMLHeadingElement> & {
|
||||||
id?: string
|
id?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const H3 = ({ className, children, ...props }: H3Props) => {
|
export const H3 = ({ className, children, ...props }: H3Props) => {
|
||||||
const { isBrowser } = useIsBrowser()
|
const copyText = useHeadingUrl({ id: props.id || "" })
|
||||||
const copyText = useMemo(() => {
|
|
||||||
const url = `#${props.id}`
|
|
||||||
if (!isBrowser) {
|
|
||||||
return url
|
|
||||||
}
|
|
||||||
|
|
||||||
const hashIndex = window.location.href.indexOf("#")
|
|
||||||
return (
|
|
||||||
window.location.href.substring(
|
|
||||||
0,
|
|
||||||
hashIndex !== -1 ? hashIndex : window.location.href.length
|
|
||||||
) + url
|
|
||||||
)
|
|
||||||
}, [props.id, isBrowser])
|
|
||||||
return (
|
return (
|
||||||
<h3
|
<h3
|
||||||
className={clsx(
|
className={clsx(
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ export * from "./use-click-outside"
|
|||||||
export * from "./use-collapsible"
|
export * from "./use-collapsible"
|
||||||
export * from "./use-collapsible-code-lines"
|
export * from "./use-collapsible-code-lines"
|
||||||
export * from "./use-copy"
|
export * from "./use-copy"
|
||||||
|
export * from "./use-heading-url"
|
||||||
export * from "./use-current-learning-path"
|
export * from "./use-current-learning-path"
|
||||||
export * from "./use-is-external-link"
|
export * from "./use-is-external-link"
|
||||||
export * from "./use-keyboard-shortcut"
|
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