Skip to content

Commit

Permalink
feat: Update version and add badge to sidebar group
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdrumgod committed Mar 25, 2024
1 parent e436005 commit bfb4a14
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 36 deletions.
1 change: 1 addition & 0 deletions docs/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default defineConfig({
rapidocAttrs: {
theme: 'dark',
},
badge: 'The Badge',
},
{
base: 'api/1password',
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starlight-openapi-rapidoc-docs",
"version": "0.7.0",
"version": "0.7.1",
"license": "MIT",
"description": "Starlight plugin to generate documentation from OpenAPI/Swagger specifications + RapiDoc.",
"author": "HiDeoo <[email protected]> (https://hideoo.dev)",
Expand Down
6 changes: 6 additions & 0 deletions docs/src/content/docs/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ Additional styles can be added in `styles/custom.css` file in the same directory

The number of times to repeat the circular reference in the schema.

### `badge`

**Type:** `string` or `{ text: string, variant: 'note' | 'danger' | 'success' | 'caution' | 'tip' | 'default' }`

The badge to display in the sidebar group (top level). Can be a string or an object with `text` and `variant` properties.

## Multiple schemas

You can generate documentation for multiple OpenAPI/Swagger schemas by passing multiple objects to the plugin configuration.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starlight-openapi-rapidoc-monorepo",
"version": "0.7.0",
"version": "0.7.1",
"license": "MIT",
"description": "Starlight plugin to generate documentation from OpenAPI/Swagger specifications + RapiDoc.",
"author": "HiDeoo <[email protected]> (https://hideoo.dev)",
Expand Down
29 changes: 13 additions & 16 deletions packages/starlight-openapi-rapidoc/libs/pathItem.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { getOperationsByTag, getWebhooksOperations } from './operation'
import { getBaseLink } from './path'
import type { Schema } from './schema'
import {
makeSidebarGroup,
makeSidebarLink,
makeSidebarLinkFromPathOperation,
type SidebarManualGroup,
} from './starlight'
import { makeSidebarGroup, makeSidebarLinkFromPathOperation, type SidebarManualGroup } from './starlight'

export function getPathItemSidebarGroups({ config, document }: Schema): SidebarManualGroup['items'] {
const baseLink = getBaseLink(config)
const { showMethodBadgeSidebar } = config
const operations = getOperationsByTag(document)

return [...operations.entries()].map(([tag, operations]) =>
makeSidebarGroup(
tag,
operations.map((operation) => makeSidebarLinkFromPathOperation(operation, { baseLink, showMethodBadgeSidebar })),
config.collapsed,
),
makeSidebarGroup({
...config,
label: tag,
items: operations.map((operation) =>
makeSidebarLinkFromPathOperation(operation, { baseLink, showMethodBadgeSidebar }),
),
}),
)
}

Expand All @@ -31,11 +28,11 @@ export function getWebhooksSidebarGroups({ config, document }: Schema): SidebarM
}

return [
makeSidebarGroup(
'Webhooks',
operations.map(({ slug, title }) => makeSidebarLink(title, baseLink + slug)),
config.collapsed,
),
makeSidebarGroup({
...config,
label: 'Webhooks',
items: operations.map((operation) => makeSidebarLinkFromPathOperation(operation, { baseLink })),
}),
]
}

Expand Down
36 changes: 28 additions & 8 deletions packages/starlight-openapi-rapidoc/libs/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,41 @@ export const SchemaConfigSchema = z.object({
* Number of times to repeat the circular reference in the content
*/
numberOfRepeatCircular: z.number().optional(),

/**
* Badge to be shown in the sidebar
*/
badge: z
.string()
.optional()
.or(
z
.object({
text: z.string(),
variant: z.enum(['note', 'danger', 'success', 'caution', 'tip', 'default']),
})
.optional(),
),
})

export function getSchemaSidebarGroups(schema: Schema): SidebarManualGroup {
const { config, document } = schema
const { badge, ...cleanedConfig } = config

const cleanedSchema = {
...schema,
config: cleanedConfig,
}

return makeSidebarGroup(
config.label ?? document.info.title,
[
return makeSidebarGroup({
...config,
items: [
makeSidebarLink('Overview', getBaseLink(config)),
...getPathItemSidebarGroups(schema),
...getWebhooksSidebarGroups(schema),
...getPathItemSidebarGroups(cleanedSchema),
...getWebhooksSidebarGroups(cleanedSchema),
],
config.collapsed,
config.group,
)
label: config.label ?? document.info.title,
})
}

export type StarlightOpenAPISchemaConfig = z.infer<typeof SchemaConfigSchema>
Expand Down
19 changes: 10 additions & 9 deletions packages/starlight-openapi-rapidoc/libs/starlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ export function getSidebarFromSchemas(
})
}

export function makeSidebarGroup(
label: string,
items: SidebarManualGroup['items'],
collapsed: boolean,
group?: SidebarManualGroup['group'],
): SidebarManualGroup {
return { collapsed, items, label, group }
export function makeSidebarGroup(groupConfig: SidebarManualGroup): SidebarManualGroup {
const { label, items, collapsed = true, group, badge } = groupConfig

return { collapsed, items, label, group, badge }
}

export const chooseBadgeVariant = (method: string) => {
Expand Down Expand Up @@ -161,7 +158,7 @@ export function makeSidebarLink(label: string, link: string): SidebarLink {

export function makeSidebarLinkFromPathOperation(
operation: PathItemOperation,
config: { baseLink: string; showMethodBadgeSidebar: boolean | undefined },
config: { baseLink: string; showMethodBadgeSidebar?: boolean | undefined },
): SidebarLink {
const { slug, title, method } = operation
const { baseLink = '', showMethodBadgeSidebar = false } = config
Expand Down Expand Up @@ -266,10 +263,14 @@ type SidebarGroup =
}

export interface SidebarManualGroup {
collapsed?: boolean
collapsed?: boolean | undefined
items: (SidebarLink | SidebarGroup)[]
label: string
group?: symbol | undefined
badge?:
| string
| { text: string; variant?: 'note' | 'danger' | 'success' | 'caution' | 'tip' | 'default' | undefined }
| undefined
}

interface SidebarLink {
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight-openapi-rapidoc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starlight-openapi-rapidoc",
"version": "0.7.0",
"version": "0.7.1",
"license": "MIT",
"description": "Starlight plugin to generate documentation from OpenAPI/Swagger specifications + RapiDoc..",
"author": "HiDeoo <[email protected]> (https://hideoo.dev)",
Expand Down

0 comments on commit bfb4a14

Please sign in to comment.