mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-25 10:03:45 +03:00
chore: refactor + jsxify md tags
This commit is contained in:
parent
8c27d31396
commit
00a0abf4a4
|
@ -38,7 +38,7 @@ info:
|
||||||
OAuth2 - an open protocol to allow secure authorization in a simple
|
OAuth2 - an open protocol to allow secure authorization in a simple
|
||||||
and standard method from web, mobile and desktop applications.
|
and standard method from web, mobile and desktop applications.
|
||||||
|
|
||||||
<security-definitions />
|
<SecurityDefinitions />
|
||||||
|
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
title: Swagger Petstore
|
title: Swagger Petstore
|
||||||
|
@ -78,11 +78,11 @@ tags:
|
||||||
- name: pet_model
|
- name: pet_model
|
||||||
x-displayName: The Pet Model
|
x-displayName: The Pet Model
|
||||||
description: |
|
description: |
|
||||||
<object-description schemaRef="#/components/schemas/Pet" />
|
<ObjectDescription schemaRef="#/components/schemas/Pet" />
|
||||||
- name: store_model
|
- name: store_model
|
||||||
x-displayName: The Order Model
|
x-displayName: The Order Model
|
||||||
description: |
|
description: |
|
||||||
<object-description schemaRef="#/components/schemas/Order" examplesRef="#/components/examples/Order" showReadOnly={true} showWriteOnly={true} />
|
<ObjectDescription schemaRef="#/components/schemas/Order" exampleRef="#/components/examples/Order" showReadOnly={true} showWriteOnly={true} />
|
||||||
x-tagGroups:
|
x-tagGroups:
|
||||||
- name: General
|
- name: General
|
||||||
tags:
|
tags:
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Schema } from '../Schema';
|
|
||||||
|
|
||||||
import { DarkRightPanel, MiddlePanel, Row, Section } from '../../common-elements';
|
import { DarkRightPanel, MiddlePanel, Row, Section } from '../../common-elements';
|
||||||
import { MediaTypeModel, OpenAPIParser, RedocNormalizedOptions } from '../../services';
|
import { MediaTypeModel, OpenAPIParser, RedocNormalizedOptions } from '../../services';
|
||||||
|
import styled from '../../styled-components';
|
||||||
import { OpenAPIMediaType } from '../../types';
|
import { OpenAPIMediaType } from '../../types';
|
||||||
import { MediaTypeSamples } from '../PayloadSamples/MediaTypeSamples';
|
import { MediaTypeSamples } from '../PayloadSamples/MediaTypeSamples';
|
||||||
|
import { Schema } from '../Schema';
|
||||||
|
|
||||||
export interface ObjectDescriptionProps {
|
export interface ObjectDescriptionProps {
|
||||||
schemaRef: string;
|
schemaRef: string;
|
||||||
examplesRef?: string;
|
exampleRef?: string;
|
||||||
showReadOnly?: boolean;
|
showReadOnly?: boolean;
|
||||||
showWriteOnly?: boolean;
|
showWriteOnly?: boolean;
|
||||||
parser: OpenAPIParser;
|
parser: OpenAPIParser;
|
||||||
|
@ -16,7 +17,7 @@ export interface ObjectDescriptionProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ObjectDescription extends React.PureComponent<ObjectDescriptionProps> {
|
export class ObjectDescription extends React.PureComponent<ObjectDescriptionProps> {
|
||||||
private static getMediaType(schemaRef, examplesRef): OpenAPIMediaType {
|
private static getMediaType(schemaRef: string, exampleRef?: string): OpenAPIMediaType {
|
||||||
if (!schemaRef) {
|
if (!schemaRef) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -25,33 +26,28 @@ export class ObjectDescription extends React.PureComponent<ObjectDescriptionProp
|
||||||
schema: { $ref: schemaRef },
|
schema: { $ref: schemaRef },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (examplesRef) {
|
if (exampleRef) {
|
||||||
info.examples = { object: { $ref: examplesRef } };
|
info.examples = { example: { $ref: exampleRef } };
|
||||||
}
|
}
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getMediaModel({
|
private _mediaModel: MediaTypeModel;
|
||||||
schemaRef,
|
|
||||||
examplesRef,
|
|
||||||
parser,
|
|
||||||
options,
|
|
||||||
}: ObjectDescriptionProps) {
|
|
||||||
return new MediaTypeModel(
|
|
||||||
parser,
|
|
||||||
'json',
|
|
||||||
false,
|
|
||||||
ObjectDescription.getMediaType(schemaRef, examplesRef),
|
|
||||||
options,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private mediaModel: MediaTypeModel;
|
private get mediaModel() {
|
||||||
|
const { parser, schemaRef, exampleRef, options } = this.props;
|
||||||
|
if (!this._mediaModel) {
|
||||||
|
this._mediaModel = new MediaTypeModel(
|
||||||
|
parser,
|
||||||
|
'json',
|
||||||
|
false,
|
||||||
|
ObjectDescription.getMediaType(schemaRef, exampleRef),
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
constructor(props: ObjectDescriptionProps) {
|
return this._mediaModel;
|
||||||
super(props);
|
|
||||||
this.mediaModel = ObjectDescription.getMediaModel(this.props);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
@ -63,15 +59,29 @@ export class ObjectDescription extends React.PureComponent<ObjectDescriptionProp
|
||||||
<Schema
|
<Schema
|
||||||
skipWriteOnly={!showWriteOnly}
|
skipWriteOnly={!showWriteOnly}
|
||||||
skipReadOnly={!showReadOnly}
|
skipReadOnly={!showReadOnly}
|
||||||
key="schema"
|
|
||||||
schema={this.mediaModel.schema}
|
schema={this.mediaModel.schema}
|
||||||
/>
|
/>
|
||||||
</MiddlePanel>
|
</MiddlePanel>
|
||||||
<DarkRightPanel>
|
<DarkRightPanel>
|
||||||
<MediaTypeSamples mediaType={this.mediaModel} />
|
<MediaSamplesWrap>
|
||||||
|
<MediaTypeSamples mediaType={this.mediaModel} />
|
||||||
|
</MediaSamplesWrap>
|
||||||
</DarkRightPanel>
|
</DarkRightPanel>
|
||||||
</Row>
|
</Row>
|
||||||
</Section>
|
</Section>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MediaSamplesWrap = styled.div`
|
||||||
|
background: ${({ theme }) => theme.codeSample.backgroundColor};
|
||||||
|
& > div,
|
||||||
|
& > pre {
|
||||||
|
padding: ${props => props.theme.spacing.unit * 4}px;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
& > div > pre {
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
|
@ -13,8 +13,9 @@ import { SearchStore } from './SearchStore';
|
||||||
import { ObjectDescription } from '../components/ObjectDescription/ObjectDescription';
|
import { ObjectDescription } from '../components/ObjectDescription/ObjectDescription';
|
||||||
import { SecurityDefs } from '../components/SecuritySchemes/SecuritySchemes';
|
import { SecurityDefs } from '../components/SecuritySchemes/SecuritySchemes';
|
||||||
import {
|
import {
|
||||||
OBJECT_DEFINTION_COMPONENT_NAME,
|
OBJECT_DEFINTION_JSX_NAME,
|
||||||
SECURITY_DEFINITIONS_COMPONENT_NAME,
|
SECURITY_DEFINITIONS_COMPONENT_NAME,
|
||||||
|
SECURITY_DEFINITIONS_JSX_NAME,
|
||||||
} from '../utils/openapi';
|
} from '../utils/openapi';
|
||||||
|
|
||||||
export interface StoreState {
|
export interface StoreState {
|
||||||
|
@ -155,10 +156,15 @@ const DEFAULT_OPTIONS: RedocRawOptions = {
|
||||||
securitySchemes: store.spec.securitySchemes,
|
securitySchemes: store.spec.securitySchemes,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
[OBJECT_DEFINTION_COMPONENT_NAME]: {
|
[SECURITY_DEFINITIONS_JSX_NAME]: {
|
||||||
component: ObjectDescription,
|
component: SecurityDefs,
|
||||||
propsSelector: (store: AppStore) => ({
|
propsSelector: (store: AppStore) => ({
|
||||||
securitySchemes: store.spec.securitySchemes,
|
securitySchemes: store.spec.securitySchemes,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
[OBJECT_DEFINTION_JSX_NAME]: {
|
||||||
|
component: ObjectDescription,
|
||||||
|
propsSelector: (store: AppStore) => ({
|
||||||
parser: store.spec.parser,
|
parser: store.spec.parser,
|
||||||
options: store.options,
|
options: store.options,
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -59,16 +59,16 @@ export class MenuBuilder {
|
||||||
*/
|
*/
|
||||||
static addMarkdownItems(
|
static addMarkdownItems(
|
||||||
description: string,
|
description: string,
|
||||||
grandparent: GroupModel | undefined,
|
parent: GroupModel | undefined,
|
||||||
initialDepth: number,
|
initialDepth: number,
|
||||||
options: RedocNormalizedOptions,
|
options: RedocNormalizedOptions,
|
||||||
): ContentItemModel[] {
|
): ContentItemModel[] {
|
||||||
const renderer = new MarkdownRenderer(options);
|
const renderer = new MarkdownRenderer(options);
|
||||||
const headings = renderer.extractHeadings(description || '');
|
const headings = renderer.extractHeadings(description || '');
|
||||||
|
|
||||||
const mapHeadingsDeep = (parent, items, depth = 1) =>
|
const mapHeadingsDeep = (_parent, items, depth = 1) =>
|
||||||
items.map(heading => {
|
items.map(heading => {
|
||||||
const group = new GroupModel('section', heading, parent);
|
const group = new GroupModel('section', heading, _parent);
|
||||||
group.depth = depth;
|
group.depth = depth;
|
||||||
if (heading.items) {
|
if (heading.items) {
|
||||||
group.items = mapHeadingsDeep(group, heading.items, depth + 1);
|
group.items = mapHeadingsDeep(group, heading.items, depth + 1);
|
||||||
|
@ -84,7 +84,7 @@ export class MenuBuilder {
|
||||||
return group;
|
return group;
|
||||||
});
|
});
|
||||||
|
|
||||||
return mapHeadingsDeep(grandparent, headings, initialDepth);
|
return mapHeadingsDeep(parent, headings, initialDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -495,8 +495,10 @@ export function normalizeServers(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const OBJECT_DEFINTION_COMPONENT_NAME = 'object-description';
|
|
||||||
export const SECURITY_DEFINITIONS_COMPONENT_NAME = 'security-definitions';
|
export const SECURITY_DEFINITIONS_COMPONENT_NAME = 'security-definitions';
|
||||||
|
export const SECURITY_DEFINITIONS_JSX_NAME = 'SecurityDefinitions';
|
||||||
|
export const OBJECT_DEFINTION_JSX_NAME = 'ObjectDescription';
|
||||||
|
|
||||||
export let SECURITY_SCHEMES_SECTION_PREFIX = 'section/Authentication/';
|
export let SECURITY_SCHEMES_SECTION_PREFIX = 'section/Authentication/';
|
||||||
export function setSecuritySchemePrefix(prefix: string) {
|
export function setSecuritySchemePrefix(prefix: string) {
|
||||||
SECURITY_SCHEMES_SECTION_PREFIX = prefix;
|
SECURITY_SCHEMES_SECTION_PREFIX = prefix;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user