Skip to content

Commit 409df8d

Browse files
authored
[Docs Site] Downgrade heading levels to h4 minimum in changelog (#21908)
1 parent a32feb3 commit 409df8d

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

package-lock.json

Lines changed: 1 addition & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"fast-xml-parser": "5.2.1",
7171
"github-slugger": "2.0.0",
7272
"globals": "16.0.0",
73+
"hast-util-heading-rank": "3.0.0",
7374
"hast-util-select": "6.0.4",
7475
"hastscript": "9.0.1",
7576
"he": "1.2.0",

src/pages/changelog/index.astro

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import StarlightPage, {
33
type StarlightPageProps,
44
} from "@astrojs/starlight/components/StarlightPage.astro";
5-
import { render } from "astro:content";
65
76
import Header from "~/components/changelog/Header.astro";
87
import ProductPills from "~/components/changelog/ProductPills.astro";
@@ -11,6 +10,10 @@ import { Steps } from "~/components";
1110
import { format } from "date-fns";
1211
import { getChangelogs } from "~/util/changelog";
1312
import { productsByGroup } from "~/util/products";
13+
import { entryToString } from "~/util/container";
14+
import { process } from "~/util/rehype";
15+
16+
import rehypeShiftHeadings from "~/plugins/rehype/shift-headings.ts";
1417
1518
const notes = await getChangelogs({
1619
filter: (entry) => !entry.data.hidden,
@@ -42,7 +45,8 @@ const props = {
4245
.map((group) => group[0])
4346
.sort();
4447

45-
const { Content } = await render(entry);
48+
const html = await entryToString(entry, Astro.locals);
49+
const processed = await process(html, [[rehypeShiftHeadings]]);
4650

4751
return (
4852
<div
@@ -76,9 +80,7 @@ const props = {
7680
</h3>
7781
<ProductPills products={entry.data.products} />
7882
</div>
79-
<p>
80-
<Content />
81-
</p>
83+
<p set:html={processed} />
8284
</li>
8385
</ol>
8486
</Steps>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { headingRank } from "hast-util-heading-rank";
2+
import { visit } from "unist-util-visit";
3+
import type { Root, Element } from "hast";
4+
5+
export default function () {
6+
return function (tree: Root) {
7+
visit(tree, "element", function (element) {
8+
const classNames = (element.properties.className as string[]) ?? [];
9+
10+
if (classNames.includes("heading-wrapper")) {
11+
const heading = element.children.find(
12+
(el) => el.type === "element" && headingRank(el),
13+
) as Element | undefined;
14+
15+
if (heading) {
16+
let level = headingRank(heading);
17+
18+
if (level && level < 4) {
19+
const index = classNames.indexOf(`level-h${level}`);
20+
21+
level = 4;
22+
element.children[element.children.indexOf(heading)] = {
23+
...heading,
24+
tagName: "h" + (level > 6 ? 6 : level < 1 ? 1 : level),
25+
};
26+
27+
classNames[index] = `level-h${level}`;
28+
}
29+
}
30+
}
31+
});
32+
};
33+
}

0 commit comments

Comments
 (0)