Skip to content

Commit fe75197

Browse files
authored
fix section headings with PlatformIdentifier (#10925)
1 parent 678962c commit fe75197

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"rehype-preset-minify": "^7.0.0",
9191
"rehype-prism-diff": "^1.1.2",
9292
"rehype-prism-plus": "^1.6.3",
93-
"rehype-slug": "^6.0.0",
9493
"rehype-stringify": "^10.0.0",
9594
"remark-gfm": "^4.0.0",
9695
"remark-mdx-images": "^3.0.0",

src/mdx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import rehypeAutolinkHeadings from 'rehype-autolink-headings';
99
import rehypePresetMinify from 'rehype-preset-minify';
1010
import rehypePrismDiff from 'rehype-prism-diff';
1111
import rehypePrismPlus from 'rehype-prism-plus';
12-
import rehypeSlug from 'rehype-slug';
1312
import remarkGfm from 'remark-gfm';
1413
import remarkMdxImages from 'remark-mdx-images';
1514

@@ -18,6 +17,7 @@ import getPackageRegistry from './build/packageRegistry';
1817
import {apiCategories} from './build/resolveOpenAPI';
1918
import getAllFilesRecursively from './files';
2019
import rehypeOnboardingLines from './rehype-onboarding-lines';
20+
import rehypeSlug from './rehype-slug.js';
2121
import remarkCodeTabs from './remark-code-tabs';
2222
import remarkCodeTitles from './remark-code-title';
2323
import remarkComponentSpacing from './remark-component-spacing';

src/rehype-slug.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Forked from https://github.com/rehypejs/rehype-slug to support PlatformIdentifier nodes
3+
*/
4+
5+
/**
6+
* @typedef {import('hast').Root} Root
7+
*/
8+
9+
/**
10+
* @typedef Options
11+
* Configuration (optional).
12+
* @property {string} [prefix='']
13+
* Prefix to add in front of `id`s (default: `''`).
14+
*/
15+
16+
import GithubSlugger from 'github-slugger';
17+
import {headingRank} from 'hast-util-heading-rank';
18+
import {toString} from 'hast-util-to-string';
19+
import {visit} from 'unist-util-visit';
20+
21+
/** @type {Options} */
22+
const emptyOptions = {};
23+
const slugs = new GithubSlugger();
24+
25+
/**
26+
* Add `id`s to headings.
27+
*
28+
* @param {Options | null | undefined} [options]
29+
* Configuration (optional).
30+
* @returns
31+
* Transform.
32+
*/
33+
export default function rehypeSlug(options) {
34+
const settings = options || emptyOptions;
35+
const prefix = settings.prefix || '';
36+
37+
/**
38+
* @param {Root} tree
39+
* Tree.
40+
* @returns {undefined}
41+
* Nothing.
42+
*/
43+
return function (tree) {
44+
slugs.reset();
45+
46+
// Custom function to handle PlatformIdentifier nodes
47+
/**
48+
* @param {Root} n
49+
* @returns {string}
50+
*/
51+
const myToString = n =>
52+
n.children.length
53+
? n.children
54+
.map(node =>
55+
node.name === 'PlatformIdentifier'
56+
? node.attributes.find(att => att.name === 'name').value
57+
: toString(node)
58+
)
59+
.join('')
60+
: n.value;
61+
62+
visit(tree, 'element', function (node) {
63+
if (headingRank(node) && !node.properties.id) {
64+
node.properties.id = prefix + slugs.slug(myToString(node));
65+
}
66+
});
67+
};
68+
}

0 commit comments

Comments
 (0)