Skip to content

Commit

Permalink
Allow custom plugin to render (#784)
Browse files Browse the repository at this point in the history
* Make docs plugin configurable

* Update README
  • Loading branch information
jlvandenhout committed Mar 28, 2024
1 parent e00226f commit 72e585f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ Here is an example of properly configuring `docusaurus.config.js` file for `docu

The `docusaurus-plugin-openapi-docs` plugin can be configured with the following options:

| Name | Type | Default | Description |
| -------------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | `null` | A unique plugin ID. |
| `docsPluginId` | `string` | `null` | The ID associated with the `plugin-content-docs` or `preset` instance used to render the OpenAPI docs (e.g. "your-plugin-id", "classic", "default"). |
| Name | Type | Default | Description |
| -------------- | -------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | `null` | A unique plugin ID. |
| `docsPlugin` | `string` | `@docusaurus/plugin-content-docs` | The plugin used to render the OpenAPI docs (ignored if the plugin instance referenced by `docsPluginId` is a `preset`). |
| `docsPluginId` | `string` | `null` | The plugin ID associated with the `preset` or configured `docsPlugin` instance used to render the OpenAPI docs (e.g. "your-plugin-id", "classic", "default"). |

### config

Expand Down
22 changes: 14 additions & 8 deletions packages/docusaurus-plugin-openapi-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function isURL(str: string): boolean {

export function getDocsPluginConfig(
presetsPlugins: any[],
plugin: string,
pluginId: string
): Object | undefined {
// eslint-disable-next-line array-callback-return
Expand All @@ -52,10 +53,7 @@ export function getDocsPluginConfig(
}

// Search plugin-content-docs instances
if (
typeof data[0] === "string" &&
data[0] === "@docusaurus/plugin-content-docs"
) {
if (typeof data[0] === "string" && data[0] === plugin) {
const configPluginId = data[1].id ? data[1].id : "default";
if (configPluginId === pluginId) {
return data[1];
Expand All @@ -71,7 +69,7 @@ export function getDocsPluginConfig(
}

// Search plugin-content-docs instances
if (filteredConfig[0] === "@docusaurus/plugin-content-docs") {
if (filteredConfig[0] === plugin) {
const configPluginId = filteredConfig[1].id
? filteredConfig[1].id
: "default";
Expand All @@ -95,14 +93,22 @@ export default function pluginOpenAPIDocs(
context: LoadContext,
options: PluginOptions
): Plugin<LoadedContent> {
const { config, docsPluginId } = options;
const {
config,
docsPlugin = "@docusaurus/plugin-content-docs",
docsPluginId,
} = options;
const { siteDir, siteConfig } = context;

// Get routeBasePath and path from plugin-content-docs or preset
const presets: any = siteConfig.presets;
const plugins: any = siteConfig.plugins;
const presetsPlugins = presets.concat(plugins);
let docData: any = getDocsPluginConfig(presetsPlugins, docsPluginId);
let docData: any = getDocsPluginConfig(
presetsPlugins,
docsPlugin,
docsPluginId
);
let docRouteBasePath = docData ? docData.routeBasePath : undefined;
let docPath = docData ? (docData.path ? docData.path : "docs") : undefined;

Expand All @@ -121,7 +127,7 @@ export default function pluginOpenAPIDocs(

// Override docPath if pluginId provided
if (pluginId) {
docData = getDocsPluginConfig(presetsPlugins, pluginId);
docData = getDocsPluginConfig(presetsPlugins, docsPlugin, pluginId);
docRouteBasePath = docData ? docData.routeBasePath : undefined;
docPath = docData ? (docData.path ? docData.path : "docs") : undefined;
}
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-openapi-docs/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const markdownGenerators = Joi.object({

export const OptionsSchema = Joi.object({
id: Joi.string().required(),
docsPlugin: Joi.string(),
docsPluginId: Joi.string().required(),
config: Joi.object()
.pattern(
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-openapi-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type {
} from "@docusaurus/plugin-content-docs-types";
export interface PluginOptions {
id?: string;
docsPlugin?: string;
docsPluginId: string;
config: {
[key: string]: APIOptions;
Expand Down

0 comments on commit 72e585f

Please sign in to comment.