Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Add stricter types to the language property of the SyntaxHighlighter #22790

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Story, Meta } from '@storybook/addon-docs';

<Story name="plaintext">Plain text</Story>;
<Story name="text">Plain text</Story>;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export default {};
export const Plaintext = () => 'Plain text';

Plaintext.story = {
name: 'plaintext',
name: 'text',
};"
`;
3 changes: 2 additions & 1 deletion code/ui/blocks/src/blocks/mdx.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FC, MouseEvent, PropsWithChildren, SyntheticEvent } from 'react';
import React, { useContext } from 'react';
import { NAVIGATE_URL } from '@storybook/core-events';
import type { SupportedLanguage } from '@storybook/components';
import { Code, components, nameSpaceClassNames } from '@storybook/components';
import { global } from '@storybook/global';
import { styled } from '@storybook/theming';
Expand Down Expand Up @@ -48,7 +49,7 @@ export const CodeOrSourceMdx: FC<PropsWithChildren<CodeOrSourceMdxProps>> = ({
const language = className && className.split('-');
return (
<Source
language={(language && language[1]) || 'plaintext'}
language={((language && language[1]) as SupportedLanguage) || 'text'}
format={false}
code={children as string}
{...rest}
Expand Down
4 changes: 2 additions & 2 deletions code/ui/blocks/src/components/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
useTheme,
} from '@storybook/theming';

import type { SupportedLanguage, SyntaxHighlighterProps } from '@storybook/components';
import { SyntaxHighlighter } from '@storybook/components';
import type { SyntaxHighlighterProps } from '@storybook/components';
import { EmptyBlock } from './EmptyBlock';

const StyledSyntaxHighlighter: React.FunctionComponent<SyntaxHighlighterProps> = styled(
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface SourceCodeProps {
/**
* The language the syntax highlighter uses for your story’s code
*/
language?: string;
language?: SupportedLanguage;
/**
* Use this to override the content of the source block.
*/
Expand Down
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 All @@ -26,11 +27,14 @@ export type SyntaxHighlighterFormatTypes = boolean | 'dedent' | BuiltInParserNam
// the reason these a COPIED is the types for this package are defining modules by filename
// which will not match one we've localized type-definitions
type LineTagPropsFunction = (lineNumber: number) => React.HTMLProps<HTMLElement>;

export type SupportedLanguage = 'text' | keyof typeof supportedLanguages;

export interface SyntaxHighlighterBaseProps {
children?: React.ReactNode;
codeTagProps?: React.HTMLProps<HTMLElement>;
customStyle?: any;
language?: string;
language?: SupportedLanguage;
lineNumberStyle?: any;
lineProps?: LineTagPropsFunction | React.HTMLProps<HTMLElement>;
showLineNumbers?: boolean;
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { Children } from 'react';
import { SyntaxHighlighter } from '../../syntaxhighlighter/syntaxhighlighter';
import { isReactChildString } from '../lib/isReactChildString';
import { codeCommon } from '../lib/common';
import type { SupportedLanguage } from '../../syntaxhighlighter/syntaxhighlighter-types';

const isInlineCodeRegex = /[\n\r]/g;

Expand Down Expand Up @@ -60,7 +61,7 @@ export const Code = ({
<StyledSyntaxHighlighter
bordered
copyable
language={language?.[1] ?? 'plaintext'}
language={(language?.[1] as SupportedLanguage) ?? 'text'}
format={false}
{...props}
>
Expand Down
1 change: 1 addition & 0 deletions code/ui/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type {
SyntaxHighlighterFormatTypes,
SyntaxHighlighterProps,
SyntaxHighlighterRendererProps,
SupportedLanguage,
} from './components/syntaxhighlighter/syntaxhighlighter-types';
export { SyntaxHighlighter } from './components/syntaxhighlighter/lazy-syntaxhighlighter';
export { createCopyToClipboardFunction } from './components/syntaxhighlighter/syntaxhighlighter';
Expand Down
Loading