Skip to content

Commit

Permalink
register SyntaxHighlighter languages without formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbes7878 committed Jun 27, 2023
1 parent 9520625 commit 7a7eef0
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
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 @@ -25,13 +41,17 @@ const LazySyntaxHighlighterWithFormatter = lazy(async () => {
}

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

0 comments on commit 7a7eef0

Please sign in to comment.