Skip to content

Commit c4e0f9d

Browse files
authored
anchors (#1175)
1 parent b75fba1 commit c4e0f9d

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

docs/mdx-components.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,32 @@ function extractText(children: React.ReactNode): string {
2727
export function useMDXComponents(components: MDXComponents): MDXComponents {
2828
return {
2929
...components,
30+
h1: ({ children }: { children?: React.ReactNode }) => {
31+
const id = slugify(extractText(children));
32+
return (
33+
<h1 id={id} className="heading-anchor">
34+
{children}
35+
<a href={`#${id}`} aria-label="Link to this section">#</a>
36+
</h1>
37+
);
38+
},
3039
h2: ({ children }: { children?: React.ReactNode }) => {
3140
const id = slugify(extractText(children));
32-
return <h2 id={id}>{children}</h2>;
41+
return (
42+
<h2 id={id} className="heading-anchor">
43+
{children}
44+
<a href={`#${id}`} aria-label="Link to this section">#</a>
45+
</h2>
46+
);
3347
},
3448
h3: ({ children }: { children?: React.ReactNode }) => {
3549
const id = slugify(extractText(children));
36-
return <h3 id={id}>{children}</h3>;
50+
return (
51+
<h3 id={id} className="heading-anchor">
52+
{children}
53+
<a href={`#${id}`} aria-label="Link to this section">#</a>
54+
</h3>
55+
);
3756
},
3857
a: ({
3958
href,

docs/src/app/globals.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ pre:not(.shiki) {
201201
color: var(--foreground);
202202
}
203203

204+
.prose .heading-anchor {
205+
position: relative;
206+
}
207+
208+
.prose .heading-anchor > a {
209+
opacity: 0;
210+
margin-left: 0.375rem;
211+
color: var(--muted-foreground);
212+
text-decoration: none;
213+
font-weight: 400;
214+
transition: opacity 0.15s ease;
215+
}
216+
217+
.prose .heading-anchor:hover > a {
218+
opacity: 1;
219+
}
220+
221+
.prose .heading-anchor > a:hover {
222+
color: var(--foreground);
223+
}
224+
204225
.prose p {
205226
margin-bottom: 1rem;
206227
line-height: 1.65;

0 commit comments

Comments
 (0)