From 72e585fd42c403794150411c56653e94487f8b65 Mon Sep 17 00:00:00 2001 From: Jeroen van den Hout Date: Thu, 28 Mar 2024 15:52:18 +0100 Subject: [PATCH] Allow custom plugin to render (#784) * Make docs plugin configurable * Update README --- README.md | 9 ++++---- .../src/index.ts | 22 ++++++++++++------- .../src/options.ts | 1 + .../src/types.ts | 1 + 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 893f75e4b..c7b689d66 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/packages/docusaurus-plugin-openapi-docs/src/index.ts b/packages/docusaurus-plugin-openapi-docs/src/index.ts index dc5e896e9..6f7c488c9 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/index.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/index.ts @@ -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 @@ -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]; @@ -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"; @@ -95,14 +93,22 @@ export default function pluginOpenAPIDocs( context: LoadContext, options: PluginOptions ): Plugin { - 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; @@ -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; } diff --git a/packages/docusaurus-plugin-openapi-docs/src/options.ts b/packages/docusaurus-plugin-openapi-docs/src/options.ts index 057e113a1..878ce4e14 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/options.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/options.ts @@ -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( diff --git a/packages/docusaurus-plugin-openapi-docs/src/types.ts b/packages/docusaurus-plugin-openapi-docs/src/types.ts index c55fb0509..1e20efe9f 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/types.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/types.ts @@ -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;