-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathServerRedoc.tsx
More file actions
71 lines (64 loc) · 1.99 KB
/
ServerRedoc.tsx
File metadata and controls
71 lines (64 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from 'react';
import useBrokenLinks, { BrokenLinks } from '@docusaurus/useBrokenLinks';
import clsx from 'clsx';
import '../../global';
import { IMenuItem, Redoc as RedocComponent } from 'redoc';
import type { ServerRedocProps } from '../../types/common';
import { useSpec } from '../../utils/useSpec';
import { ServerStyles } from './Styles';
import './styles.css';
/*!
* Redocusaurus
* https://redocusaurus.vercel.app/
* (c) 2025 Rohit Gohri
* Released under the MIT License
*/
function ServerRedoc(props: ServerRedocProps): React.JSX.Element {
const { className, optionsOverrides, ...specProps } = props;
const { store, darkThemeOptions, lightThemeOptions, hasLogo } = useSpec(
specProps,
optionsOverrides,
);
const collector = useBrokenLinks();
// eslint-disable-next-line @typescript-eslint/no-use-before-define
collectMenuItemAnchors(collector, store.menu.items);
return (
<>
<ServerStyles
specProps={specProps}
lightThemeOptions={lightThemeOptions}
darkThemeOptions={darkThemeOptions}
/>
<div
className={clsx([
'redocusaurus',
hasLogo && 'redocusaurus-has-logo',
className,
])}
>
<RedocComponent store={store} />
</div>
</>
);
}
function collectMenuItemAnchors(
collector: BrokenLinks,
menuItems: IMenuItem[],
parentAnchor = '',
) {
menuItems.forEach((menuItem) => {
// Register anchor for menu item
collector.collectAnchor(menuItem.id);
// If this is a child menu item, register a shortened anchor as well
// This may not be necessary in all cases, but definitely needed for
// menuItems of the form `tag/<Tag ID>/operation/<Operation ID>`.
if (parentAnchor != '') {
const childAnchor = menuItem.id.replace(`${parentAnchor}/`, '');
collector.collectAnchor(childAnchor);
}
if (menuItem.items.length > 0) {
collectMenuItemAnchors(collector, menuItem.items, menuItem.id);
}
});
}
export default ServerRedoc;