Home

Awesome

<h1 align="center">Docusaurus OpenAPI Doc Generator</h1> <div align="center"> <img width="200" src="https://user-images.githubusercontent.com/9343811/165975569-1bc29814-884c-4931-83df-860043b625b7.svg" /> </div> <div align="center">

OpenAPI plugin for generating API reference docs in Docusaurus v3 (compatible with Docusaurus Faster.

<img src="https://img.shields.io/badge/dynamic/json?style=for-the-badge&logo=meta&color=blueviolet&label=Docusaurus&query=dependencies%5B%22%40docusaurus%2Fcore%22%5D&url=https%3A%2F%2Fraw.githubusercontent.com%2FPaloAltoNetworks%2Fdocusaurus-openapi-docs%2Fmain%2Fdemo%2Fpackage.json" /> <br/><br/>

license npm latest package npm downloads npm canary package npm beta package <br/> build prettier Cypress.io jest PRs Welcome <br />

</div> <p align="center"> <img src="https://github.com/user-attachments/assets/38f33ad6-97a9-41de-9e00-31b8298a4e22" width="900" /> </p>

Overview

The docusaurus-plugin-openapi-docs package extends the Docusaurus CLI with commands for generating MDX using the OpenAPI specification as the source. The resulting MDX is fully compatible with plugin-content-docs and can be used to render beautiful reference API docs when combined with the docusaurus-theme-openapi-docs theme.

Key Features:

Compatibility Matrix

Docusaurus OpenAPI DocsDocusaurus
4.x.x (current)3.5.0 - 3.5.2
3.0.x (end-of-support)3.0.1 - 3.4.0
2.2.3 (legacy)2.4.1 - 2.4.3
1.7.3 (end-of-support)2.0.1 - 2.2.0

Bootstrapping from Template (new Docusaurus site)

Run the following to bootstrap a Docsaurus v3 site (classic theme) with docusaurus-openapi-docs:

npx create-docusaurus@3.5.2 my-website --package-manager yarn

When prompted to select a template choose Git repository.

Template Repository URL:

https://github.com/PaloAltoNetworks/docusaurus-template-openapi-docs.git

When asked how the template repo should be cloned choose "copy".

cd my-website
yarn start

If all goes well, you should be greeted by a brand new Docusaurus site that includes API reference docs for the ubiquitous Petstore API!

Installation (existing Docusaurus site)

Both the plugin and theme are currently designed to pair with a specific Docusaurus release. The Docusaurus badge in the README.md and at the top of this page will always reflect the current compatible versions.

Plugin

yarn add docusaurus-plugin-openapi-docs

Theme

yarn add docusaurus-theme-openapi-docs

Configuring docusaurus.config.ts (Plugin and theme usage)

Here is an example of properly configuring docusaurus.config.ts for docusaurus-plugin-openapi-docs and docusaurus-theme-openapi-docs usage.

Note: Instructions may differ slightly for sites that haven't migrated to typescript.

// docusaurus.config.ts
// note that parts of the complete config were left out for brevity
import type * as Preset from "@docusaurus/preset-classic";
import type { Config } from "@docusaurus/types";
import type * as Plugin from "@docusaurus/types/src/plugin";
import type * as OpenApiPlugin from "docusaurus-plugin-openapi-docs";

{
  presets: [
    [
      "classic",
      {
        docs: {
          sidebarPath: "./sidebars.ts",
          docItemComponent: "@theme/ApiItem", // Derived from docusaurus-theme-openapi
        },
        theme: {
          customCss: "./src/css/custom.css",
        },
      } satisfies Preset.Options,
    ],
  ],

  plugins: [
    [
      'docusaurus-plugin-openapi-docs',
      {
        id: "api", // plugin id
        docsPluginId: "classic", // configured for preset-classic
        config: {
          petstore: {
            specPath: "examples/petstore.yaml",
            outputDir: "docs/petstore",
            sidebarOptions: {
              groupPathsBy: "tag",
            },
          } satisfies OpenApiPlugin.Options,
        }
      },
    ]
  ],
  themes: ["docusaurus-theme-openapi-docs"], // export theme components
}

Note: You may optionally configure a dedicated @docusaurus/plugin-content-docs instance for use with docusaurus-theme-openapi-docs by setting docItemComponent to @theme/ApiItem.

Plugin Configuration Options

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

NameTypeDefaultDescription
idstringnullA unique plugin ID.
docsPluginstring@docusaurus/plugin-content-docsThe plugin used to render the OpenAPI docs (ignored if the plugin instance referenced by docsPluginId is a preset).
docsPluginIdstringnullThe plugin ID associated with the preset or configured docsPlugin instance used to render the OpenAPI docs (e.g. "your-plugin-id", "classic", "default").

config

config can be configured with the following options:

NameTypeDefaultDescription
specPathstringnullDesignated URL or path to the source of an OpenAPI specification file or directory of multiple OpenAPI specification files.
ouputDirstringnullDesired output path for generated MDX and sidebar files.
proxystringnullOptional: Proxy URL to prepend to base URL when performing API requests from browser.
templatestringnullOptional: Customize MDX content with a desired template.
downloadUrlstringnullOptional: Designated URL for downloading OpenAPI specification. (requires info section/doc)
hideSendButtonbooleannullOptional: If set to true, hides the "Send API Request" button in API demo panel
showExtensionsbooleannullOptional: If set to true, renders operation-level vendor-extensions in description.
sidebarOptionsobjectnullOptional: Set of options for sidebar configuration. See below for a list of supported options.
versionstringnullOptional: Version assigned to single or micro-spec API specified in specPath.
labelstringnullOptional: Version label used when generating version selector dropdown menu.
baseUrlstringnullOptional: Version base URL used when generating version selector dropdown menu.
versionsobjectnullOptional: Set of options for versioning configuration. See below for a list of supported options.
markdownGeneratorsobjectnullOptional: Customize MDX content with a set of options for specifying markdown generator functions. See below for a list of supported options.
showSchemasbooleannullOptional: If set to true, generates schema pages and adds them to the sidebar.

sidebarOptions

sidebarOptions can be configured with the following options:

NameTypeDefaultDescription
groupPathsBystringnullOrganize and group sidebar slice by specified option. Note: Currently, groupPathsBy only contains support for grouping by tag and tagGroup.
categoryLinkSourcestringnullDefines what source to use for rendering category link pages when grouping paths by tag. <br/><br/>The supported options are as follows: <br/><br/> tag: Sets the category link config type to generated-index and uses the tag description as the link config description. <br/><br/>info: Sets the category link config type to doc and renders the info section as the category link (recommended only for multi/micro-spec scenarios). <br/><br/>none: Does not create pages for categories, only groups that can be expanded/collapsed.
sidebarCollapsiblebooleantrueWhether sidebar categories are collapsible by default.
sidebarCollapsedbooleantrueWhether sidebar categories are collapsed by default.
customPropsobjectnullAdditional props for customizing a sidebar item.
sidebarGeneratorsobjectnullOptional: Customize sidebar rendering with callback functions.

You may optionally configure a sidebarOptions. In doing so, an individual sidebar.js slice with the configured options will be generated within the respective outputDir.

versions can be configured with the following options:

NameTypeDefaultDescription
specPathstringnullDesignated URL or path to the source of an OpenAPI specification file or directory of micro OpenAPI specification files.
ouputDirstringnullDesired output path for versioned, generated MDX files.
labelstringnullOptional: Version label used when generating version selector dropdown menu.
baseUrlstringnullOptional: Version base URL used when generating version selector dropdown menu.
downloadUrlstringnullOptional: Designated URL for downloading OpenAPI specification for versioned.

All versions will automatically inherit sidebarOptions from the parent/base config.

markdownGenerators

markdownGenerators can be configured with the following options:

NameTypeDefaultDescription
createApiPageMDfunctionnullOptional: Returns a string of the raw markdown body for API pages.<br/><br/>Function type: (pageData: ApiPageMetadata) => string
createInfoPageMDfunctionnullOptional: Returns a string of the raw markdown body for info pages.<br/><br/>Function type: (pageData: InfoPageMetadata) => string
createTagPageMDfunctionnullOptional: Returns a string of the raw markdown body for tag pages.<br/><br/>Function type: (pageData: TagPageMetadata) => string
createSchemaPageMDfunctionnullOptional: Returns a string of the raw markdown body for schema pages.<br/><br/>Function type: (pageData: SchemaPageMetadata) => string

sidebarGenerators

sidebarGenerators can be configured with the following options:

NameTypeDefaultDescription
createDocItemfunctionnullOptional: Returns a SidebarItemDoc object containing metadata for a sidebar item.<br/><br/>Function type: (item: ApiPageMetadata | SchemaPageMetadata, context: { sidebarOptions: SidebarOptions; basePath: string }) => SidebarItemDoc

CLI Usage

Usage: docusaurus <command> [options]

Options:
  -V, --version                                            output the version number
  -h, --help                                               display help for command

Commands:
  build [options] [siteDir]                                Build website.
  swizzle [options] [themeName] [componentName] [siteDir]  Wraps or ejects the original theme files into website folder for customization.
  deploy [options] [siteDir]                               Deploy website to GitHub pages.
  start [options] [siteDir]                                Start the development server.
  serve [options] [siteDir]                                Serve website locally.
  clear [siteDir]                                          Remove build artifacts.
  write-translations [options] [siteDir]                   Extract required translations of your site.
  write-heading-ids [options] [siteDir] [files...]         Generate heading ids in Markdown content.
  docs:version <version>                                   Tag a new docs version
  gen-api-docs [options] <id>                              Generates OpenAPI docs in MDX file format and sidebar.ts (if enabled).
  gen-api-docs:version [options] <id:version>              Generates versioned OpenAPI docs in MDX file format, versions.js and sidebar.ts (if
                                                           enabled).
  clean-api-docs [options] <id>                            Clears the generated OpenAPI docs MDX files and sidebar.ts (if enabled).
  clean-api-docs:version [options] <id:version>            Clears the versioned, generated OpenAPI docs MDX files, versions.json and sidebar.ts
                                                           (if enabled)

Generating OpenAPI Docs

To generate all OpenAPI docs, run the following command from the root directory of your project:

yarn docusaurus gen-api-docs all

This will generate API docs for all of the OpenAPI specification (OAS) files referenced in your docusaurus-plugin-openapi-docs config.

You may also generate OpenAPI docs for a single path or OAS by specifying the unique id:

yarn docusaurus gen-api-docs <id>

Example:

yarn docusaurus gen-api-docs petstore

The example above will only generate API docs relative to petstore.

Cleaning API Docs

To clean/remove all API Docs, run the following command from the root directory of your project:

yarn docusaurus clean-api-docs all

You may also remove a particular set of API docs by specifying the unique id of your desired spec instance.

yarn docusaurus clean-api-docs <id>

Example:

yarn docusaurus clean-api-docs petstore

The example above will remove all API docs relative to burgers.

Versioning OpenAPI docs

To generate all versioned OpenAPI docs, run the following command from the root directory of your project:

yarn docusaurus gen-api-docs:version <id>:all

Example:

yarn docusaurus gen-api-docs:version petstore:all

This will generate API docs for all of the OpenAPI specification (OAS) files referenced in your versions config and will also generate a versions.json file.

Substitue all with a specific version ID to generate/clean a specific version. Generating for all or a specific version ID will automatically update the versions.json file.

Developer Quick Start

Looking to make a contribution? Make sure to checkout out our contributing guide.

After forking the main repository, run the following:

git clone https://github.com/<your account>/docusaurus-openapi-docs.git
cd docusaurus-openapi-docs
yarn
yarn build-packages
yarn watch:demo

Credits

Special thanks to @bourdakos1 (Nick Bourdakos), the author of docusaurus-openapi, which this project is heavily based on.

For more insight into why we decided to completely fork see #47

Contributors

<a href="https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/graphs/contributors"> <img src="https://contrib.rocks/image?repo=PaloAltoNetworks/docusaurus-openapi-docs" /> </a>

Support

See SUPPORT.md for our support agreement and guidelines.

If you believe you found a bug or have an idea you'd like to suggest you may report an issue or start a discussion.