diff --git a/src/components/SourceCode/SourceCode.tsx b/src/components/SourceCode/SourceCode.tsx index 12a580db72..9a6e352fa0 100644 --- a/src/components/SourceCode/SourceCode.tsx +++ b/src/components/SourceCode/SourceCode.tsx @@ -9,24 +9,21 @@ export interface SourceCodeProps { lang: string; } -export class SourceCode extends React.PureComponent { - render() { - const { source, lang } = this.props; - return ; - } -} +export const SourceCode = (props: SourceCodeProps) => { + const { source, lang } = props; + return ; +}; -export class SourceCodeWithCopy extends React.Component { - render() { - return ( - - {({ renderCopyButton }) => ( - - {renderCopyButton()} - - - )} - - ); - } -} +export const SourceCodeWithCopy = (props: SourceCodeProps) => { + const { source, lang } = props; + return ( + + {({ renderCopyButton }) => ( + + {renderCopyButton()} + + + )} + + ); +}; diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index 8e7aa3d882..3abbcf9989 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -13,6 +13,7 @@ import { humanizeNumberRange, getContentWithLegacyExamples, getDefinitionName, + langFromMime, } from '../'; import { FieldModel, OpenAPIParser, RedocNormalizedOptions } from '../../services'; @@ -1320,4 +1321,18 @@ describe('Utils', () => { expect(getDefinitionName()).toBeUndefined(); }); }); + + describe('langFromMime', () => { + test('should return correct lang name from content type', () => { + expect(langFromMime('application/xml')).toEqual('xml'); + expect(langFromMime('application/x-xml')).toEqual('xml'); + expect(langFromMime('application/csv')).toEqual('csv'); + expect(langFromMime('application/x-csv')).toEqual('csv'); + expect(langFromMime('text/plain')).toEqual('tex'); + expect(langFromMime('text/x-plain')).toEqual('tex'); + expect(langFromMime('application/plain')).toEqual('tex'); + + expect(langFromMime('text/some-type')).toEqual('clike'); + }); + }); }); diff --git a/src/utils/highlight.ts b/src/utils/highlight.ts index 23cdd43d57..067afcb039 100644 --- a/src/utils/highlight.ts +++ b/src/utils/highlight.ts @@ -21,6 +21,7 @@ import 'prismjs/components/prism-scala.js'; import 'prismjs/components/prism-sql.js'; import 'prismjs/components/prism-swift.js'; import 'prismjs/components/prism-yaml.js'; +import 'prismjs/components/prism-csv.js'; const DEFAULT_LANG = 'clike'; diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 93eeb3d914..2e47399f37 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -395,6 +395,15 @@ export function langFromMime(contentType: string): string { if (contentType.search(/xml/i) !== -1) { return 'xml'; } + + if (contentType.search(/csv/i) !== -1) { + return 'csv'; + } + + if (contentType.search(/plain/i) !== -1) { + return 'tex'; + } + return 'clike'; }