Skip to content

Commit c18827e

Browse files
authored
Handle multiple instances of presets (#189)
1 parent 0e93ff3 commit c18827e

File tree

9 files changed

+50
-12
lines changed

9 files changed

+50
-12
lines changed

.changeset/perfect-cheetahs-visit.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'docusaurus-plugin-redoc': patch
3+
'docusaurus-theme-redoc': patch
4+
'redocusaurus': patch
5+
---
6+
7+
Pass through a themeId to load multiple presets at the same time

.github/workflows/version.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ jobs:
5050
yarn workspace redocusaurus exec "sh $PWD/scripts/release.sh" >> $GITHUB_ENV
5151
5252
- name: Create Github Release
53-
if: ${{ env.github_release == 'yes' }}
53+
if: |
54+
steps.changesets.outputs.published == 'true' &&
55+
env.github_release == 'yes'
5456
uses: softprops/action-gh-release@master
5557
with:
5658
body_path: latest-CHANGELOG.md

packages/docusaurus-plugin-redoc/src/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default function redocPlugin(
5151
console.error('[REDOCUSAURUS_PLUGIN] Opts Input:', opts);
5252
console.error('[REDOCUSAURUS_PLUGIN] Options:', options);
5353
}
54+
55+
const { themeId } = options;
5456
return {
5557
name: 'docusaurus-plugin-redoc',
5658
async loadContent() {
@@ -119,23 +121,25 @@ export default function redocPlugin(
119121

120122
const data: SpecProps = {
121123
url,
124+
themeId,
122125
// eslint-disable-next-line @typescript-eslint/no-explicit-any
123126
spec: content.converted as any,
124127
};
125128
setGlobalData(data);
126129

127130
if (options.route) {
131+
const routePath = options.route.startsWith('/')
132+
? options.route.slice(1)
133+
: options.route;
134+
128135
const specProps = await createData(
129-
`redocApiSpecV1-${options.id || '1'}.json`,
136+
`redocApiSpecV1.1-${options.id || '1'}.json`,
130137
JSON.stringify(data),
131138
);
132139
const layoutProps = await createData(
133140
`redocApiLayoutV1-${options.id || '1'}.json`,
134141
JSON.stringify(options.layout),
135142
);
136-
const routePath = options.route.startsWith('/')
137-
? options.route.slice(1)
138-
: options.route;
139143

140144
const modules: Record<keyof ApiDocProps, string> = {
141145
specProps,

packages/docusaurus-plugin-redoc/src/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface PluginOptions {
2222
route?: string;
2323
layout?: LayoutProps;
2424
debug?: boolean;
25+
themeId?: string;
2526
/**
2627
* Redocly config to bundle file
2728
* @see https://redocly.com/docs/cli/configuration/configuration-file/
@@ -46,4 +47,5 @@ export const PluginOptionSchema = Joi.object<PluginOptions>({
4647
debug: Joi.boolean().default(DEFAULT_OPTIONS.debug),
4748
route: Joi.string().uri({ relativeOnly: true }).optional(),
4849
config: Joi.any().optional(),
50+
themeId: Joi.string().optional(),
4951
});

packages/docusaurus-theme-redoc/src/theme/ApiDocMdx/ApiDocMdx.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useMemo } from 'react';
2-
import Redoc from '../Redoc';
3-
import { useSpecData } from '../useSpecData';
2+
import Redoc from '@theme/Redoc';
3+
import useSpecData from '@theme/useSpecData';
44
import { MdxProps as Props } from '../../types/common';
55
import '../ApiSchema/styles.css';
66

packages/docusaurus-theme-redoc/src/types/common.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type ParsedSpec = OpenAPISpec;
77
export interface SpecProps {
88
spec: ParsedSpec;
99
url?: string;
10+
themeId?: string;
1011
}
1112

1213
export type RedocProps = SpecProps;

packages/docusaurus-theme-redoc/src/types/modules.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ interface SpecProps {
22
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33
spec: import('redoc/typings/types').OpenAPISpec;
44
url?: string;
5+
themeId?: string;
56
}
67

78
declare module '@theme/Redoc' {
8-
const Redoc: (props: SpecProps & { className?: string }) => JSX.Element;
9+
import type { RedocRawOptions } from 'redoc';
10+
const Redoc: (
11+
props: SpecProps & {
12+
className?: string;
13+
optionsOverrides?: RedocRawOptions;
14+
},
15+
) => JSX.Element;
916
export default Redoc;
1017
}
1118

packages/docusaurus-theme-redoc/src/utils/useSpec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import { GlobalData } from '../types/options';
1616
* Released under the MIT License
1717
*/
1818
export function useSpec(
19-
{ spec, url }: SpecProps,
19+
{ spec, url, themeId }: SpecProps,
2020
optionsOverrides?: RedocRawOptions,
2121
) {
2222
const fullUrl = useBaseUrl(url, { absolute: true });
2323
const isBrowser = useIsBrowser();
2424
const isDarkTheme = useColorMode().colorMode === 'dark';
25-
const themeOptions = usePluginData('docusaurus-theme-redoc') as GlobalData;
25+
const themeOptions = usePluginData(
26+
'docusaurus-theme-redoc',
27+
themeId,
28+
) as GlobalData;
2629

2730
const stores = useMemo(() => {
2831
const { lightTheme, darkTheme, options: redocOptions } = themeOptions;

packages/redocusaurus/src/index.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PluginOptions } from 'docusaurus-plugin-redoc';
33
import type { ThemeOptions } from 'docusaurus-theme-redoc';
44

55
export interface PresetOptions {
6+
id?: string;
67
debug?: boolean;
78
specs: PluginOptions[];
89
theme?: ThemeOptions;
@@ -32,15 +33,26 @@ export default function preset(
3233
if (debug) {
3334
console.error('[REDOCUSAURUS] Specs:', specsArray);
3435
}
36+
const id = opts.id ? `-${opts.id}` : '';
37+
const themeId = `theme-redoc${id}`;
3538

3639
const config = {
37-
themes: [[require.resolve('docusaurus-theme-redoc'), theme]],
40+
themes: [
41+
[
42+
require.resolve('docusaurus-theme-redoc'),
43+
{
44+
id: themeId,
45+
...theme,
46+
},
47+
],
48+
],
3849
plugins: [
3950
...specsArray.map((pluginOpts, index) => [
4051
require.resolve('docusaurus-plugin-redoc'),
4152
{
4253
...pluginOpts,
43-
id: pluginOpts.id || `plugin-redoc-${index}`,
54+
themeId,
55+
id: pluginOpts.id || `plugin-redoc${id}-${index}`,
4456
debug,
4557
},
4658
]),

0 commit comments

Comments
 (0)