From 00bd966797b69ef07aef7258539516f18513a227 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 1 Aug 2018 10:20:15 +0300 Subject: [PATCH] feat: support label for x-code-samples fixes #586 --- docs/redoc-vendor-extensions.md | 1 + src/components/RequestSamples/RequestSamples.tsx | 9 ++++++--- src/services/models/Operation.ts | 6 +++--- src/services/models/index.ts | 1 - src/services/models/types.ts | 4 ---- src/types/open-api.d.ts | 1 + src/utils/highlight.ts | 1 + 7 files changed, 12 insertions(+), 11 deletions(-) delete mode 100644 src/services/models/types.ts diff --git a/docs/redoc-vendor-extensions.md b/docs/redoc-vendor-extensions.md index 16909bba..0bae2e22 100644 --- a/docs/redoc-vendor-extensions.md +++ b/docs/redoc-vendor-extensions.md @@ -173,6 +173,7 @@ Operation code sample | Field Name | Type | Description | | :---------- | :------: | :----------- | | lang | string | Code sample language. Value should be one of the following [list](https://github.com/github/linguist/blob/master/lib/linguist/popular.yml) | +| label | string? | Code sample label e.g. `Node` or `Python2.7`, _optional_, `lang` will be used by default | | source | string | Code sample source code | diff --git a/src/components/RequestSamples/RequestSamples.tsx b/src/components/RequestSamples/RequestSamples.tsx index 779bae0c..22d4f43a 100644 --- a/src/components/RequestSamples/RequestSamples.tsx +++ b/src/components/RequestSamples/RequestSamples.tsx @@ -29,13 +29,16 @@ export class RequestSamples extends React.Component { {hasBodySample && Payload } - {samples.map(sample => {sample.lang})} + {samples.map(sample => ( + + {sample.label !== undefined ? sample.label : sample.lang} + + ))} {hasBodySample && (
- {' '} - {' '} +
)} diff --git a/src/services/models/Operation.ts b/src/services/models/Operation.ts index 797895e2..c1abcdce 100644 --- a/src/services/models/Operation.ts +++ b/src/services/models/Operation.ts @@ -4,7 +4,7 @@ import { IMenuItem } from '../MenuStore'; import { GroupModel } from './Group.model'; import { SecurityRequirementModel } from './SecurityRequirement'; -import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types'; +import { OpenAPIExternalDocumentation, OpenAPIServer, OpenAPIXCodeSample } from '../../types'; import { getOperationSummary, @@ -22,7 +22,7 @@ import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { FieldModel } from './Field'; import { RequestBodyModel } from './RequestBody'; import { ResponseModel } from './Response'; -import { CodeSample } from './types'; + /** * Operation model ready to be used by components */ @@ -52,7 +52,7 @@ export class OperationModel implements IMenuItem { path: string; servers: OpenAPIServer[]; security: SecurityRequirementModel[]; - codeSamples: CodeSample[]; + codeSamples: OpenAPIXCodeSample[]; constructor( private parser: OpenAPIParser, diff --git a/src/services/models/index.ts b/src/services/models/index.ts index 14f899f8..65006e79 100644 --- a/src/services/models/index.ts +++ b/src/services/models/index.ts @@ -9,5 +9,4 @@ export * from './Response'; export * from './Schema'; export * from './Field'; export * from './ApiInfo'; -export * from './types'; export * from './SecuritySchemes'; diff --git a/src/services/models/types.ts b/src/services/models/types.ts deleted file mode 100644 index f91347f7..00000000 --- a/src/services/models/types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface CodeSample { - lang: string; - source: string; -} diff --git a/src/types/open-api.d.ts b/src/types/open-api.d.ts index 09a77856..2d628736 100644 --- a/src/types/open-api.d.ts +++ b/src/types/open-api.d.ts @@ -59,6 +59,7 @@ export interface OpenAPIPath { export interface OpenAPIXCodeSample { lang: string; + label?: string; source: string; } diff --git a/src/utils/highlight.ts b/src/utils/highlight.ts index 894f003d..a642e25a 100644 --- a/src/utils/highlight.ts +++ b/src/utils/highlight.ts @@ -43,6 +43,7 @@ export function mapLang(lang: string): string { * @return highlighted souce code as **html string** */ export function highlight(source: string, lang: string): string { + lang = lang.toLowerCase(); let grammar = Prism.languages[lang]; if (!grammar) { grammar = Prism.languages[mapLang(lang)];