Add options to sort tags,operations and groups alphabetically

This commit is contained in:
rinpatch 2021-02-03 10:33:00 +03:00
parent 755a9095f5
commit 5b37959a73
4 changed files with 49 additions and 2 deletions

View File

@ -253,6 +253,9 @@ You can use all of the following options with standalone version on <redoc> tag
* **function**: A getter function. Must return a number representing the offset (in pixels).
* `showExtensions` - show vendor extensions ("x-" fields). Extensions used by ReDoc are ignored. Can be boolean or an array of `string` with names of extensions to display.
* `sortPropsAlphabetically` - sort properties alphabetically.
* `sortTagsAlphabetically` - sort tags alphabetically.
* `sortOperationsAlphabetically` - sort operations alphabetically.
* `sortTagGroupsAlphabetically` - sort tag groups alphabetically.
* `suppressWarnings` - if set, warnings are not rendered at the top of documentation (they still are logged to the console).
* `payloadSampleIdx` - if set, payload sample will be inserted at this index or last. Indexes start from 0.
* `theme` - ReDoc theme. For details check [theme docs](#redoc-theme-object).

View File

@ -81,7 +81,7 @@ export class AppStore {
MenuStore.updateOnHistory(history.currentId, this.scroll);
this.spec = new SpecStore(spec, specUrl, this.options);
this.menu = new MenuStore(this.spec, this.scroll, history);
this.menu = new MenuStore(this.spec, this.scroll, history, this.options);
if (!this.options.disableSearch) {
this.search = new SearchStore();

View File

@ -8,6 +8,8 @@ import { ScrollService } from './ScrollService';
import { flattenByProp, SECURITY_SCHEMES_SECTION_PREFIX } from '../utils';
import { GROUP_DEPTH } from './MenuBuilder';
import {RedocNormalizedOptions} from "./RedocNormalizedOptions";
export type MenuItemGroupType = 'group' | 'tag' | 'section';
export type MenuItemType = MenuItemGroupType | 'operation';
@ -74,12 +76,45 @@ export class MenuStore {
*
* @param spec [SpecStore](#SpecStore) which contains page content structure
* @param scroll scroll service instance used by this menu
* @param history
* @param options
*/
constructor(spec: SpecStore, public scroll: ScrollService, public history: HistoryService) {
constructor(spec: SpecStore, public scroll: ScrollService, public history: HistoryService, public options: RedocNormalizedOptions) {
makeObservable(this);
const alphaSort = items => items.sort((a, b) => a.name.localeCompare(b.name));
this.items = spec.contentItems;
if (this.options.sortTagGroupsAlphabetically) {
const groups = this.items.filter(item => item.type === "group");
alphaSort(groups);
this.items = this.items.filter(item => item.type !== "group").concat(groups);
}
if (this.options.sortTagsAlphabetically) {
const tags = this.items.filter(item => item.type === "tag");
alphaSort(tags);
this.items = this.items.filter(item => item.type !== "tag").concat(tags);
this.items.map(item => {
if(item.type === "group") {
alphaSort(item.items);
}});
}
if (this.options.sortOperationsAlphabetically) {
this.items.map(item => {
if(item.type === "tag") {
alphaSort(item.items);
} else if (item.type === "group") {
item.items.map(groupItem => {
if(groupItem.type === "tag") {
alphaSort(item.items);
}
});
}
});
}
this.flatItems = flattenByProp(this.items || [], 'items');
this.flatItems.forEach((item, idx) => (item.absoluteIdx = idx));

View File

@ -13,6 +13,9 @@ export interface RedocRawOptions {
requiredPropsFirst?: boolean | string;
sortPropsAlphabetically?: boolean | string;
sortEnumValuesAlphabetically?: boolean | string;
sortTagGroupsAlphabetically?: boolean | string;
sortTagsAlphabetically?: boolean | string;
sortOperationsAlphabetically?: boolean | string;
noAutoAuth?: boolean | string;
nativeScrollbars?: boolean | string;
pathInMiddlePanel?: boolean | string;
@ -170,6 +173,9 @@ export class RedocNormalizedOptions {
requiredPropsFirst: boolean;
sortPropsAlphabetically: boolean;
sortEnumValuesAlphabetically: boolean;
sortTagGroupsAlphabetically: boolean;
sortTagsAlphabetically: boolean;
sortOperationsAlphabetically: boolean;
noAutoAuth: boolean;
nativeScrollbars: boolean;
pathInMiddlePanel: boolean;
@ -227,6 +233,9 @@ export class RedocNormalizedOptions {
this.requiredPropsFirst = argValueToBoolean(raw.requiredPropsFirst);
this.sortPropsAlphabetically = argValueToBoolean(raw.sortPropsAlphabetically);
this.sortEnumValuesAlphabetically = argValueToBoolean(raw.sortEnumValuesAlphabetically);
this.sortTagGroupsAlphabetically = argValueToBoolean(raw.sortTagGroupsAlphabetically);
this.sortTagsAlphabetically = argValueToBoolean(raw.sortTagsAlphabetically);
this.sortOperationsAlphabetically = argValueToBoolean(raw.sortOperationsAlphabetically);
this.noAutoAuth = argValueToBoolean(raw.noAutoAuth);
this.nativeScrollbars = argValueToBoolean(raw.nativeScrollbars);
this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel);