@rspress/plugin-api-docgen 2.0.0-beta.4 → 2.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/plugin-api-docgen",
3
- "version": "2.0.0-beta.4",
3
+ "version": "2.0.0-beta.6",
4
4
  "description": "A plugin for rspress to generate api doc.",
5
5
  "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
6
  "repository": {
@@ -25,15 +25,18 @@
25
25
  "dependencies": {
26
26
  "chokidar": "^3.6.0",
27
27
  "documentation": "14.0.3",
28
+ "github-slugger": "^2.0.0",
28
29
  "react-docgen-typescript": "2.2.2",
29
30
  "react-markdown": "^10.1.0",
30
31
  "remark-gfm": "^4.0.1",
31
32
  "unified": "^11.0.5",
32
- "@rspress/shared": "2.0.0-beta.4"
33
+ "unist-util-visit": "^5.0.0",
34
+ "@rspress/shared": "2.0.0-beta.6"
33
35
  },
34
36
  "devDependencies": {
35
37
  "@microsoft/api-extractor": "^7.52.7",
36
- "@rslib/core": "0.6.8",
38
+ "@rslib/core": "0.6.9",
39
+ "@types/hast": "^3.0.4",
37
40
  "@types/mdast": "^4.0.4",
38
41
  "@types/node": "^18.11.17",
39
42
  "@types/react": "^18.3.21",
@@ -41,11 +44,11 @@
41
44
  "react": "^19.1.0",
42
45
  "react-dom": "^19.1.0",
43
46
  "react-router-dom": "^6.29.0",
44
- "rsbuild-plugin-publint": "^0.3.1",
47
+ "rsbuild-plugin-publint": "^0.3.2",
45
48
  "typescript": "^5.8.2"
46
49
  },
47
50
  "peerDependencies": {
48
- "@rspress/core": "^2.0.0-beta.4",
51
+ "@rspress/core": "^2.0.0-beta.6",
49
52
  "typescript": "^5.8.2"
50
53
  },
51
54
  "peerDependenciesMeta": {
@@ -5,6 +5,97 @@ import { getCustomMDXComponent } from '@rspress/core/theme';
5
5
  import ReactMarkdown from 'react-markdown';
6
6
  import remarkGfm from 'remark-gfm';
7
7
  import './API.css';
8
+ import GithubSlugger from 'github-slugger';
9
+ import type { Content, Element, Root } from 'hast';
10
+ // biome-ignore lint/style/useImportType: <exact>
11
+ import React from 'react';
12
+ import type { Plugin } from 'unified';
13
+ import { visit } from 'unist-util-visit';
14
+
15
+ function headingRank(node: Root | Content): number | null {
16
+ const name =
17
+ (node && node.type === 'element' && node.tagName.toLowerCase()) || '';
18
+ const code =
19
+ name.length === 2 && name.charCodeAt(0) === 104 /* `h` */
20
+ ? name.charCodeAt(1)
21
+ : 0;
22
+ return code > 48 /* `0` */ && code < 55 /* `7` */
23
+ ? code - 48 /* `0` */
24
+ : null;
25
+ }
26
+
27
+ const rehypeHeaderAnchor: Plugin<[], Root> = () => {
28
+ const slugger = new GithubSlugger();
29
+ return tree => {
30
+ visit(tree, 'element', node => {
31
+ if (!headingRank(node)) {
32
+ return;
33
+ }
34
+ // generate id
35
+
36
+ if (!node.properties?.id) {
37
+ const text = collectHeaderText(node);
38
+ node.properties ??= {};
39
+ node.properties.id = slugger.slug(text);
40
+ }
41
+ // apply to headings
42
+ node.children.unshift(create(node));
43
+ });
44
+ };
45
+ };
46
+
47
+ /**
48
+ * Create an `a`.
49
+ *
50
+ * @param {Readonly<Element>} node
51
+ * Related heading.
52
+ * @returns {Element}
53
+ * Link.
54
+ */
55
+ function create(node: Element): Element {
56
+ return {
57
+ type: 'element',
58
+ tagName: 'a',
59
+ properties: {
60
+ class: 'header-anchor',
61
+ ariaHidden: 'true',
62
+ href: `#${node.properties!.id}`,
63
+ },
64
+ children: [
65
+ {
66
+ type: 'text',
67
+ value: '#',
68
+ },
69
+ ],
70
+ };
71
+ }
72
+
73
+ const extractTextAndId = (title?: string): string => {
74
+ if (!title) {
75
+ return '';
76
+ }
77
+ const text = title.trimEnd();
78
+ return text;
79
+ };
80
+
81
+ const collectHeaderText = (node: Element): string => {
82
+ let text = '';
83
+ node.children.forEach(child => {
84
+ if (child.type === 'text') {
85
+ const textPart = extractTextAndId(child.value);
86
+ child.value = textPart;
87
+ text += textPart;
88
+ }
89
+ if (child.type === 'element') {
90
+ child.children.forEach(c => {
91
+ if (c.type === 'text') {
92
+ text += c.value;
93
+ }
94
+ });
95
+ }
96
+ });
97
+ return text;
98
+ };
8
99
 
9
100
  export default (props: { moduleName: string }) => {
10
101
  const lang = useLang();
@@ -19,6 +110,7 @@ export default (props: { moduleName: string }) => {
19
110
  <div className="rspress-plugin-api-docgen">
20
111
  <ReactMarkdown
21
112
  remarkPlugins={[[remarkGfm]]}
113
+ rehypePlugins={[[rehypeHeaderAnchor]]}
22
114
  components={
23
115
  getCustomMDXComponent() as Record<string, React.ElementType>
24
116
  }