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

SyntaxHighlighter: Expose registerLanguage #23166

Merged
merged 14 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 18 additions & 19 deletions code/ui/blocks/src/components/Source.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import type { ComponentProps, FunctionComponent } from 'react';
import { ThemeProvider, convert, ignoreSsrWarning, styled, themes } from '@storybook/theming';

import React from 'react';
import { styled, ThemeProvider, convert, themes, ignoreSsrWarning } from '@storybook/theming';
import { SyntaxHighlighter } from '@storybook/components';

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

const StyledSyntaxHighlighter: typeof SyntaxHighlighter = styled(SyntaxHighlighter)(
({ theme }) => ({
// DocBlocks-specific styling and overrides
fontSize: `${theme.typography.size.s2 - 1}px`,
lineHeight: '19px',
margin: '25px 0 40px',
borderRadius: theme.appBorderRadius,
boxShadow:
theme.base === 'light'
? 'rgba(0, 0, 0, 0.10) 0 1px 3px 0'
: 'rgba(0, 0, 0, 0.20) 0 2px 5px 0',
'pre.prismjs': {
padding: 20,
background: 'inherit',
},
})
);
const StyledSyntaxHighlighter: React.FunctionComponent<SyntaxHighlighterProps> = styled(
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
SyntaxHighlighter
)(({ theme }) => ({
// DocBlocks-specific styling and overrides
fontSize: `${theme.typography.size.s2 - 1}px`,
lineHeight: '19px',
margin: '25px 0 40px',
borderRadius: theme.appBorderRadius,
boxShadow:
theme.base === 'light' ? 'rgba(0, 0, 0, 0.10) 0 1px 3px 0' : 'rgba(0, 0, 0, 0.20) 0 2px 5px 0',
'pre.prismjs': {
padding: 20,
background: 'inherit',
},
}));

export enum SourceError {
NO_STORY = 'There\u2019s no story here.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
import type { ComponentProps } from 'react';
import React, { Suspense, lazy } from 'react';

const LazySyntaxHighlighter = lazy(() => import('./syntaxhighlighter'));
import type { ComponentProps } from 'react';
import type ReactSyntaxHighlighter from './syntaxhighlighter';

let languages: Parameters<typeof ReactSyntaxHighlighter.registerLanguage>[] = [];
let Comp: typeof ReactSyntaxHighlighter | null = null;

const LazySyntaxHighlighter = lazy(async () => {
const { SyntaxHighlighter } = await import('./syntaxhighlighter');

if (languages.length > 0) {
languages.forEach((args) => {
SyntaxHighlighter.registerLanguage(...args);
});
languages = [];
}

if (Comp === null) Comp = SyntaxHighlighter;

return {
default: (props: ComponentProps<typeof SyntaxHighlighter>) => <SyntaxHighlighter {...props} />,
};
});

const LazySyntaxHighlighterWithFormatter = lazy(async () => {
const [{ SyntaxHighlighter }, { formatter }] = await Promise.all([
import('./syntaxhighlighter'),
import('./formatter'),
]);

if (languages.length > 0) {
languages.forEach((args) => {
SyntaxHighlighter.registerLanguage(...args);
});
languages = [];
}

if (Comp === null) {
Comp = SyntaxHighlighter;
}

return {
default: (props: ComponentProps<typeof LazySyntaxHighlighter>) => (
default: (props: ComponentProps<typeof SyntaxHighlighter>) => (
<SyntaxHighlighter {...props} formatter={formatter} />
),
};
});

export const SyntaxHighlighter = (props: ComponentProps<typeof LazySyntaxHighlighter>) => (
export const SyntaxHighlighter = (
props:
| ComponentProps<typeof LazySyntaxHighlighter>
| ComponentProps<typeof LazySyntaxHighlighterWithFormatter>
) => (
<Suspense fallback={<div />}>
{props.format !== false ? (
<LazySyntaxHighlighterWithFormatter {...props} />
Expand All @@ -24,3 +60,13 @@ export const SyntaxHighlighter = (props: ComponentProps<typeof LazySyntaxHighlig
)}
</Suspense>
);

SyntaxHighlighter.registerLanguage = (
...args: Parameters<typeof ReactSyntaxHighlighter.registerLanguage>
) => {
if (Comp !== null) {
Comp.registerLanguage(...args);
return;
}
languages.push(args);
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ThemeProvider, ensure, themes } from '@storybook/theming';

import type { ComponentProps } from 'react';
import React from 'react';
import { ThemeProvider, themes, ensure } from '@storybook/theming';
import scss from 'react-syntax-highlighter/dist/esm/languages/prism/scss';
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
import { SyntaxHighlighter } from './lazy-syntaxhighlighter';

// Register custom language
SyntaxHighlighter.registerLanguage('scss', scss);
Copy link
Member Author

@ndelangen ndelangen Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be done, as the story renders, a custom render function would allow us to do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Let me try that. 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realize I'm reading too quickly and did this in a custom loader instead of render func in a9d0d22. The loader works, but let me know if you'd prefer I go the render route. Happy to rewrite.


export default {
component: SyntaxHighlighter,
};
Expand Down Expand Up @@ -108,6 +113,18 @@ export const GraphQL = {
},
};

export const CustomSyntax = {
args: {
language: 'scss',
children: `// Custom language syntax registered
div.parent {
div.child {
color: $red;
}
}`,
},
};

export const Unsupported = {
args: {
language: 'C#',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export interface SyntaxHighlighterState {

// copied from @types/react-syntax-highlighter/index.d.ts

export const SyntaxHighlighter: FC<SyntaxHighlighterProps> = ({
export const SyntaxHighlighter = ({
children,
language = 'jsx',
copyable = false,
Expand All @@ -200,7 +200,7 @@ export const SyntaxHighlighter: FC<SyntaxHighlighterProps> = ({
className = null,
showLineNumbers = false,
...rest
}) => {
}: SyntaxHighlighterProps) => {
if (typeof children !== 'string' || !children.trim()) {
return null;
}
Expand Down Expand Up @@ -254,4 +254,8 @@ export const SyntaxHighlighter: FC<SyntaxHighlighterProps> = ({
);
};

SyntaxHighlighter.registerLanguage = (
...args: Parameters<typeof ReactSyntaxHighlighter.registerLanguage>
) => ReactSyntaxHighlighter.registerLanguage(...args);

export default SyntaxHighlighter;