Skip to content

Commit

Permalink
feat: add redocBundleURL option
Browse files Browse the repository at this point in the history
It is helpful to be able render documentation with a non-standard
redoc bundle, e.g., for testing.  In theory, one could use a custom
HTML template for this purpose but in practice that's not
straight-forward because the template would have to be different
depending on whether or not `preview-docs` or `build-docs` is run.
The template would also have to include all the CSS code.
  • Loading branch information
dmosberger committed Dec 19, 2024
1 parent b5243a3 commit 13b1c0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-gifts-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/cli": minor
---

add redocBundleURL option
9 changes: 6 additions & 3 deletions packages/cli/src/commands/build-docs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export async function getPageHTML(
: redocOptions?.htmlTemplate
? resolve(configPath ? dirname(configPath) : '', redocOptions.htmlTemplate)
: join(__dirname, './template.hbs');

const redocBundleURL =
redocOptions.redocBundleURL ||
'https://cdn.redocly.com/redoc/v${redocCurrentVersion}/bundles/redoc.standalone.js';

const template = compile(readFileSync(templateFileName).toString());
return template({
redocHTML: `
Expand All @@ -86,9 +91,7 @@ export async function getPageHTML(
Redoc.${'hydrate(__redoc_state, container)'};
</script>`,
redocHead:
`<script src="https://cdn.redocly.com/redoc/v${redocCurrentVersion}/bundles/redoc.standalone.js"></script>` +
css,
redocHead: `<script src="${redocBundleURL}"></script>` + css,
title: title || api.info.title || 'ReDoc documentation',
disableGoogleFont,
templateOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import type { IncomingMessage } from 'http';

function getPageHTML(
htmlTemplate: string,
redocOptions: object = {},
redocOptions: {
redocBundleURL?: string;
} = {},
useRedocPro: boolean,
wsPort: number,
host: string
Expand All @@ -24,6 +26,12 @@ function getPageHTML(

const template = compile(templateSrc);

const redocBundleURL =
redocOptions.redocBundleURL ||
(useRedocPro
? 'https://cdn.redocly.com/reference-docs/latest/redocly-reference-docs.min.js'
: 'https://cdn.redocly.com/redoc/latest/bundles/redoc.standalone.js');

return template({
redocHead: `
<script>
Expand All @@ -33,11 +41,7 @@ function getPageHTML(
</script>
<script src="/simplewebsocket.min.js"></script>
<script src="/hot.js"></script>
<script src="${
useRedocPro
? 'https://cdn.redocly.com/reference-docs/latest/redocly-reference-docs.min.js'
: 'https://cdn.redocly.com/redoc/latest/bundles/redoc.standalone.js'
}"></script>
<script src="/${redocBundleURL}"></script>
`,
redocHTML: `
<div id="redoc"></div>
Expand Down

0 comments on commit 13b1c0b

Please sign in to comment.