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;
}
export class SourceCode extends React.PureComponent<SourceCodeProps> {
render() {
const { source, lang } = this.props;
return <StyledPre dangerouslySetInnerHTML={{ __html: highlight(source, lang) }} />;
}
}
export const SourceCode = (props: SourceCodeProps) => {
const { source, lang } = props;
return <StyledPre dangerouslySetInnerHTML={{ __html: highlight(source, lang) }} />;
};
export class SourceCodeWithCopy extends React.Component<SourceCodeProps> {
render() {
return (
<CopyButtonWrapper data={this.props.source}>
{({ renderCopyButton }) => (
<SampleControlsWrap>
<SampleControls>{renderCopyButton()}</SampleControls>
<SourceCode lang={this.props.lang} source={this.props.source} />
</SampleControlsWrap>
)}
</CopyButtonWrapper>
);
}
}
export const SourceCodeWithCopy = (props: SourceCodeProps) => {
const { source, lang } = props;
return (
<CopyButtonWrapper data={source}>
{({ renderCopyButton }) => (
<SampleControlsWrap>
<SampleControls>{renderCopyButton()}</SampleControls>
<SourceCode lang={lang} source={source} />
</SampleControlsWrap>
)}
</CopyButtonWrapper>
);
};

View File

@ -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');
});
});
});

View File

@ -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';

View File

@ -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';
}