Skip to content

Commit

Permalink
feat: support label for x-code-samples
Browse files Browse the repository at this point in the history
fixes #586
  • Loading branch information
RomanHotsiy committed Aug 1, 2018
1 parent 99bef64 commit 00bd966
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/redoc-vendor-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |


Expand Down
9 changes: 6 additions & 3 deletions src/components/RequestSamples/RequestSamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ export class RequestSamples extends React.Component<RequestSamplesProps> {
<Tabs defaultIndex={0}>
<TabList>
{hasBodySample && <Tab key="payload"> Payload </Tab>}
{samples.map(sample => <Tab key={sample.lang}>{sample.lang}</Tab>)}
{samples.map(sample => (
<Tab key={sample.lang}>
{sample.label !== undefined ? sample.label : sample.lang}
</Tab>
))}
</TabList>
{hasBodySample && (
<TabPanel key="payload">
<div>
{' '}
<PayloadSamples content={requestBodyContent!} />{' '}
<PayloadSamples content={requestBodyContent!} />
</div>
</TabPanel>
)}
Expand Down
6 changes: 3 additions & 3 deletions src/services/models/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
*/
Expand Down Expand Up @@ -52,7 +52,7 @@ export class OperationModel implements IMenuItem {
path: string;
servers: OpenAPIServer[];
security: SecurityRequirementModel[];
codeSamples: CodeSample[];
codeSamples: OpenAPIXCodeSample[];

constructor(
private parser: OpenAPIParser,
Expand Down
1 change: 0 additions & 1 deletion src/services/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ export * from './Response';
export * from './Schema';
export * from './Field';
export * from './ApiInfo';
export * from './types';
export * from './SecuritySchemes';
4 changes: 0 additions & 4 deletions src/services/models/types.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/types/open-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface OpenAPIPath {

export interface OpenAPIXCodeSample {
lang: string;
label?: string;
source: string;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down

0 comments on commit 00bd966

Please sign in to comment.