Skip to content

feat: add support to info, and tag, and schema custom tamplates #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion demo/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ const config: Config = {
groupPathsBy: "tag",
categoryLinkSource: "tag",
},
template: "api.mustache", // Customize API MDX with mustache template
template: "templates/api.mustache", // Customize API MDX with mustache template
infoTemplate: "templates/info.mustache",
tagTemplate: "templates/tag.mustache",
schemaTemplate: "templates/schema.mustache",
downloadUrl: "/petstore.yaml",
hideSendButton: false,
showSchemas: true,
Expand Down
14 changes: 13 additions & 1 deletion demo/api.mustache → demo/templates/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,16 @@ show_extensions: true
{{/frontMatter.show_extensions}}
---

{{{markdown}}}
This is a Custom Api Page Generated by docusaurus-openapi-docs using `api.mustache` template

<hr />
<br />
<br />

{{{markdown}}}

<br />
<br />
<hr />

This is a Custom Api Page Generated by docusaurus-openapi-docs using `api.mustache` template
22 changes: 22 additions & 0 deletions demo/templates/info.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
id: {{{id}}}
title: "{{{title}}}"
description: "{{{frontMatter.description}}}"
sidebar_label: "{{{title}}}"
hide_title: true
custom_edit_url: null
---

This is a Custom Info Page Generated by docusaurus-openapi-docs using `info.mustache` template

<hr />
<br />
<br />

{{{markdown}}}

<br />
<br />
<hr />

This is a Custom Info Page Generated by docusaurus-openapi-docs using `info.mustache` template
27 changes: 27 additions & 0 deletions demo/templates/schema.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
id: {{{id}}}
title: "{{{title}}}"
description: "{{{frontMatter.description}}}"
sidebar_label: "{{{title}}}"
hide_title: true
{{#schema}}
hide_table_of_contents: true
{{/schema}}
schema: true
sample: {{{frontMatter.sample}}}
custom_edit_url: null
---

This is a Custom Schema Page Generated by docusaurus-openapi-docs using `schema.mustache` template

<hr />
<br />
<br />

{{{markdown}}}

<br />
<br />
<hr />

This is a Custom Schema Page Generated by docusaurus-openapi-docs using `schema.mustache` template
20 changes: 20 additions & 0 deletions demo/templates/tag.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: {{{id}}}
title: "{{{frontMatter.description}}}"
description: "{{{frontMatter.description}}}"
custom_edit_url: null
---

This is a Custom Tag Page Generated by docusaurus-openapi-docs using `tag.mustache` template

<hr />
<br />
<br />

{{{markdown}}}

<br />
<br />
<hr />

This is a Custom Tag Page Generated by docusaurus-openapi-docs using `tag.mustache` template
32 changes: 17 additions & 15 deletions packages/docusaurus-plugin-openapi-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export default function pluginOpenAPIDocs(
specPath,
outputDir,
template,
infoTemplate,
tagTemplate,
schemaTemplate,
markdownGenerators,
downloadUrl,
sidebarOptions,
Expand Down Expand Up @@ -236,7 +239,9 @@ show_extensions: true
{{{markdown}}}
`;

const infoMdTemplate = `---
const infoMdTemplate = infoTemplate
? fs.readFileSync(infoTemplate).toString()
: `---
id: {{{id}}}
title: "{{{title}}}"
description: "{{{frontMatter.description}}}"
Expand All @@ -255,7 +260,9 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
\`\`\`
`;

const tagMdTemplate = `---
const tagMdTemplate = tagTemplate
? fs.readFileSync(tagTemplate).toString()
: `---
id: {{{id}}}
title: "{{{frontMatter.description}}}"
description: "{{{frontMatter.description}}}"
Expand All @@ -272,7 +279,9 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
\`\`\`
`;

const schemaMdTemplate = `---
const schemaMdTemplate = schemaTemplate
? fs.readFileSync(schemaTemplate).toString()
: `---
id: {{{id}}}
title: "{{{title}}}"
description: "{{{frontMatter.description}}}"
Expand Down Expand Up @@ -371,21 +380,14 @@ custom_edit_url: null
}
}

// TODO: determine if we actually want/need this
if (item.type === "info") {
if (!fs.existsSync(`${outputDir}/${item.id}.info.mdx`)) {
try {
sidebarOptions?.categoryLinkSource === "info" // Only use utils template if set to "info"
? fs.writeFileSync(
`${outputDir}/${item.id}.info.mdx`,
utils,
"utf8"
)
: fs.writeFileSync(
`${outputDir}/${item.id}.info.mdx`,
view,
"utf8"
);
fs.writeFileSync(
`${outputDir}/${item.id}.info.mdx`,
utils,
"utf8"
);
console.log(
chalk.green(
`Successfully created "${outputDir}/${item.id}.info.mdx"`
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-plugin-openapi-docs/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const OptionsSchema = Joi.object({
proxy: Joi.string(),
outputDir: Joi.string().required(),
template: Joi.string(),
infoTemplate: Joi.string(),
tagTemplate: Joi.string(),
schemaTemplate: Joi.string(),
downloadUrl: Joi.string(),
hideSendButton: Joi.boolean(),
showExtensions: Joi.boolean(),
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-plugin-openapi-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export interface APIOptions {
specPath: string;
outputDir: string;
template?: string;
infoTemplate?: string;
tagTemplate?: string;
schemaTemplate?: string;
downloadUrl?: string;
hideSendButton?: boolean;
showExtensions?: boolean;
Expand Down