Skip to content

Commit ba2bd9c

Browse files
Add option to disable normalization of spec download URLs (#402)
* first pass: hard-code false value for absolute * WIP add toggle for absolute * move URL normalization flag down to spec options * undo package.json change * docs(changeset): Add support for relative spec download URLs * Apply suggestions from code review * Update packages/docusaurus-theme-redoc/src/utils/useSpec.ts --------- Co-authored-by: Rohit Gohri <[email protected]>
1 parent 42fc3cb commit ba2bd9c

File tree

8 files changed

+20
-5
lines changed

8 files changed

+20
-5
lines changed

.changeset/angry-doors-switch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'docusaurus-plugin-redoc': patch
3+
'docusaurus-theme-redoc': patch
4+
---
5+
6+
Add support for relative spec download URLs

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function redocPlugin(
4242
}> {
4343
const { baseUrl } = context.siteConfig;
4444
const options: PluginOptionsWithDefault = { ...DEFAULT_OPTIONS, ...opts };
45-
const { debug, spec, url: downloadUrl, config, themeId } = options;
45+
const { debug, spec, url: downloadUrl, config, themeId, normalizeUrl: normalizeDownloadUrl } = options;
4646

4747
let url = downloadUrl;
4848
const isSpecFile = fs.existsSync(spec);
@@ -127,6 +127,7 @@ export default function redocPlugin(
127127
url,
128128
themeId,
129129
isSpecFile,
130+
normalizeUrl: normalizeDownloadUrl,
130131
// eslint-disable-next-line @typescript-eslint/no-explicit-any
131132
spec: content.converted as any,
132133
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface PluginOptions extends PluginDirectUsageOptions {
3535
spec: string;
3636
url?: string;
3737
route?: string;
38+
normalizeUrl?: boolean;
3839
}
3940

4041
export interface PluginOptionsWithDefault extends PluginOptions {
@@ -44,6 +45,7 @@ export interface PluginOptionsWithDefault extends PluginOptions {
4445
export const DEFAULT_OPTIONS = {
4546
layout: {},
4647
debug: false,
48+
normalizeUrl: true,
4749
} satisfies Omit<PluginOptions, 'spec'>;
4850

4951
export const PluginOptionSchema = Joi.object<PluginOptions>({
@@ -56,6 +58,7 @@ export const PluginOptionSchema = Joi.object<PluginOptions>({
5658
// Basic
5759
spec: Joi.string(),
5860
url: Joi.string().uri({ allowRelative: true }).optional(),
61+
normalizeUrl: Joi.boolean().default(DEFAULT_OPTIONS.normalizeUrl),
5962
route: Joi.string().uri({ relativeOnly: true }).optional(),
6063
layout: Joi.any().default(DEFAULT_OPTIONS.layout),
6164
});

packages/docusaurus-theme-redoc/src/redocData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const getDefaultTheme = (
3030
prism: {
3131
additionalLanguages: ['scala'],
3232
},
33-
},
33+
}
3434
},
3535
customTheme,
3636
);
@@ -148,6 +148,7 @@ export function getRedocThemes(
148148
export async function getGlobalData({
149149
primaryColor,
150150
primaryColorDark = primaryColor,
151+
//normalizeUrl = true,
151152
theme: customThemeDeprecated,
152153
options,
153154
}: ThemeOptions): Promise<GlobalData> {

packages/docusaurus-theme-redoc/src/theme/Redoc/ServerStyles.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export function ServerStyles({
4040
lightThemeOptions: RedocRawOptions;
4141
darkThemeOptions: RedocRawOptions;
4242
}) {
43-
const fullUrl = useBaseUrl(specProps.url, { absolute: true });
43+
const absoluteUrl = useBaseUrl(specProps.url, { absolute: true });
44+
const fullUrl = specProps.normalizeUrl ? absoluteUrl : specProps.url;
4445

4546
const lightCss = prefixCssSelectors(
4647
renderCss(new AppStore(specProps.spec, fullUrl, lightThemeOptions)),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface SpecProps {
99
url?: string;
1010
isSpecFile?: boolean;
1111
themeId?: string;
12+
normalizeUrl?: boolean;
1213
}
1314

1415
export type RedocProps = SpecProps;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface SpecProps {
44
url?: string;
55
isSpecFile?: boolean;
66
themeId?: string;
7+
normalizeUrl?: boolean;
78
}
89

910
declare module '@theme/Redoc' {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ let currentStore: AppStore | null = null;
1717
* Released under the MIT License
1818
*/
1919
export function useSpec(
20-
{ spec, url, themeId }: SpecProps,
20+
{ spec, url, themeId, normalizeUrl }: SpecProps,
2121
optionsOverrides?: RedocRawOptions,
2222
) {
2323
const specOptions = useSpecOptions(themeId, optionsOverrides);
24-
const fullUrl = useBaseUrl(url, { absolute: true });
24+
const absoluteUrl = useBaseUrl(url, { absolute: true });
25+
const fullUrl = normalizeUrl ? absoluteUrl : url;
2526
const isBrowser = useIsBrowser();
2627
const isDarkTheme = useColorMode().colorMode === 'dark';
2728

0 commit comments

Comments
 (0)