|
| 1 | +<h1 align="center">Docusaurus OpenAPI</h1> |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | + |
| 5 | +OpenAPI plugin for generating API reference docs in Docusaurus v2. |
| 6 | + |
| 7 | +</div> |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +</p> |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Install the preset in your docusaurus project by running: |
| 18 | + |
| 19 | +```sh |
| 20 | +// with npm |
| 21 | +npm install docusaurus-preset-openapi |
| 22 | + |
| 23 | +// with yarn |
| 24 | +yarn add docusaurus-preset-openapi |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +1. Add your OpenAPI definition to the root of your site dir as `openapi.json` (See [Configuration](#Configuration) options below). |
| 30 | + |
| 31 | +2. The OpenAPI preset can be used as a drop-in replacement for `@docusaurus/preset-classic`, replace it as follows: |
| 32 | + |
| 33 | + ```js |
| 34 | + /* docusaurus.config.js */ |
| 35 | + |
| 36 | + { |
| 37 | + presets: [ |
| 38 | + [ |
| 39 | + "docusaurus-preset-openapi", |
| 40 | + { |
| 41 | + // docs: { ... }, |
| 42 | + // blogs: { ... }, |
| 43 | + // theme: { ... }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + ], |
| 47 | + } |
| 48 | + ``` |
| 49 | + |
| 50 | +3. Add a link to the API section of your site by updating `themeConfig.navbar.items`: |
| 51 | + |
| 52 | + ```js |
| 53 | + /* docusaurus.config.js */ |
| 54 | + |
| 55 | + { |
| 56 | + themeConfig: { |
| 57 | + navbar: { |
| 58 | + items: [ |
| 59 | + // ... other items |
| 60 | + { to: "/api", label: "API", position: "left" }, |
| 61 | + // ... other items |
| 62 | + ], |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | +## Configuration |
| 69 | + |
| 70 | +The OpenAPI plugin can be configured with the following options: |
| 71 | + |
| 72 | +| Name | Type | Default | Description | |
| 73 | +| ---------------------------- | --------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------- | |
| 74 | +| `path` | `string` | `'openapi.json'` | Path to OpenAPI definition (`json` or `yaml`) on filesystem relative to site dir. | |
| 75 | +| `routeBasePath` | `string` | `'api'` | URL route for the API section of your site. **DO NOT** include a trailing slash. Use `/` for shipping API without base path. | |
| 76 | +| `sidebarCollapsible` | `boolean` | `true` | Whether sidebar categories are collapsible by default. | |
| 77 | +| `sidebarCollapsed` | `boolean` | `true` | Whether sidebar categories are collapsed by default. | |
| 78 | +| `apiLayoutComponent` | `string` | `'@theme/ApiPage'` | Root Layout component of each API page. | |
| 79 | +| `apiItemComponent` | `string` | `'@theme/ApiItem'` | Main API container, with demo panel, pagination, etc. | |
| 80 | +| `remarkPlugins` | `any[]` | `[]` | Remark plugins passed to MDX. | |
| 81 | +| `rehypePlugins` | `any[]` | `[]` | Rehype plugins passed to MDX. | |
| 82 | +| `beforeDefaultRemarkPlugins` | `any[]` | `[]` | Custom Remark plugins passed to MDX before the default Docusaurus Remark plugins. | |
| 83 | +| `beforeDefaultRehypePlugins` | `any[]` | `[]` | Custom Rehype plugins passed to MDX before the default Docusaurus Rehype plugins. | |
| 84 | + |
| 85 | +Example: |
| 86 | + |
| 87 | +```js |
| 88 | +/* docusaurus.config.js */ |
| 89 | + |
| 90 | +{ |
| 91 | + presets: [ |
| 92 | + [ |
| 93 | + "docusaurus-preset-openapi", |
| 94 | + { |
| 95 | + api: { |
| 96 | + path: "api/cars.yaml" |
| 97 | + routeBasePath: "cars", |
| 98 | + // ... other options |
| 99 | + } |
| 100 | + }, |
| 101 | + ], |
| 102 | + ], |
| 103 | +} |
| 104 | +``` |
0 commit comments