Skip to content

Commit 221fa13

Browse files
authored
[Docs Site] Support custom IDs in AnchorHeading (#16980)
* [Docs Site] Support custom IDs in AnchorHeading * Rename id to slug
1 parent 060e209 commit 221fa13

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/components/AnchorHeading.astro

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
---
22
import { z } from "astro:schema";
33
import { marked } from "marked";
4-
import { slug } from "github-slugger";
4+
import { slug as GithubSlug } from "github-slugger";
55
66
type Props = z.infer<typeof props>;
77
88
const props = z.object({
99
title: z.string(),
10+
slug: z.string().optional(),
1011
depth: z.number().min(1).max(6),
1112
});
1213
13-
const { title, depth } = props.parse(Astro.props);
14+
const { title, slug, depth } = props.parse(Astro.props);
15+
16+
const slugified = GithubSlug(slug ?? title);
1417
1518
const Heading = `h${depth}` as "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
1619
---
1720

1821
<div tabindex="-1" class=`heading-wrapper level-h${depth}`>
19-
<Heading id={slug(title)} set:html={marked.parseInline(title) as string} />
20-
<a class="anchor-link" href={`#${slug(title)}`}>
22+
<Heading id={slugified} set:html={marked.parseInline(title) as string} />
23+
<a class="anchor-link" href={`#${slugified}`}>
2124
<span aria-hidden class="anchor-icon">
2225
<svg width="16" height="16" viewBox="0 0 24 24">
2326
<path

0 commit comments

Comments
 (0)