Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/site/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const inlineCode = "relative rounded-md bg-muted px-[0.3rem] py-[0.2rem] font-mo
const linkClass = "font-medium underline underline-offset-4"

function Prose({ children }: { children: string }) {
const { search } = useLocation()
return (
<Markdown
remarkPlugins={[remarkGfm]}
Expand All @@ -93,7 +94,7 @@ function Prose({ children }: { children: string }) {
a: ({ href = "", children: label }) => {
const target = resolveHref(href)
return target.to ? (
<Link to={target.to} className={linkClass}>
<Link to={target.to.startsWith("#") ? { search, hash: target.to } : target.to} className={linkClass}>
{label}
</Link>
) : (
Expand Down Expand Up @@ -358,14 +359,21 @@ function DocShell({ title, description, url, toc, children }: { title: string; d

export function DocPage({ section }: { section: "docs" | "components" }) {
const { slug = "introduction" } = useParams()
const { hash } = useLocation()
const { hash, key } = useLocation()
const page = pages.find((entry) => entry.slug === slug && (section === "docs" ? entry.section === "docs" : entry.section !== "docs"))
const manager = React.useState<Manager>("pnpm")

// A link to a heading lands on it once the page is there.
React.useEffect(() => {
if (hash) document.getElementById(decodeURIComponent(hash.slice(1)))?.scrollIntoView()
}, [hash, page])
if (!hash) return
let id = hash.slice(1)
try {
id = decodeURIComponent(id)
} catch {
// A malformed escape can still be a literal heading ID.
}
document.getElementById(id)?.scrollIntoView()
}, [hash, key, page])

if (!page) return <Navigate to="/docs/components" replace />
return (
Expand Down
3 changes: 2 additions & 1 deletion src/site/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const ordered: { title: string; url: string }[] = [
* other page of theirs stays theirs.
*/
export function resolveHref(href: string): { to?: string; href?: string } {
if (/^([a-z]+:|#)/i.test(href)) return { href }
if (href.startsWith("#")) return { to: href }
if (/^[a-z]+:/i.test(href)) return { href }
const [path, hash = ""] = href.split("#")
const component = path.match(/^\/docs\/components\/(?:base\/|radix\/)?([a-z0-9-]+)\/?$/)?.[1]
const tail = hash ? `#${hash}` : ""
Expand Down
Loading