Skip to content

Commit c6acf9e

Browse files
committed
add markdownGenerators example and update docs
1 parent 19f5ed5 commit c6acf9e

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

demo/customMdGenerators.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { createAuthorization } from "docusaurus-plugin-openapi-docs/lib/markdown/createAuthorization";
2+
import { createCallbacks } from "docusaurus-plugin-openapi-docs/lib/markdown/createCallbacks";
3+
import { createDeprecationNotice } from "docusaurus-plugin-openapi-docs/lib/markdown/createDeprecationNotice";
4+
import { createDescription } from "docusaurus-plugin-openapi-docs/lib/markdown/createDescription";
5+
import { createHeading } from "docusaurus-plugin-openapi-docs/lib/markdown/createHeading";
6+
import { createMethodEndpoint } from "docusaurus-plugin-openapi-docs/lib/markdown/createMethodEndpoint";
7+
import { createParamsDetails } from "docusaurus-plugin-openapi-docs/lib/markdown/createParamsDetails";
8+
import { createRequestBodyDetails } from "docusaurus-plugin-openapi-docs/lib/markdown/createRequestBodyDetails";
9+
import { createRequestHeader } from "docusaurus-plugin-openapi-docs/lib/markdown/createRequestHeader";
10+
import { createStatusCodes } from "docusaurus-plugin-openapi-docs/lib/markdown/createStatusCodes";
11+
import { createVendorExtensions } from "docusaurus-plugin-openapi-docs/lib/markdown/createVendorExtensions";
12+
import { render } from "docusaurus-plugin-openapi-docs/lib/markdown/utils";
13+
14+
function createServersTable(servers: any[]) {
15+
if (servers.length) {
16+
return `| URL | Description |
17+
| ---- | ----- |
18+
${servers
19+
.map((server) => {
20+
return `| ${server.url} | ${server.description} | `.replace("\n", "<br/>");
21+
})
22+
.join("\n")}
23+
`;
24+
}
25+
}
26+
27+
export function myCustomApiMdGenerator({
28+
title,
29+
api: {
30+
deprecated,
31+
"x-deprecated-description": deprecatedDescription,
32+
description,
33+
method,
34+
path,
35+
extensions,
36+
parameters,
37+
requestBody,
38+
responses,
39+
callbacks,
40+
servers, // destructure servers here
41+
},
42+
infoPath,
43+
frontMatter,
44+
}: ApiPageMetadata) {
45+
return render([
46+
`import ApiTabs from "@theme/ApiTabs";\n`,
47+
`import DiscriminatorTabs from "@theme/DiscriminatorTabs";\n`,
48+
`import MethodEndpoint from "@theme/ApiExplorer/MethodEndpoint";\n`,
49+
`import SecuritySchemes from "@theme/ApiExplorer/SecuritySchemes";\n`,
50+
`import MimeTabs from "@theme/MimeTabs";\n`,
51+
`import ParamsItem from "@theme/ParamsItem";\n`,
52+
`import ResponseSamples from "@theme/ResponseSamples";\n`,
53+
`import SchemaItem from "@theme/SchemaItem";\n`,
54+
`import SchemaTabs from "@theme/SchemaTabs";\n`,
55+
`import Heading from "@theme/Heading";\n`,
56+
`import OperationTabs from "@theme/OperationTabs";\n`,
57+
`import TabItem from "@theme/TabItem";\n\n`,
58+
createHeading(title),
59+
createMethodEndpoint(method, path),
60+
createServersTable(servers),
61+
infoPath && createAuthorization(infoPath),
62+
frontMatter.show_extensions
63+
? createVendorExtensions(extensions)
64+
: undefined,
65+
createDeprecationNotice({ deprecated, description: deprecatedDescription }),
66+
createDescription(description),
67+
requestBody || parameters ? createRequestHeader("Request") : undefined,
68+
createParamsDetails({ parameters, type: "path" }),
69+
createParamsDetails({ parameters, type: "query" }),
70+
createParamsDetails({ parameters, type: "header" }),
71+
createParamsDetails({ parameters, type: "cookie" }),
72+
createRequestBodyDetails({
73+
title: "Body",
74+
body: requestBody,
75+
} as RequestBodyProps),
76+
createStatusCodes({ responses }),
77+
createCallbacks({ callbacks }),
78+
]);
79+
}

demo/docs/intro.mdx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ The `docusaurus-plugin-openapi-docs` plugin can be configured with the following
197197
| `label` | `string` | `null` | _Optional:_ Version label used when generating version selector dropdown menu. |
198198
| `baseUrl` | `string` | `null` | _Optional:_ Version base URL used when generating version selector dropdown menu. |
199199
| `versions` | `object` | `null` | _Optional:_ Set of options for versioning configuration. See below for a list of supported options. |
200-
| `markdownGenerators` | `object` | `null` | _Optional:_ Customize MDX content with a set of options for specifying markdown generator functions. See below for a list of supported options. |
200+
| `markdownGenerators` | `object` | `null` | _Optional:_ Customize MDX content with a set of options for specifying markdown generator functions. See [markdownGenerators](#markdowngenerators) for a list of supported options. |
201201
| `showSchemas` | `boolean` | `null` | _Optional:_ If set to `true`, generates schema pages and adds them to the sidebar. |
202202

203203
`sidebarOptions` can be configured with the following options:
@@ -234,6 +234,24 @@ The `docusaurus-plugin-openapi-docs` plugin can be configured with the following
234234
| `createTagPageMD` | `function` | `null` | _Optional:_ Returns a string of the raw markdown body for tag pages.<br/><br/>**Function type:** `(pageData: TagPageMetadata) => string` |
235235
| `createSchemaPageMD` | `function` | `null` | _Optional:_ Returns a string of the raw markdown body for schema pages.<br/><br/>**Function type:** `(pageData: SchemaPageMetadata) => string` |
236236

237+
Example:
238+
239+
```javascript
240+
petstore31: {
241+
specPath: "examples/petstore-3.1.yaml",
242+
proxy: "https://cors.pan.dev",
243+
outputDir: "docs/petstore31",
244+
sidebarOptions: {
245+
groupPathsBy: "tag",
246+
categoryLinkSource: "tag",
247+
},
248+
downloadUrl: "/petstore-3.1.yaml",
249+
hideSendButton: false,
250+
showSchemas: true,
251+
markdownGenerators: { createApiPageMD: myCustomApiMdGenerator }, // customize MDX with markdown generator
252+
} satisfies OpenApiPlugin.Options,
253+
```
254+
237255
## CLI Usage
238256

239257
After you've installed and configured the plugin and theme, the CLI can be used to `generate` and `clean` API docs.

demo/docusaurus.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type * as Plugin from "@docusaurus/types/src/plugin";
44
import type * as OpenApiPlugin from "docusaurus-plugin-openapi-docs";
55

66
import { DOCUSAURUS_VERSION } from "@docusaurus/utils";
7+
import { myCustomApiMdGenerator } from "./customMdGenerators";
78

89
const config: Config = {
910
title: "Docusaurus OpenAPI Docs",
@@ -305,10 +306,10 @@ const config: Config = {
305306
groupPathsBy: "tag",
306307
categoryLinkSource: "tag",
307308
},
308-
template: "api.mustache", // Customize API MDX with mustache template
309309
downloadUrl: "/petstore-3.1.yaml",
310310
hideSendButton: false,
311311
showSchemas: true,
312+
markdownGenerators: { createApiPageMD: myCustomApiMdGenerator }, // customize MDX with markdown generator
312313
} satisfies OpenApiPlugin.Options,
313314
cos: {
314315
specPath: "examples/openapi-cos.json",

0 commit comments

Comments
 (0)