Skip to content
Closed
Changes from all 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,12 +1,27 @@
import type { ComponentProps } from 'react';
import React, { Suspense, lazy } from 'react';

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(() => import('./syntaxhighlighter'));
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'),
Expand All @@ -20,18 +35,20 @@ const LazySyntaxHighlighterWithFormatter = lazy(async () => {
languages = [];
}

if (Comp === null) {
Comp = SyntaxHighlighter;
}
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 Down