fix: highlight text syntax (#2069)

This commit is contained in:
Alex Varchuk 2022-07-06 17:14:20 +03:00 committed by GitHub
parent f8c30e5e57
commit 4fc6aa0859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 20 deletions

View File

@ -9,24 +9,21 @@ export interface SourceCodeProps {
lang: string; lang: string;
} }
export class SourceCode extends React.PureComponent<SourceCodeProps> { export const SourceCode = (props: SourceCodeProps) => {
render() { const { source, lang } = props;
const { source, lang } = this.props; return <StyledPre dangerouslySetInnerHTML={{ __html: highlight(source, lang) }} />;
return <StyledPre dangerouslySetInnerHTML={{ __html: highlight(source, lang) }} />; };
}
}
export class SourceCodeWithCopy extends React.Component<SourceCodeProps> { export const SourceCodeWithCopy = (props: SourceCodeProps) => {
render() { const { source, lang } = props;
return ( return (
<CopyButtonWrapper data={this.props.source}> <CopyButtonWrapper data={source}>
{({ renderCopyButton }) => ( {({ renderCopyButton }) => (
<SampleControlsWrap> <SampleControlsWrap>
<SampleControls>{renderCopyButton()}</SampleControls> <SampleControls>{renderCopyButton()}</SampleControls>
<SourceCode lang={this.props.lang} source={this.props.source} /> <SourceCode lang={lang} source={source} />
</SampleControlsWrap> </SampleControlsWrap>
)} )}
</CopyButtonWrapper> </CopyButtonWrapper>
); );
} };
}

View File

@ -13,6 +13,7 @@ import {
humanizeNumberRange, humanizeNumberRange,
getContentWithLegacyExamples, getContentWithLegacyExamples,
getDefinitionName, getDefinitionName,
langFromMime,
} from '../'; } from '../';
import { FieldModel, OpenAPIParser, RedocNormalizedOptions } from '../../services'; import { FieldModel, OpenAPIParser, RedocNormalizedOptions } from '../../services';
@ -1320,4 +1321,18 @@ describe('Utils', () => {
expect(getDefinitionName()).toBeUndefined(); 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');
});
});
}); });

View File

@ -21,6 +21,7 @@ import 'prismjs/components/prism-scala.js';
import 'prismjs/components/prism-sql.js'; import 'prismjs/components/prism-sql.js';
import 'prismjs/components/prism-swift.js'; import 'prismjs/components/prism-swift.js';
import 'prismjs/components/prism-yaml.js'; import 'prismjs/components/prism-yaml.js';
import 'prismjs/components/prism-csv.js';
const DEFAULT_LANG = 'clike'; const DEFAULT_LANG = 'clike';

View File

@ -395,6 +395,15 @@ export function langFromMime(contentType: string): string {
if (contentType.search(/xml/i) !== -1) { if (contentType.search(/xml/i) !== -1) {
return 'xml'; return 'xml';
} }
if (contentType.search(/csv/i) !== -1) {
return 'csv';
}
if (contentType.search(/plain/i) !== -1) {
return 'tex';
}
return 'clike'; return 'clike';
} }