Skip to content

Commit 00bd966

Browse files
committed
feat: support label for x-code-samples
fixes #586
1 parent 99bef64 commit 00bd966

File tree

7 files changed

+12
-11
lines changed

7 files changed

+12
-11
lines changed

docs/redoc-vendor-extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Operation code sample
173173
| Field Name | Type | Description |
174174
| :---------- | :------: | :----------- |
175175
| lang | string | Code sample language. Value should be one of the following [list](https://github.com/github/linguist/blob/master/lib/linguist/popular.yml) |
176+
| label | string? | Code sample label e.g. `Node` or `Python2.7`, _optional_, `lang` will be used by default |
176177
| source | string | Code sample source code |
177178

178179

src/components/RequestSamples/RequestSamples.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ export class RequestSamples extends React.Component<RequestSamplesProps> {
2929
<Tabs defaultIndex={0}>
3030
<TabList>
3131
{hasBodySample && <Tab key="payload"> Payload </Tab>}
32-
{samples.map(sample => <Tab key={sample.lang}>{sample.lang}</Tab>)}
32+
{samples.map(sample => (
33+
<Tab key={sample.lang}>
34+
{sample.label !== undefined ? sample.label : sample.lang}
35+
</Tab>
36+
))}
3337
</TabList>
3438
{hasBodySample && (
3539
<TabPanel key="payload">
3640
<div>
37-
{' '}
38-
<PayloadSamples content={requestBodyContent!} />{' '}
41+
<PayloadSamples content={requestBodyContent!} />
3942
</div>
4043
</TabPanel>
4144
)}

src/services/models/Operation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IMenuItem } from '../MenuStore';
44
import { GroupModel } from './Group.model';
55
import { SecurityRequirementModel } from './SecurityRequirement';
66

7-
import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types';
7+
import { OpenAPIExternalDocumentation, OpenAPIServer, OpenAPIXCodeSample } from '../../types';
88

99
import {
1010
getOperationSummary,
@@ -22,7 +22,7 @@ import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
2222
import { FieldModel } from './Field';
2323
import { RequestBodyModel } from './RequestBody';
2424
import { ResponseModel } from './Response';
25-
import { CodeSample } from './types';
25+
2626
/**
2727
* Operation model ready to be used by components
2828
*/
@@ -52,7 +52,7 @@ export class OperationModel implements IMenuItem {
5252
path: string;
5353
servers: OpenAPIServer[];
5454
security: SecurityRequirementModel[];
55-
codeSamples: CodeSample[];
55+
codeSamples: OpenAPIXCodeSample[];
5656

5757
constructor(
5858
private parser: OpenAPIParser,

src/services/models/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ export * from './Response';
99
export * from './Schema';
1010
export * from './Field';
1111
export * from './ApiInfo';
12-
export * from './types';
1312
export * from './SecuritySchemes';

src/services/models/types.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/types/open-api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface OpenAPIPath {
5959

6060
export interface OpenAPIXCodeSample {
6161
lang: string;
62+
label?: string;
6263
source: string;
6364
}
6465

src/utils/highlight.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function mapLang(lang: string): string {
4343
* @return highlighted souce code as **html string**
4444
*/
4545
export function highlight(source: string, lang: string): string {
46+
lang = lang.toLowerCase();
4647
let grammar = Prism.languages[lang];
4748
if (!grammar) {
4849
grammar = Prism.languages[mapLang(lang)];

0 commit comments

Comments
 (0)