Skip to content

Commit

Permalink
Add stricter types to the language property of the SyntaxHighlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed May 25, 2023
1 parent 09db9f7 commit 324279b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BuiltInParserName } from 'prettier';
import type { ReactNode } from 'react';
import type { supportedLanguages } from './syntaxhighlighter';

export interface SyntaxHighlighterRendererProps {
rows: any[];
Expand Down Expand Up @@ -30,7 +31,7 @@ export interface SyntaxHighlighterBaseProps {
children?: React.ReactNode;
codeTagProps?: React.HTMLProps<HTMLElement>;
customStyle?: any;
language?: string;
language?: keyof typeof supportedLanguages;
lineNumberStyle?: any;
lineProps?: LineTagPropsFunction | React.HTMLProps<HTMLElement>;
showLineNumbers?: boolean;
Expand Down
28 changes: 17 additions & 11 deletions code/ui/components/src/syntaxhighlighter/syntaxhighlighter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ import type {

const { navigator, document, window: globalWindow } = global;

ReactSyntaxHighlighter.registerLanguage('jsextra', jsExtras);
ReactSyntaxHighlighter.registerLanguage('jsx', jsx);
ReactSyntaxHighlighter.registerLanguage('json', json);
ReactSyntaxHighlighter.registerLanguage('yml', yml);
ReactSyntaxHighlighter.registerLanguage('md', md);
ReactSyntaxHighlighter.registerLanguage('bash', bash);
ReactSyntaxHighlighter.registerLanguage('css', css);
ReactSyntaxHighlighter.registerLanguage('html', html);
ReactSyntaxHighlighter.registerLanguage('tsx', tsx);
ReactSyntaxHighlighter.registerLanguage('typescript', typescript);
ReactSyntaxHighlighter.registerLanguage('graphql', graphql);
export const supportedLanguages = {
jsextra: jsExtras,
jsx,
json,
yml,
md,
bash,
css,
html,
tsx,
typescript,
graphql,
};

Object.entries(supportedLanguages).forEach(([key, val]) => {
ReactSyntaxHighlighter.registerLanguage(key, val);
});

const themedSyntax = memoize(2)((theme) =>
Object.entries(theme.code || {}).reduce((acc, [key, val]) => ({ ...acc, [`* .${key}`]: val }), {})
Expand Down
2 changes: 1 addition & 1 deletion code/ui/components/src/typography/elements/Code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const Code = ({
<StyledSyntaxHighlighter
bordered
copyable
language={language?.[1] ?? 'plaintext'}
language={(language?.[1] as any) ?? 'plaintext'}
format={false}
{...props}
>
Expand Down

0 comments on commit 324279b

Please sign in to comment.