SaylorTwift's picture
SaylorTwift HF Staff
Add files using upload-large-folder tool
9b906ea verified
Raw
History Blame Contribute Delete
1.73 kB
import React from "react";
import { ExtraProps } from "react-markdown";
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
import { CopyableContentWrapper } from "#/components/shared/buttons/copyable-content-wrapper";
import { cn } from "#/utils/utils";
import { SyntaxHighlighter } from "./syntax-highlighter";
// See https://github.com/remarkjs/react-markdown?tab=readme-ov-file#use-custom-components-syntax-highlight
/**
* Component to render code blocks in markdown.
*/
export function code({
children,
className,
}: React.ClassAttributes<HTMLElement> &
React.HTMLAttributes<HTMLElement> &
ExtraProps) {
const match = /language-(\w+)/.exec(className || ""); // get the language
const codeString = String(children).replace(/\n$/, "");
if (!match) {
const isMultiline = String(children).includes("\n");
if (!isMultiline) {
return (
<code
className={cn(
className,
"bg-surface-raised text-foreground border border-surface-raised rounded px-[0.4em] py-[0.2em]",
)}
>
{children}
</code>
);
}
return (
<CopyableContentWrapper text={codeString}>
<pre className="bg-surface-raised text-foreground border border-surface-raised rounded p-[1em] overflow-auto">
<code className={className}>{codeString}</code>
</pre>
</CopyableContentWrapper>
);
}
return (
<CopyableContentWrapper text={codeString}>
<SyntaxHighlighter
className="rounded-lg"
style={vscDarkPlus}
language={match?.[1]}
PreTag="div"
>
{codeString}
</SyntaxHighlighter>
</CopyableContentWrapper>
);
}