@rspress/plugin-typedoc 1.37.2 → 1.37.4

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/src/sidebar.ts DELETED
@@ -1,179 +0,0 @@
1
- import path from 'node:path';
2
- import fs from '@rspress/shared/fs-extra';
3
- import type { SidebarGroup } from '@rspress/shared';
4
- import { transformModuleName } from './utils';
5
- import { ROUTE_PREFIX } from './constants';
6
-
7
- interface ModuleItem {
8
- id: number;
9
- name: string;
10
- children: {
11
- id: number;
12
- name: string;
13
- }[];
14
- groups: {
15
- title: string;
16
- children: number[];
17
- }[];
18
- }
19
-
20
- async function patchLinks(outputDir: string) {
21
- // Patch links in markdown files
22
- // Scan all the markdown files in the output directory
23
- // replace [foo](bar) -> [foo](./bar)
24
- const normlizeLinksInFile = async (filePath: string) => {
25
- const content = await fs.readFile(filePath, 'utf-8');
26
- // replace: [foo](bar) -> [foo](./bar)
27
- const newContent = content.replace(
28
- /\[([^\]]+)\]\(([^)]+)\)/g,
29
- (_match, p1, p2) => {
30
- return `[${p1}](./${p2})`;
31
- },
32
- );
33
- await fs.writeFile(filePath, newContent);
34
- };
35
-
36
- const traverse = async (dir: string) => {
37
- const files = await fs.readdir(dir);
38
- for (const file of files) {
39
- const filePath = path.join(dir, file);
40
- const stat = await fs.stat(filePath);
41
- if (stat.isDirectory()) {
42
- await traverse(filePath);
43
- } else if (stat.isFile() && /\.mdx?/.test(file)) {
44
- await normlizeLinksInFile(filePath);
45
- }
46
- }
47
- };
48
- await traverse(outputDir);
49
- }
50
-
51
- export async function resolveSidebarForSingleEntry(
52
- jsonFile: string,
53
- ): Promise<SidebarGroup[]> {
54
- const result: SidebarGroup[] = [];
55
- const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));
56
- if (!data.children || data.children.length <= 0) {
57
- return [];
58
- }
59
- const symbolMap = new Map<string, number>();
60
- data.groups.forEach((group: { title: string; children: number[] }) => {
61
- const groupItem: SidebarGroup = {
62
- text: group.title,
63
- items: [],
64
- };
65
- group.children.forEach((id: number) => {
66
- const dataItem = data.children.find((item: ModuleItem) => item.id === id);
67
- if (dataItem) {
68
- // Note: we should handle the case that classes and interfaces have the same name
69
- // Such as class `Env` and variable `env`
70
- // The final output file name should be `classes/Env.md` and `variables/env-1.md`
71
- let fileName = dataItem.name;
72
- if (symbolMap.has(dataItem.name)) {
73
- const count = symbolMap.get(dataItem.name)! + 1;
74
- symbolMap.set(dataItem.name, count);
75
- fileName = `${dataItem.name}-${count}`;
76
- } else {
77
- symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);
78
- }
79
- groupItem.items.push({
80
- text: dataItem.name,
81
- link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`,
82
- });
83
- }
84
- });
85
- result.push(groupItem);
86
- });
87
-
88
- await patchLinks(path.dirname(jsonFile));
89
-
90
- return result;
91
- }
92
-
93
- export async function resolveSidebarForMultiEntry(
94
- jsonFile: string,
95
- ): Promise<SidebarGroup[]> {
96
- const result: SidebarGroup[] = [];
97
- const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));
98
- if (!data.children || data.children.length <= 0) {
99
- return result;
100
- }
101
-
102
- function getModulePath(name: string) {
103
- return path
104
- .join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`)
105
- .replace(/\\/g, '/');
106
- }
107
-
108
- function getClassPath(moduleName: string, className: string) {
109
- return path
110
- .join(
111
- `${ROUTE_PREFIX}/classes`,
112
- `${transformModuleName(moduleName)}.${className}`,
113
- )
114
- .replace(/\\/g, '/');
115
- }
116
-
117
- function getInterfacePath(moduleName: string, interfaceName: string) {
118
- return path
119
- .join(
120
- `${ROUTE_PREFIX}/interfaces`,
121
- `${transformModuleName(moduleName)}.${interfaceName}`,
122
- )
123
- .replace(/\\/g, '/');
124
- }
125
-
126
- function getFunctionPath(moduleName: string, functionName: string) {
127
- return path
128
- .join(
129
- `${ROUTE_PREFIX}/functions`,
130
- `${transformModuleName(moduleName)}.${functionName}`,
131
- )
132
- .replace(/\\/g, '/');
133
- }
134
-
135
- data.children.forEach((module: ModuleItem) => {
136
- const moduleNames = module.name.split('/');
137
- const name = moduleNames[moduleNames.length - 1];
138
- const moduleConfig = {
139
- text: `Module:${name}`,
140
- items: [{ text: 'Overview', link: getModulePath(module.name) }],
141
- };
142
- if (!module.children || module.children.length <= 0) {
143
- return;
144
- }
145
- module.children.forEach(sub => {
146
- const kindString = module.groups
147
- .find(item => item.children.includes(sub.id))
148
- ?.title.slice(0, -1);
149
- if (!kindString) {
150
- return;
151
- }
152
- switch (kindString) {
153
- case 'Class':
154
- moduleConfig.items.push({
155
- text: `Class:${sub.name}`,
156
- link: getClassPath(module.name, sub.name),
157
- });
158
- break;
159
- case 'Interface':
160
- moduleConfig.items.push({
161
- text: `Interface:${sub.name}`,
162
- link: getInterfacePath(module.name, sub.name),
163
- });
164
- break;
165
- case 'Function':
166
- moduleConfig.items.push({
167
- text: `Function:${sub.name}`,
168
- link: getFunctionPath(module.name, sub.name),
169
- });
170
- break;
171
- default:
172
- break;
173
- }
174
- });
175
- result.push(moduleConfig);
176
- });
177
- await patchLinks(path.dirname(jsonFile));
178
- return result;
179
- }