Skip to content
Merged

anchors #1175

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
23 changes: 21 additions & 2 deletions docs/mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,32 @@ function extractText(children: React.ReactNode): string {
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
h1: ({ children }: { children?: React.ReactNode }) => {
const id = slugify(extractText(children));
return (
<h1 id={id} className="heading-anchor">
{children}
<a href={`#${id}`} aria-label="Link to this section">#</a>
</h1>
);
},
h2: ({ children }: { children?: React.ReactNode }) => {
const id = slugify(extractText(children));
return <h2 id={id}>{children}</h2>;
return (
<h2 id={id} className="heading-anchor">
{children}
<a href={`#${id}`} aria-label="Link to this section">#</a>
</h2>
);
},
h3: ({ children }: { children?: React.ReactNode }) => {
const id = slugify(extractText(children));
return <h3 id={id}>{children}</h3>;
return (
<h3 id={id} className="heading-anchor">
{children}
<a href={`#${id}`} aria-label="Link to this section">#</a>
</h3>
);
},
a: ({
href,
Expand Down
21 changes: 21 additions & 0 deletions docs/src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ pre:not(.shiki) {
color: var(--foreground);
}

.prose .heading-anchor {
position: relative;
}

.prose .heading-anchor > a {
opacity: 0;
margin-left: 0.375rem;
color: var(--muted-foreground);
text-decoration: none;
font-weight: 400;
transition: opacity 0.15s ease;
}

.prose .heading-anchor:hover > a {
opacity: 1;
}

.prose .heading-anchor > a:hover {
color: var(--foreground);
}

.prose p {
margin-bottom: 1rem;
line-height: 1.65;
Expand Down
Loading