Skip to content

Commit 7dbd570

Browse files
committed
include rehype-slug behaviour, move to individual file
1 parent bc0d04a commit 7dbd570

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

astro.config.ts

Lines changed: 2 additions & 28 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,7 +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";
20-
import { visit } from "unist-util-visit";
19+
import rehypeHeadingSlugs from "./plugins/rehype/heading-slugs";
2120

2221
const runLinkCheck = process.env.RUN_LINK_CHECK || false;
2322

@@ -71,30 +70,6 @@ const autolinkConfig: rehypeAutolinkHeadingsOptions = {
7170
content: () => [AnchorLinkIcon],
7271
};
7372

74-
// # foo {/*bar*/} = <a id="bar">foo</a>
75-
function rehypeCustomHeadingId() {
76-
return function (tree: any) {
77-
visit(tree, "element", function (element: any) {
78-
if (/^h[1-6]$/.test(element.tagName)) {
79-
const last = element.children.at(-1);
80-
81-
if (
82-
last.type === "mdxTextExpression" &&
83-
last.value.startsWith("/*") &&
84-
last.value.endsWith("*/")
85-
) {
86-
const id = last.value.slice(2, -2).trim();
87-
element.properties.id = id;
88-
89-
const text = element.children.at(-2);
90-
text.value = text.value.trimEnd();
91-
element.children.with(-2, text);
92-
}
93-
}
94-
});
95-
};
96-
}
97-
9873
// https://astro.build/config
9974
export default defineConfig({
10075
site: "https://developers.cloudflare.com",
@@ -120,8 +95,7 @@ export default defineConfig({
12095
rel: ["noopener"],
12196
},
12297
],
123-
rehypeCustomHeadingId,
124-
rehypeSlug,
98+
rehypeHeadingSlugs,
12599
[rehypeAutolinkHeadings, autolinkConfig],
126100
// @ts-expect-error TODO: fix types
127101
rehypeTitleFigure,

package-lock.json

Lines changed: 0 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: 0 additions & 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",

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+
}

0 commit comments

Comments
 (0)