-
Notifications
You must be signed in to change notification settings - Fork 276
Add support for custom sidebar DocItem generators supporting customisation #963
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
Add support for custom sidebar DocItem generators supporting customisation #963
Conversation
…ation Similarly to markdownGenerators, the optional createDocItem() method in sidebarOptions allows users to configure a custom generator function for sidebar items. This allows customisation of class names or custom props, for example, based on each item.
customProps: customProps, | ||
className: className ? className : undefined, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method was extracted from where it was originally because it relied on basePath
and customProps
from higher scope. I've hoisted it and provided those via the context
argument instead.
@@ -5,6 +5,7 @@ | |||
* LICENSE file in the root directory of this source tree. | |||
* ========================================================================== */ | |||
|
|||
import { SidebarItemDoc } from "@docusaurus/plugin-content-docs/src/sidebars/types"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this is the right path to import types from
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it works, since our plugin aims to be 100% compatible with Docusaurus sidebars, i.e. we don't extend the SidebarItemDoc
type.
Visit the preview URL for this PR (updated for commit ee9790c): https://docusaurus-openapi-36b86--pr963-2b6qrebn.web.app (expires Sun, 13 Oct 2024 13:59:06 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: bf293780ee827f578864d92193b8c2866acd459f |
Thanks @robbieaverill! I tested using an My updated petstore plugin config: petstore: {
specPath: "examples/petstore.yaml",
proxy: "https://cors.pan.dev",
outputDir: "docs/petstore",
sidebarOptions: {
groupPathsBy: "tag",
categoryLinkSource: "tag",
createDocItem(
item,
{ sidebarOptions: { customProps }, basePath }
) {
const sidebar_label = item.frontMatter.sidebar_label;
const title = item.title;
const id =
item.type === "schema" ? `schemas/${item.id}` : item.id;
const className =
item.type === "api"
? clsx(
{
"menu__list-item--deprecated": item.api.deprecated,
experimental: item.api["x-experimental"], // checks for existence of extension and adds "experimental" class
"api-method": !!item.api.method,
},
item.api.method
)
: clsx(
{
"menu__list-item--deprecated": item.schema.deprecated,
},
"schema"
);
return {
type: "doc" as const,
id:
basePath === "" || undefined
? `${id}`
: `${basePath}/${id}`,
label: (sidebar_label as string) ?? title ?? id,
customProps: customProps,
className: className ? className : undefined,
};
},
},
template: "api.mustache", // Customize API MDX with mustache template
downloadUrl: "/petstore.yaml",
hideSendButton: false,
showSchemas: true,
} satisfies OpenApiPlugin.Options, My custom CSS: .experimental > .menu__link::after {
content: "🧑🔬";
} Results: ![]() |
What are your thoughts on possibly refactoring the option from petstore: {
specPath: "examples/petstore.yaml",
proxy: "https://cors.pan.dev",
outputDir: "docs/petstore",
sidebarOptions: {
groupPathsBy: "tag",
categoryLinkSource: "tag",
sidebarGenerators: { createDocItem: myCreateDocItem }, // sidebar generators example, where myCreateDocItem is my function
},
template: "api.mustache", // Customize API MDX with mustache template
downloadUrl: "/petstore.yaml",
hideSendButton: false,
showSchemas: true,
} satisfies OpenApiPlugin.Options, |
I think that would be a sensible place for it to live. Are you happy with the way I've put it together, particularly in terms of having the extra context argument? |
@sserrata I've moved |
Thanks for the changes @robbieaverill! Tested and it appears to work as expected. I also really appreciate the documentation. |
@robbieaverill @sserrata Thank you both for adding this feature! It is much better than my original solution and super extensible :) I now have icons!!! ![]() css to get the icons before the method badge:
|
Description
Similarly to markdownGenerators, the optional createDocItem() method in sidebarOptions allows users to configure a custom generator function for sidebar items. This allows customisation of class names or custom props, for example, based on each item.
Motivation and Context
How Has This Been Tested?
I used the demo "petstore versioned" API to define a custom doc item creator function that both adds a custom class name and adds a customProp, and swizzled the component that renders sidebar items to ensure it would pick up the customProp. Details below.
Detailed explanation of testing
In the demo config file I added a custom create method:
And I swizzled the
DocSidebarItem/Link/index.tsx
template (and associated CSS module file) from the class theme and added some lines to render a span for nav items that are marked withmy-custom-prop
incustomProps
:Screenshots (if appropriate)
Types of changes
Checklist