Skip to content

Commit

Permalink
Lazy load code highlight (#2067)
Browse files Browse the repository at this point in the history
Most of the markdown doesn't require code highlighting, so I lazy load
the highlighter to make the markdown render faster on the first load.
Also, use async version to lazy load code highlighting by language.
  • Loading branch information
notmd committed Mar 14, 2023
1 parent 389675d commit a1d0d71
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
37 changes: 25 additions & 12 deletions website/src/components/Messages/RenderedCodeblock.tsx
@@ -1,10 +1,15 @@
import { Button, Flex, useToast } from "@chakra-ui/react";
import { Copy, Check } from "lucide-react";
import { useState, MouseEvent } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { Box, Flex, IconButton, useToast } from "@chakra-ui/react";
import { Check, Copy } from "lucide-react";
import { HTMLAttributes, lazy, MouseEvent, Suspense, useState } from "react";

export const RenderedCodeblock = ({ node, inline, className, children, style, ...props }) => {
const SyntaxHighlighter = lazy(() => import("./SyntaxHighlighter"));

export const RenderedCodeblock = ({
inline,
className,
children,
...props
}: HTMLAttributes<HTMLElement> & { inline?: boolean }) => {
const match = /language-(\w+)/.exec(className || "");
const lang = match ? match[1] : "";

Expand Down Expand Up @@ -39,10 +44,18 @@ export const RenderedCodeblock = ({ node, inline, className, children, style, ..

return !inline ? (
<Flex pos="relative" flexDir="row" role="group">
<SyntaxHighlighter style={oneDark} language={lang} {...props}>
{String(children).replace(/\n$/, "")}
</SyntaxHighlighter>
<Button
<Suspense
fallback={
<Box as="pre" className={className} my=".5em" p="1em" bgColor="#282C34 !important" borderRadius="0.3em">
{children}
</Box>
}
>
<SyntaxHighlighter language={lang}>{String(children).replace(/\n$/, "")}</SyntaxHighlighter>
</Suspense>
<IconButton
size="sm"
aria-label="Copy"
onClick={handleCopyClick}
top="0"
pos="absolute"
Expand All @@ -60,8 +73,8 @@ export const RenderedCodeblock = ({ node, inline, className, children, style, ..
}}
colorScheme={isCopied ? "blue" : "gray"}
>
{isCopied ? <Check /> : <Copy />}
</Button>
{isCopied ? <Check size={16} /> : <Copy size={16} />}
</IconButton>
</Flex>
) : (
<code className={className} {...props}>
Expand Down
4 changes: 2 additions & 2 deletions website/src/components/Messages/RenderedMarkdown.tsx
Expand Up @@ -119,7 +119,7 @@ const RenderedMarkdown = ({ markdown }: RenderedMarkdownProps) => {
const linkProps = useMemo(() => {
return {
as: NextLink,
href: link,
href: link!,
target: "_blank",
rel: "noopener noreferrer",
};
Expand All @@ -145,7 +145,7 @@ const RenderedMarkdown = ({ markdown }: RenderedMarkdownProps) => {
<Button variant="ghost" mr={3} onClick={onClose}>
{t("cancel")}
</Button>
<Button colorScheme="blue" as={NextLink} {...linkProps} onClick={onClose}>
<Button colorScheme="blue" {...linkProps} onClick={onClose}>
{t("confirm")}
</Button>
</ModalFooter>
Expand Down
11 changes: 11 additions & 0 deletions website/src/components/Messages/SyntaxHighlighter.tsx
@@ -0,0 +1,11 @@
import type { SyntaxHighlighterProps } from "react-syntax-highlighter";
import { PrismAsyncLight } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";

export default function SyntaxHighlighter({ children, lang, ...props }: SyntaxHighlighterProps) {
return (
<PrismAsyncLight language={lang} {...props} style={oneDark}>
{String(children).replace(/\n$/, "")}
</PrismAsyncLight>
);
}

0 comments on commit a1d0d71

Please sign in to comment.