From 2e4663b3b7022f25d3dc808afbcb3b3ad9483c41 Mon Sep 17 00:00:00 2001 From: Junghyun Colin Kim Date: Wed, 8 Dec 2021 22:53:14 +0900 Subject: [PATCH] Use an option usePathInSidebar (#1805) * Use an option usePathInSidebar If this option is enabled, it displays a path in the sidebar instead of summary. Usage: ```js { ) : ( - - {item.name} + + {item.sidebarLabel} {this.props.children} {(item.depth > 0 && item.items.length > 0 && ( @@ -96,7 +96,7 @@ export class OperationMenuItemContent extends React.Component{shortenHTTPVerb(item.httpVerb)} )} - {item.name} + {item.sidebarLabel} {this.props.children} diff --git a/src/services/MenuStore.ts b/src/services/MenuStore.ts index 65f6c7c8..70fb45b0 100644 --- a/src/services/MenuStore.ts +++ b/src/services/MenuStore.ts @@ -16,6 +16,7 @@ export interface IMenuItem { id: string; absoluteIdx?: number; name: string; + sidebarLabel: string; description?: string; depth: number; active: boolean; diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index fcba8784..f1640b92 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -5,6 +5,11 @@ import { isNumeric, mergeObjects } from '../utils/helpers'; import { LabelsConfigRaw, setRedocLabels } from './Labels'; import { MDXComponentMeta } from './MarkdownRenderer'; +export enum SideNavStyleEnum { + SummaryOnly = 'summary-only', + PathOnly = 'path-only', +} + export interface RedocRawOptions { theme?: ThemeInterface; scrollYOffset?: number | string | (() => number); @@ -22,6 +27,7 @@ export interface RedocRawOptions { disableSearch?: boolean | string; onlyRequiredInSamples?: boolean | string; showExtensions?: boolean | string | string[]; + sideNavStyle?: SideNavStyleEnum; hideSingleRequestSampleTab?: boolean | string; menuToggle?: boolean | string; jsonSampleExpandLevel?: number | string | 'all'; @@ -142,6 +148,22 @@ export class RedocNormalizedOptions { } } + static normalizeSideNavStyle(value: RedocRawOptions['sideNavStyle']): SideNavStyleEnum { + const defaultValue = SideNavStyleEnum.SummaryOnly; + if (typeof value !== 'string') { + return defaultValue; + } + + switch (value) { + case defaultValue: + return value; + case SideNavStyleEnum.PathOnly: + return SideNavStyleEnum.PathOnly; + default: + return defaultValue; + } + } + static normalizePayloadSampleIdx(value: RedocRawOptions['payloadSampleIdx']): number { if (typeof value === 'number') { return Math.max(0, value); // always greater or equal than 0 @@ -189,6 +211,7 @@ export class RedocNormalizedOptions { disableSearch: boolean; onlyRequiredInSamples: boolean; showExtensions: boolean | string[]; + sideNavStyle: SideNavStyleEnum; hideSingleRequestSampleTab: boolean; menuToggle: boolean; jsonSampleExpandLevel: number; @@ -247,6 +270,7 @@ export class RedocNormalizedOptions { this.disableSearch = argValueToBoolean(raw.disableSearch); this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples); this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions); + this.sideNavStyle = RedocNormalizedOptions.normalizeSideNavStyle(raw.sideNavStyle); this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab); this.menuToggle = argValueToBoolean(raw.menuToggle, true); this.jsonSampleExpandLevel = RedocNormalizedOptions.normalizeJsonSampleExpandLevel( diff --git a/src/services/models/Group.model.ts b/src/services/models/Group.model.ts index f1009f4e..8a9429b2 100644 --- a/src/services/models/Group.model.ts +++ b/src/services/models/Group.model.ts @@ -14,6 +14,7 @@ export class GroupModel implements IMenuItem { id: string; absoluteIdx?: number; name: string; + sidebarLabel: string; description?: string; type: MenuItemGroupType; @@ -43,6 +44,8 @@ export class GroupModel implements IMenuItem { this.name = tagOrGroup['x-displayName'] || tagOrGroup.name; this.level = (tagOrGroup as MarkdownHeading).level || 1; + this.sidebarLabel = this.name; + // remove sections from markdown, same as in ApiInfo this.description = tagOrGroup.description || ''; diff --git a/src/services/models/Operation.ts b/src/services/models/Operation.ts index 261bb78b..8e7e9fe5 100644 --- a/src/services/models/Operation.ts +++ b/src/services/models/Operation.ts @@ -25,6 +25,7 @@ import { FieldModel } from './Field'; import { MediaContentModel } from './MediaContent'; import { RequestBodyModel } from './RequestBody'; import { ResponseModel } from './Response'; +import { SideNavStyleEnum } from '../RedocNormalizedOptions'; export interface XPayloadSample { lang: 'payload'; @@ -49,6 +50,7 @@ export class OperationModel implements IMenuItem { id: string; absoluteIdx?: number; name: string; + sidebarLabel: string; description?: string; type = 'operation' as const; @@ -104,6 +106,8 @@ export class OperationModel implements IMenuItem { this.name = getOperationSummary(operationSpec); + this.sidebarLabel = options.sideNavStyle === SideNavStyleEnum.PathOnly ? this.path : this.name; + if (this.isCallback) { // NOTE: Callbacks by default should not inherit the specification's global `security` definition. // Can be defined individually per-callback in the specification. Defaults to none.