Skip to content

[docs] Fix incorrect links in TOC on releases page #2150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import rehypeExtractToc from '@stefanprobst/rehype-extract-toc';
import remarkGfm from 'remark-gfm';
import remarkTypography from 'remark-typography';
import { rehypeQuickNav } from 'docs/src/components/QuickNav/rehypeQuickNav.mjs';
import { rehypeChangelog } from 'docs/src/components/QuickNav/rehypeChangelog.mjs';
import { rehypeKbd } from 'docs/src/components/Kbd/rehypeKbd.mjs';
import { rehypeReference } from 'docs/src/components/ReferenceTable/rehypeReference.mjs';
import { rehypeDemos } from 'docs/src/components/Demo/rehypeDemos.mjs';
Expand All @@ -27,6 +28,7 @@ const withMdx = nextMdx({
rehypeReference,
...rehypeSyntaxHighlighting,
rehypeSlug,
rehypeChangelog,
rehypeExtractToc,
rehypeQuickNav,
rehypeSubtitle,
Expand Down
38 changes: 38 additions & 0 deletions docs/src/components/QuickNav/rehypeChangelog.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { headingRank } from 'hast-util-heading-rank';
import { toString } from 'hast-util-to-string';
import { visit, EXIT } from 'unist-util-visit';
import { stringToUrl } from './rehypeSlug.mjs';

// Copied from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
// Adds `'v'` to beginning to match the headings.
const SEMVER_PATTERN =
/^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/gm;

/**
* Adds `id`s prefixed with a semver string to changelog headings.
*/
export function rehypeChangelog() {
return (tree) => {
visit(tree, 'element', (node, _, parent) => {
if (headingRank(node) === 1 && toString(node) !== 'Releases') {
return EXIT;
}

if (headingRank(node) === 3) {
let index = parent.children.indexOf(node);

// eslint-disable-next-line no-plusplus
while (index--) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the official util does it: https://github.com/syntax-tree/unist-util-find-before
Though I didn't think we need to add it as a dependency just for this

const child = parent.children[index];
if (SEMVER_PATTERN.test(toString(child)) && !node.properties.id) {
const version = toString(child);
node.properties.id = `${version}-${stringToUrl(toString(node))}`;
}
}
} else if (headingRank(node) && !node.properties.id) {
node.properties.id = stringToUrl(toString(node));
}
return undefined;
});
};
}
8 changes: 6 additions & 2 deletions docs/src/components/QuickNav/rehypeSlug.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { headingRank } from 'hast-util-heading-rank';
import { toString } from 'hast-util-to-string';
import { visit } from 'unist-util-visit';
import { visit, EXIT } from 'unist-util-visit';

// This is a fork of https://github.com/rehypejs/rehype-slug/blob/main/lib/index.js, but better

Expand Down Expand Up @@ -39,8 +39,12 @@ export function rehypeSlug(options) {
return (tree) => {
visit(tree, 'element', (node) => {
if (headingRank(node) && !node.properties.id) {
if (headingRank(node) === 1 && toString(node) === 'Releases') {
return EXIT;
}
node.properties.id = prefix + stringToUrl(toString(node));
}
return undefined;
});
};
}
Expand All @@ -49,7 +53,7 @@ export function rehypeSlug(options) {
* Converts a string into a well-formatted URL material, taking into account common contractions
* `"1. Here’s a wicked example & more." => "1-here-is-a-wicked-example-and-more"`
*/
function stringToUrl(string) {
export function stringToUrl(string) {
return string
.replace(/([a-z])('|’|‘)ll([^a-z])/gi, '$1-will$3') // "you'll, we'll" => "you-will, we-will"
.replace(/won('|’|‘)t([^a-z])/gi, 'will-not$2') // "won't" => "will-not"
Expand Down