docusaurus-plugin-openapi-docs 0.0.0-379 → 0.0.0-382

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -103,14 +103,24 @@ Here is an example of properly configuring your `docusaurus.config.js` file for
103
103
 
104
104
  > 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`.
105
105
 
106
- ### Plugin Configuration Options
106
+ ## Plugin Configuration Options
107
107
 
108
- `docusaurus-plugin-openapi-docs` can be configured with the following options:
108
+ The `docusaurus-plugin-openapi-docs` plugin can be configured with the following options:
109
+
110
+ | Name | Type | Default | Description |
111
+ | ------------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
112
+ | `id` | `string` | `null` | A unique document id. |
113
+ | `docPluginId` | `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"). |
114
+
115
+ ### config
116
+
117
+ `config` can be configured with the following options:
109
118
 
110
119
  | Name | Type | Default | Description |
111
120
  | ---------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------- |
112
121
  | `specPath` | `string` | `null` | Designated URL or path to the source of an OpenAPI specification file or directory of multiple OpenAPI specification files. |
113
122
  | `ouputDir` | `string` | `null` | Desired output path for generated MDX files. |
123
+ | `ouputDir` | `string` | `null` | Desired output path for generated MDX files. |
114
124
  | `template` | `string` | `null` | _Optional:_ Customize MDX content with a desired template. |
115
125
  | `sidebarOptions` | `object` | `null` | _Optional:_ Set of options for sidebar configuration. See below for a list of supported options. |
116
126
  | `version` | `string` | `null` | _Optional:_ Version assigned to single or micro-spec API specified in `specPath`. |
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { LoadContext, Plugin } from "@docusaurus/types";
2
2
  import type { PluginOptions, LoadedContent } from "./types";
3
3
  export declare function isURL(str: string): boolean;
4
+ export declare function getDocsData(dataArray: any[], filter: string): Object | undefined;
4
5
  declare function pluginOpenAPIDocs(context: LoadContext, options: PluginOptions): Plugin<LoadedContent>;
5
6
  declare namespace pluginOpenAPIDocs {
6
7
  var validateOptions: ({ options, validate }: any) => any;
package/lib/index.js CHANGED
@@ -9,7 +9,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  return (mod && mod.__esModule) ? mod : { "default": mod };
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.isURL = void 0;
12
+ exports.getDocsData = exports.isURL = void 0;
13
13
  const fs_1 = __importDefault(require("fs"));
14
14
  const path_1 = __importDefault(require("path"));
15
15
  const utils_1 = require("@docusaurus/utils");
@@ -23,9 +23,46 @@ function isURL(str) {
23
23
  return /^(https?:)\/\//m.test(str);
24
24
  }
25
25
  exports.isURL = isURL;
26
+ function getDocsData(dataArray, filter) {
27
+ // eslint-disable-next-line array-callback-return
28
+ const filteredData = dataArray.filter((data) => {
29
+ if (data[0] === filter) {
30
+ return data[1];
31
+ }
32
+ // Search plugin-content-docs instances
33
+ if (data[0] === "@docusaurus/plugin-content-docs") {
34
+ const pluginId = data[1].id ? data[1].id : "default";
35
+ if (pluginId === filter) {
36
+ return data[1];
37
+ }
38
+ }
39
+ })[0];
40
+ if (filteredData) {
41
+ // Search presets, e.g. "classic"
42
+ if (filteredData[0] === filter) {
43
+ return filteredData[1].docs;
44
+ }
45
+ // Search plugin-content-docs instances
46
+ if (filteredData[0] === "@docusaurus/plugin-content-docs") {
47
+ const pluginId = filteredData[1].id ? filteredData[1].id : "default";
48
+ if (pluginId === filter) {
49
+ return filteredData[1];
50
+ }
51
+ }
52
+ }
53
+ return;
54
+ }
55
+ exports.getDocsData = getDocsData;
26
56
  function pluginOpenAPIDocs(context, options) {
27
- let { config } = options;
28
- let { siteDir } = context;
57
+ const { config, docPluginId } = options;
58
+ const { siteDir, siteConfig } = context;
59
+ // Get routeBasePath and path from plugin-content-docs or preset
60
+ const presets = siteConfig.presets;
61
+ const plugins = siteConfig.plugins;
62
+ const presetsPlugins = presets.concat(plugins);
63
+ const docData = getDocsData(presetsPlugins, docPluginId);
64
+ const docRouteBasePath = docData ? docData.routeBasePath : undefined;
65
+ const docPath = docData ? (docData.path ? docData.path : "docs") : undefined;
29
66
  async function generateApiDocs(options) {
30
67
  let { specPath, outputDir, template, sidebarOptions } = options;
31
68
  const contentPath = isURL(specPath)
@@ -45,7 +82,7 @@ function pluginOpenAPIDocs(context, options) {
45
82
  }
46
83
  // TODO: figure out better way to set default
47
84
  if (Object.keys(sidebarOptions !== null && sidebarOptions !== void 0 ? sidebarOptions : {}).length > 0) {
48
- const sidebarSlice = (0, sidebars_1.default)(sidebarOptions, options, loadedApi, tags);
85
+ const sidebarSlice = (0, sidebars_1.default)(sidebarOptions, options, loadedApi, tags, docPath);
49
86
  const sidebarSliceTemplate = template
50
87
  ? fs_1.default.readFileSync(template).toString()
51
88
  : `module.exports = {{{slice}}};`;
@@ -135,8 +172,14 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
135
172
  item.markdown = markdown;
136
173
  if (item.type === "api") {
137
174
  item.json = JSON.stringify(item.api);
175
+ let infoBasePath = `${outputDir}/${item.infoId}`;
176
+ if (docRouteBasePath) {
177
+ infoBasePath = `${docRouteBasePath}/${outputDir
178
+ .split(docPath)[1]
179
+ .replace(/^\/+/g, "")}/${item.infoId}`.replace(/^\/+/g, "");
180
+ }
138
181
  if (item.infoId)
139
- item.infoPath = `${outputDir}/${item.infoId}`;
182
+ item.infoPath = infoBasePath;
140
183
  }
141
184
  const view = (0, mustache_1.render)(mdTemplate, item);
142
185
  const utils = (0, mustache_1.render)(infoMdTemplate, item);
package/lib/options.js CHANGED
@@ -9,15 +9,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.OptionsSchema = void 0;
10
10
  const utils_validation_1 = require("@docusaurus/utils-validation");
11
11
  const sidebarOptions = utils_validation_1.Joi.object({
12
- contentDocsPath: utils_validation_1.Joi.string(),
13
12
  groupPathsBy: utils_validation_1.Joi.string().valid("tag"),
14
13
  categoryLinkSource: utils_validation_1.Joi.string().valid("tag", "info"),
15
14
  customProps: utils_validation_1.Joi.object(),
16
15
  sidebarCollapsible: utils_validation_1.Joi.boolean(),
17
16
  sidebarCollapsed: utils_validation_1.Joi.boolean(),
18
- }).with("groupPathsBy", "contentDocsPath");
17
+ });
19
18
  exports.OptionsSchema = utils_validation_1.Joi.object({
20
19
  id: utils_validation_1.Joi.string().required(),
20
+ docPluginId: utils_validation_1.Joi.string().required(),
21
21
  config: utils_validation_1.Joi.object()
22
22
  .pattern(/^/, utils_validation_1.Joi.object({
23
23
  specPath: utils_validation_1.Joi.string().required(),
@@ -1,4 +1,4 @@
1
1
  import { ProcessedSidebar } from "@docusaurus/plugin-content-docs/src/sidebars/types";
2
2
  import { TagObject } from "../openapi/types";
3
3
  import type { SidebarOptions, APIOptions, ApiMetadata } from "../types";
4
- export default function generateSidebarSlice(sidebarOptions: SidebarOptions, options: APIOptions, api: ApiMetadata[], tags: TagObject[]): ProcessedSidebar;
4
+ export default function generateSidebarSlice(sidebarOptions: SidebarOptions, options: APIOptions, api: ApiMetadata[], tags: TagObject[], docPath: string): ProcessedSidebar;
@@ -18,9 +18,9 @@ function isApiItem(item) {
18
18
  function isInfoItem(item) {
19
19
  return item.type === "info";
20
20
  }
21
- function groupByTags(items, sidebarOptions, options, tags) {
21
+ function groupByTags(items, sidebarOptions, options, tags, docPath) {
22
22
  const { outputDir } = options;
23
- const { sidebarCollapsed, sidebarCollapsible, customProps, categoryLinkSource, contentDocsPath, } = sidebarOptions;
23
+ const { sidebarCollapsed, sidebarCollapsible, customProps, categoryLinkSource, } = sidebarOptions;
24
24
  const apiItems = items.filter(isApiItem);
25
25
  const infoItems = items.filter(isInfoItem);
26
26
  const intros = infoItems.map((item) => {
@@ -35,8 +35,8 @@ function groupByTags(items, sidebarOptions, options, tags) {
35
35
  const apiTags = (0, uniq_1.default)(apiItems
36
36
  .flatMap((item) => item.api.tags)
37
37
  .filter((item) => !!item));
38
- const basePath = contentDocsPath
39
- ? outputDir.split(contentDocsPath)[1].replace(/^\/+/g, "")
38
+ const basePath = docPath
39
+ ? outputDir.split(docPath)[1].replace(/^\/+/g, "")
40
40
  : outputDir.slice(outputDir.indexOf("/", 1)).replace(/^\/+/g, "");
41
41
  function createDocItem(item) {
42
42
  var _a, _b;
@@ -135,10 +135,10 @@ function groupByTags(items, sidebarOptions, options, tags) {
135
135
  }
136
136
  return [...tagged, ...untagged];
137
137
  }
138
- function generateSidebarSlice(sidebarOptions, options, api, tags) {
138
+ function generateSidebarSlice(sidebarOptions, options, api, tags, docPath) {
139
139
  let sidebarSlice = [];
140
140
  if (sidebarOptions.groupPathsBy === "tag") {
141
- sidebarSlice = groupByTags(api, sidebarOptions, options, tags);
141
+ sidebarSlice = groupByTags(api, sidebarOptions, options, tags, docPath);
142
142
  }
143
143
  return sidebarSlice;
144
144
  }
package/lib/types.d.ts CHANGED
@@ -3,6 +3,7 @@ import { InfoObject, OperationObject, SecuritySchemeObject, TagObject } from "./
3
3
  export type { PropSidebarItemCategory, SidebarItemLink, PropSidebar, PropSidebarItem, } from "@docusaurus/plugin-content-docs-types";
4
4
  export interface PluginOptions {
5
5
  id?: string;
6
+ docPluginId: string;
6
7
  config: {
7
8
  [key: string]: APIOptions;
8
9
  };
@@ -19,6 +20,15 @@ export interface APIOptions {
19
20
  [key: string]: APIVersionOptions;
20
21
  };
21
22
  }
23
+ export interface SidebarOptions {
24
+ groupPathsBy?: string;
25
+ categoryLinkSource?: string;
26
+ customProps?: {
27
+ [key: string]: unknown;
28
+ };
29
+ sidebarCollapsible?: boolean;
30
+ sidebarCollapsed?: boolean;
31
+ }
22
32
  export interface APIVersionOptions {
23
33
  specPath: string;
24
34
  outputDir: string;
@@ -80,13 +90,3 @@ export interface ApiNavLink {
80
90
  title: string;
81
91
  permalink: string;
82
92
  }
83
- export interface SidebarOptions {
84
- contentDocsPath?: string;
85
- groupPathsBy?: string;
86
- categoryLinkSource?: string;
87
- customProps?: {
88
- [key: string]: unknown;
89
- };
90
- sidebarCollapsible?: boolean;
91
- sidebarCollapsed?: boolean;
92
- }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-openapi-docs",
3
3
  "description": "OpenAPI plugin for Docusaurus.",
4
- "version": "0.0.0-379",
4
+ "version": "0.0.0-382",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -62,5 +62,5 @@
62
62
  "engines": {
63
63
  "node": ">=14"
64
64
  },
65
- "gitHead": "87e5b6e59b66aae31c2f163060b030b232a45d39"
65
+ "gitHead": "b4ff3cdcca84c358bb0b55dd1f9704bc683b25b3"
66
66
  }
package/src/index.ts CHANGED
@@ -23,12 +23,55 @@ export function isURL(str: string): boolean {
23
23
  return /^(https?:)\/\//m.test(str);
24
24
  }
25
25
 
26
+ export function getDocsData(
27
+ dataArray: any[],
28
+ filter: string
29
+ ): Object | undefined {
30
+ // eslint-disable-next-line array-callback-return
31
+ const filteredData = dataArray.filter((data) => {
32
+ if (data[0] === filter) {
33
+ return data[1];
34
+ }
35
+
36
+ // Search plugin-content-docs instances
37
+ if (data[0] === "@docusaurus/plugin-content-docs") {
38
+ const pluginId = data[1].id ? data[1].id : "default";
39
+ if (pluginId === filter) {
40
+ return data[1];
41
+ }
42
+ }
43
+ })[0];
44
+ if (filteredData) {
45
+ // Search presets, e.g. "classic"
46
+ if (filteredData[0] === filter) {
47
+ return filteredData[1].docs;
48
+ }
49
+
50
+ // Search plugin-content-docs instances
51
+ if (filteredData[0] === "@docusaurus/plugin-content-docs") {
52
+ const pluginId = filteredData[1].id ? filteredData[1].id : "default";
53
+ if (pluginId === filter) {
54
+ return filteredData[1];
55
+ }
56
+ }
57
+ }
58
+ return;
59
+ }
60
+
26
61
  export default function pluginOpenAPIDocs(
27
62
  context: LoadContext,
28
63
  options: PluginOptions
29
64
  ): Plugin<LoadedContent> {
30
- let { config } = options;
31
- let { siteDir } = context;
65
+ const { config, docPluginId } = options;
66
+ const { siteDir, siteConfig } = context;
67
+
68
+ // Get routeBasePath and path from plugin-content-docs or preset
69
+ const presets: any = siteConfig.presets;
70
+ const plugins: any = siteConfig.plugins;
71
+ const presetsPlugins = presets.concat(plugins);
72
+ const docData: any = getDocsData(presetsPlugins, docPluginId);
73
+ const docRouteBasePath = docData ? docData.routeBasePath : undefined;
74
+ const docPath = docData ? (docData.path ? docData.path : "docs") : undefined;
32
75
 
33
76
  async function generateApiDocs(options: APIOptions) {
34
77
  let { specPath, outputDir, template, sidebarOptions } = options;
@@ -61,7 +104,8 @@ export default function pluginOpenAPIDocs(
61
104
  sidebarOptions!,
62
105
  options,
63
106
  loadedApi,
64
- tags
107
+ tags,
108
+ docPath
65
109
  );
66
110
 
67
111
  const sidebarSliceTemplate = template
@@ -164,7 +208,13 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
164
208
  item.markdown = markdown;
165
209
  if (item.type === "api") {
166
210
  item.json = JSON.stringify(item.api);
167
- if (item.infoId) item.infoPath = `${outputDir}/${item.infoId}`;
211
+ let infoBasePath = `${outputDir}/${item.infoId}`;
212
+ if (docRouteBasePath) {
213
+ infoBasePath = `${docRouteBasePath}/${outputDir
214
+ .split(docPath!)[1]
215
+ .replace(/^\/+/g, "")}/${item.infoId}`.replace(/^\/+/g, "");
216
+ }
217
+ if (item.infoId) item.infoPath = infoBasePath;
168
218
  }
169
219
 
170
220
  const view = render(mdTemplate, item);
package/src/options.ts CHANGED
@@ -8,16 +8,16 @@
8
8
  import { Joi } from "@docusaurus/utils-validation";
9
9
 
10
10
  const sidebarOptions = Joi.object({
11
- contentDocsPath: Joi.string(),
12
11
  groupPathsBy: Joi.string().valid("tag"),
13
12
  categoryLinkSource: Joi.string().valid("tag", "info"),
14
13
  customProps: Joi.object(),
15
14
  sidebarCollapsible: Joi.boolean(),
16
15
  sidebarCollapsed: Joi.boolean(),
17
- }).with("groupPathsBy", "contentDocsPath");
16
+ });
18
17
 
19
18
  export const OptionsSchema = Joi.object({
20
19
  id: Joi.string().required(),
20
+ docPluginId: Joi.string().required(),
21
21
  config: Joi.object()
22
22
  .pattern(
23
23
  /^/,
@@ -35,7 +35,8 @@ function groupByTags(
35
35
  items: ApiPageMetadata[],
36
36
  sidebarOptions: SidebarOptions,
37
37
  options: APIOptions,
38
- tags: TagObject[]
38
+ tags: TagObject[],
39
+ docPath: string
39
40
  ): ProcessedSidebar {
40
41
  const { outputDir } = options;
41
42
  const {
@@ -43,7 +44,6 @@ function groupByTags(
43
44
  sidebarCollapsible,
44
45
  customProps,
45
46
  categoryLinkSource,
46
- contentDocsPath,
47
47
  } = sidebarOptions;
48
48
 
49
49
  const apiItems = items.filter(isApiItem);
@@ -64,8 +64,8 @@ function groupByTags(
64
64
  .filter((item): item is string => !!item)
65
65
  );
66
66
 
67
- const basePath = contentDocsPath
68
- ? outputDir.split(contentDocsPath!)[1].replace(/^\/+/g, "")
67
+ const basePath = docPath
68
+ ? outputDir.split(docPath!)[1].replace(/^\/+/g, "")
69
69
  : outputDir.slice(outputDir.indexOf("/", 1)).replace(/^\/+/g, "");
70
70
 
71
71
  function createDocItem(item: ApiPageMetadata): SidebarItemDoc {
@@ -184,7 +184,8 @@ export default function generateSidebarSlice(
184
184
  sidebarOptions: SidebarOptions,
185
185
  options: APIOptions,
186
186
  api: ApiMetadata[],
187
- tags: TagObject[]
187
+ tags: TagObject[],
188
+ docPath: string
188
189
  ) {
189
190
  let sidebarSlice: ProcessedSidebar = [];
190
191
  if (sidebarOptions.groupPathsBy === "tag") {
@@ -192,7 +193,8 @@ export default function generateSidebarSlice(
192
193
  api as ApiPageMetadata[],
193
194
  sidebarOptions,
194
195
  options,
195
- tags
196
+ tags,
197
+ docPath
196
198
  );
197
199
  }
198
200
  return sidebarSlice;
package/src/types.ts CHANGED
@@ -22,6 +22,7 @@ export type {
22
22
  } from "@docusaurus/plugin-content-docs-types";
23
23
  export interface PluginOptions {
24
24
  id?: string;
25
+ docPluginId: string;
25
26
  config: {
26
27
  [key: string]: APIOptions;
27
28
  };
@@ -40,6 +41,14 @@ export interface APIOptions {
40
41
  };
41
42
  }
42
43
 
44
+ export interface SidebarOptions {
45
+ groupPathsBy?: string;
46
+ categoryLinkSource?: string;
47
+ customProps?: { [key: string]: unknown };
48
+ sidebarCollapsible?: boolean;
49
+ sidebarCollapsed?: boolean;
50
+ }
51
+
43
52
  export interface APIVersionOptions {
44
53
  specPath: string;
45
54
  outputDir: string;
@@ -112,12 +121,3 @@ export interface ApiNavLink {
112
121
  title: string;
113
122
  permalink: string;
114
123
  }
115
-
116
- export interface SidebarOptions {
117
- contentDocsPath?: string;
118
- groupPathsBy?: string;
119
- categoryLinkSource?: string;
120
- customProps?: { [key: string]: unknown };
121
- sidebarCollapsible?: boolean;
122
- sidebarCollapsed?: boolean;
123
- }