docusaurus-openapi-docs

repository·main·Indexed 22 days ago

https://github.com/paloaltonetworks/docusaurus-openapi-docs

A Docusaurus plugin and theme suite that transforms OpenAPI/Swagger 2.0 and 3.x specifications into searchable, styled MDX documentation for Docusaurus v3 sites. It includes the docusaurus-plugin-openapi-docs for MDX generation and docusaurus-theme-openapi-docs for high-quality API reference rendering, featuring fast conversion, Infima styling, and support for single, multi, and micro OpenAPI specifications.

Tokens
41.9K
Snippets
112
Records
153
Agent score
77%

What's inside docusaurus-openapi-docs

  1. Overview of Docusaurus OpenAPI Doc Generator

    main

    The docusaurus-plugin-openapi-docs package extends the Docusaurus CLI to generate MDX documentation from OpenAPI specifications (Swagger 2.0 and OpenAPI 3.x). When used in conjunction with the docusaurus-theme-openapi-docs theme, it produces high-quality, styled API reference documentation.

    Key features include:

    • Fast conversion: Large specs are converted to MDX in seconds.
    • Infima styling: Uses the same styling framework as Docusaurus.
    • Flexibility: Supports single, multi, and micro OpenAPI specifications.
    • Extensibility: Supports common vendor extensions for customization.
  2. Overview of Docusaurus OpenAPI Docs

    main

    The docusaurus-plugin-openapi-docs package extends the Docusaurus CLI with commands to generate MDX files from OpenAPI specifications (Swagger 2.0 and OpenAPI 3.x). When used with the docusaurus-theme-openapi-docs theme, it renders high-quality API reference documentation.

    Key features include:

    • Fast conversion: Converts large specs to MDX in seconds.
    • Flexible: Supports single, multi, and micro OpenAPI specs.
    • Extensible: Supports common vendor extensions to customize documentation UI.
    • Compatible: Integrates with Docusaurus plugin-content-docs.
  3. Customize Category Link pages

    main

    You can control which page is displayed when a sidebar category is clicked using several methods:

    • Generated Index: Use the generated-index feature to create an index of all paths/endpoints available under a specific tag.
    • Tag Descriptions: Use the description field of an OpenAPI tag to define the content displayed when the category is clicked.
    • Spec Info: Use the info section of an OpenAPI specification as the landing page for a category (primarily intended for micro-specs).
  4. Structure of an OpenAPI MDX API documentation page

    main

    In the docusaurus-openapi-docs ecosystem, API documentation is authored using MDX files that leverage specialized theme components to render OpenAPI details.

    An API MDX file typically includes:

    1. Frontmatter: Contains metadata like id, title, and a serialized api string used by the plugin to link the documentation to the OpenAPI specification.
    2. MethodEndpoint: Displays the HTTP method and endpoint path.
    3. ParamsDetails: Renders documentation for query parameters, path parameters, or header parameters.
    4. RequestSchema: Renders the schema for the request body.
    5. StatusCodes: Renders the possible HTTP response status codes and their descriptions.
    6. OperationTabs: (Optional) Used to organize different views of the request/response (e.g., different languages or formats).

    Note that the data for these components is often loaded from external JSON files (e.g., find-pets-by-status.ParamsDetails.json) via require() calls within the MDX file.

    import MethodEndpoint from "@theme/ApiExplorer/MethodEndpoint";
    import ParamsDetails from "@theme/ParamsDetails";
    import RequestSchema from "@theme/RequestSchema";
    import StatusCodes from "@theme/StatusCodes";
    
    <MethodEndpoint
      method={"get"}
      path={`/pet/findByStatus`}
      context={"endpoint"}
    ></MethodEndpoint>
    
    <ParamsDetails
      {...require("./find-pets-by-status.ParamsDetails.json")}
    ></ParamsDetails>
    
    <RequestSchema
      {...require("./find-pets-by-status.RequestSchema.json")}
    ></RequestSchema>
    
    <StatusCodes
      {...require("./find-pets-by-status.StatusCodes.json")}
    ></StatusCodes>
  5. Update existing documentation versions

    main

    Documentation content is managed via specific file paths depending on whether you are editing the current unreleased docs or a previously released version:

    • Unreleased/Next docs: Edit files directly in the docs/ folder (e.g., docs/hello.md updates http://localhost:3000/docs/next/hello).
    • Versioned docs: Edit files within the versioned_docs/ directory (e.g., versioned_docs/version-1.0/hello.md updates http://localhost:3000/docs/hello).
  6. Group API paths by TagGroup

    main

    The plugin supports grouping paths by tagGroup. It uses the first path group as the "group by" value and the path itself as one of the tags under that category.

    To implement this:

    1. Use the x-tagGroups extension in your OpenAPI specification (added to the root OpenAPI object).
    2. If showSchemas: true is configured in the plugin, an additional Schemas category will be created and placed at the end of the sidebar, following all tagGroups categories.
  7. Structure of an API documentation MDX file

    main

    In this project, API documentation is authored using MDX files that leverage specialized React components to render OpenAPI specifications. An API MDX file typically includes:

    • Frontmatter: Contains metadata like id, title, description, and a serialized api string (used by the plugin to resolve the endpoint details).
    • MethodEndpoint: Displays the HTTP method and URL path.
    • ParamsDetails: Renders details about request parameters.
    • RequestSchema: Renders the schema for the request body.
    • StatusCodes: Renders the possible HTTP response status codes.
    • OperationTabs: (Optional) Used to switch between different views like curl or code snippets.

    Example structure:

    import MethodEndpoint from "@theme/ApiExplorer/MethodEndpoint";
    import ParamsDetails from "@theme/ParamsDetails";
    import RequestSchema from "@theme/RequestSchema";
    import StatusCodes from "@theme/StatusCodes";
    
    <MethodEndpoint
      method="post"
      path="/user/createWithArray"
      context="endpoint"
    />
    
    <RequestSchema
      {...require("./create-users-with-array-input.RequestSchema.json")}
    />
    
    <StatusCodes
      {...require("./create-users-with-array-input.StatusCodes.json")}
    />
  8. Understand the `versions.json` metadata file

    main

    The versions.json file contains metadata used for building UI navigation and breadcrumb components.

    • Generation: It is automatically generated and updated every time gen-api-docs:version is executed (for a specific version or for all versions).
    • Deletion: It is only automatically deleted when you run yarn docusaurus clean-api-docs:version <your-api-config-key>:all.
  9. Use MDX and React Components in Markdown

    main

    Because Docusaurus uses MDX, you can import and use React components directly inside your Markdown files. You can also define components within the Markdown file using the export keyword.

    This allows for highly interactive documentation where components can handle state, clicks, or custom styling.

    export const Highlight = ({ children, color }) => (
      <span
        style={{
          backgroundColor: color,
          borderRadius: "20px",
          color: "#fff",
          padding: "10px",
          cursor: "pointer",
        }}
        onClick={() => {
          alert(`You clicked the color ${color} with label ${children}`);
        }}
      >
        {children}
      </span>
    );
    
    This is <Highlight color="#25c2a0">Docusaurus green</Highlight> !
  10. Optimize build performance with externalJsonProps

    main

    The plugin uses externalJsonProps to optimize Docusaurus build times for large OpenAPI specifications.

    How it works

    • externalJsonProps: true (default): Large JSON props (responses, request bodies, parameters) are written to separate .json files and loaded via require(). This bypasses MDX AST processing, allowing the bundler to handle JSON more efficiently.
    • externalJsonProps: false: Large JSON objects are embedded directly in the MDX. The MDX compiler must parse these into an AST and serialize them, which can be slow for deeply nested schemas.

    When to disable

    Set externalJsonProps: false if you have custom tooling that depends on parsing the MDX files directly.

    petstore: {
      specPath: "examples/petstore.yaml",
      outputDir: "docs/petstore",
      externalJsonProps: false, // Disable if needed for custom tooling
    } satisfies OpenApiPlugin.Options,
  11. Group Schemas by x-tags

    main

    The OpenAPI plugin can group schema objects into the same sidebar categories as path objects using the x-tag extension.

    Behavior:

    • When groupPathsBy is set to tag, any schema object containing x-tag will be gathered into the relevant tag's category alongside the paths.
    • Note: If showSchemas is not configured, schema objects with x-tags will still be included in the relevant tag's category sidebar.