Skip to content

Commit ba2881b

Browse files
authored
[Docs Site] Support custom heading IDs with MDX comments (#17551)
* [Docs Site] Support custom heading IDs with MDX comments * Amend AnchorHeading usage guidance * include rehype-slug behaviour, move to individual file
1 parent 0d8a917 commit ba2881b

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

astro.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import tailwind from "@astrojs/tailwind";
44
import starlightDocSearch from "@astrojs/starlight-docsearch";
55
import starlightImageZoom from "starlight-image-zoom";
66
import liveCode from "astro-live-code";
7-
import rehypeSlug from "rehype-slug";
87
import rehypeMermaid from "rehype-mermaid";
98
import rehypeAutolinkHeadings, {
109
type Options as rehypeAutolinkHeadingsOptions,
@@ -17,6 +16,7 @@ import icon from "astro-icon";
1716
import sitemap from "@astrojs/sitemap";
1817
import react from "@astrojs/react";
1918
import rehypeTitleFigure from "rehype-title-figure";
19+
import rehypeHeadingSlugs from "./plugins/rehype/heading-slugs";
2020

2121
const runLinkCheck = process.env.RUN_LINK_CHECK || false;
2222

@@ -95,7 +95,7 @@ export default defineConfig({
9595
rel: ["noopener"],
9696
},
9797
],
98-
rehypeSlug,
98+
rehypeHeadingSlugs,
9999
[rehypeAutolinkHeadings, autolinkConfig],
100100
// @ts-expect-error TODO: fix types
101101
rehypeTitleFigure,

package-lock.json

Lines changed: 2 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"rehype-autolink-headings": "^7.1.0",
7070
"rehype-external-links": "^3.0.0",
7171
"rehype-mermaid": "^2.1.0",
72-
"rehype-slug": "^6.0.0",
7372
"rehype-title-figure": "^0.1.2",
7473
"sharp": "^0.33.5",
7574
"solarflare-theme": "^0.0.2",
@@ -81,6 +80,7 @@
8180
"tippy.js": "^6.3.7",
8281
"tsx": "^4.19.1",
8382
"typescript": "^5.5.4",
83+
"unist-util-visit": "^5.0.0",
8484
"vitest": "2.0.5",
8585
"wrangler": "^3.78.10",
8686
"yaml": "^2.5.1"

plugins/rehype/heading-slugs.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { toString } from "hast-util-to-string";
2+
import { visit } from "unist-util-visit";
3+
import GithubSlugger from "github-slugger";
4+
5+
const slugs = new GithubSlugger();
6+
7+
// # foo {/*bar*/} = <a id="bar">foo</a>
8+
export default function () {
9+
return function (tree: any) {
10+
slugs.reset();
11+
12+
visit(tree, "element", function (element: any) {
13+
if (/^h[1-6]$/.test(element.tagName)) {
14+
const last = element.children.at(-1);
15+
16+
if (
17+
last.type === "mdxTextExpression" &&
18+
last.value.startsWith("/*") &&
19+
last.value.endsWith("*/")
20+
) {
21+
const id = last.value.slice(2, -2).trim();
22+
element.properties.id = slugs.slug(id);
23+
24+
const text = element.children.at(-2);
25+
text.value = text.value.trimEnd();
26+
element.children.with(-2, text);
27+
} else {
28+
if (!element.properties.id) {
29+
element.properties.id = slugs.slug(toString(element));
30+
}
31+
}
32+
}
33+
});
34+
};
35+
}

src/content/docs/style-guide/components/anchor-heading.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ import { AnchorHeading } from "~/components";
1717

1818
Markdown files (including partials) have this behavior by default, applied via rehype plugins. Therefore, the `AnchorHeading` component is usually only required when writing headings yourself inside components, or when working on non-markdown files.
1919

20-
Additionally, `AnchorHeading` is useful when rendering partial files into one location where there are duplicate headings (for example, when there are multiple H3 corresponding to `/#create` in one page). `AnchorHeading` allows you to explicitly define fragments, ensuring that each heading can be referred correctly with unique anchors.
20+
To override the ID given to a heading within Markdown, add an MDX comment at the end of the line:
21+
22+
```mdx
23+
# foo {/*bar*/}
24+
```
25+
26+
It will result in the following HTML:
27+
28+
```html
29+
<a id="bar">foo</a>
30+
```
2131

2232
:::note
2333

0 commit comments

Comments
 (0)