Skip to content
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
"rehype-preset-minify": "^7.0.0",
"rehype-prism-diff": "^1.1.2",
"rehype-prism-plus": "^1.6.3",
"rehype-slug": "^6.0.0",
"rehype-stringify": "^10.0.0",
"remark-gfm": "^4.0.0",
"remark-mdx-images": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypePresetMinify from 'rehype-preset-minify';
import rehypePrismDiff from 'rehype-prism-diff';
import rehypePrismPlus from 'rehype-prism-plus';
import rehypeSlug from 'rehype-slug';
import remarkGfm from 'remark-gfm';
import remarkMdxImages from 'remark-mdx-images';

Expand All @@ -18,6 +17,7 @@ import getPackageRegistry from './build/packageRegistry';
import {apiCategories} from './build/resolveOpenAPI';
import getAllFilesRecursively from './files';
import rehypeOnboardingLines from './rehype-onboarding-lines';
import rehypeSlug from './rehype-slug.js';
import remarkCodeTabs from './remark-code-tabs';
import remarkCodeTitles from './remark-code-title';
import remarkComponentSpacing from './remark-component-spacing';
Expand Down
68 changes: 68 additions & 0 deletions src/rehype-slug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Forked from https://github.com/rehypejs/rehype-slug to support PlatformIdentifier nodes
*/

/**
* @typedef {import('hast').Root} Root
*/

/**
* @typedef Options
* Configuration (optional).
* @property {string} [prefix='']
* Prefix to add in front of `id`s (default: `''`).
*/

import GithubSlugger from 'github-slugger';
import {headingRank} from 'hast-util-heading-rank';
import {toString} from 'hast-util-to-string';
import {visit} from 'unist-util-visit';

/** @type {Options} */
const emptyOptions = {};
const slugs = new GithubSlugger();

/**
* Add `id`s to headings.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns
* Transform.
*/
export default function rehypeSlug(options) {
const settings = options || emptyOptions;
const prefix = settings.prefix || '';

/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
slugs.reset();

// Custom function to handle PlatformIdentifier nodes
/**
* @param {Root} n
* @returns {string}
*/
const myToString = n =>
n.children.length
? n.children
.map(node =>
node.name === 'PlatformIdentifier'
? node.attributes.find(att => att.name === 'name').value
: toString(node)
)
.join('')
: n.value;

visit(tree, 'element', function (node) {
if (headingRank(node) && !node.properties.id) {
node.properties.id = prefix + slugs.slug(myToString(node));
}
});
};
}