|
1 | 1 | import * as React from "react";
|
2 | 2 | import { codeToHtml } from "shiki";
|
3 | 3 | import { CopyButton } from "@/components/site/copy-button";
|
| 4 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 5 | +import { cn } from "@/lib/utils"; |
4 | 6 |
|
5 | 7 | async function highlightMarkdownCode(code: string, language: string) {
|
6 | 8 | const html = await codeToHtml(code, {
|
@@ -43,21 +45,87 @@ const CodeBlock = ({
|
43 | 45 |
|
44 | 46 | const language = className?.replace("language-", "") || "";
|
45 | 47 | const code = codeString.trim();
|
46 |
| - const highlightedCodePromise = highlightMarkdownCode(code, language); |
47 |
| - const highlightedCode = React.use(highlightedCodePromise); |
48 |
| - |
49 |
| - return ( |
50 |
| - <pre |
51 |
| - className="not-prose relative mb-6 mt-4 rounded-md px-4 py-3" |
52 |
| - style={{ backgroundColor: "#0d1117" }} |
53 |
| - > |
54 |
| - <CopyButton code={code} /> |
55 |
| - <code |
56 |
| - dangerouslySetInnerHTML={{ __html: highlightedCode }} |
57 |
| - className="block overflow-auto" |
58 |
| - /> |
59 |
| - </pre> |
60 |
| - ); |
| 48 | + |
| 49 | + const CoreCodeBlock = ({ |
| 50 | + className, |
| 51 | + updatedCode, |
| 52 | + }: { |
| 53 | + updatedCode: string; |
| 54 | + className?: string; |
| 55 | + }) => { |
| 56 | + const highlightedCodePromise = highlightMarkdownCode(updatedCode, language); |
| 57 | + const highlightedCode = React.use(highlightedCodePromise); |
| 58 | + |
| 59 | + return ( |
| 60 | + <pre |
| 61 | + className={cn("not-prose relative rounded-md px-4 py-3", className)} |
| 62 | + style={{ backgroundColor: "#0d1117" }} |
| 63 | + > |
| 64 | + <CopyButton code={updatedCode} /> |
| 65 | + <code |
| 66 | + dangerouslySetInnerHTML={{ __html: highlightedCode }} |
| 67 | + className="block overflow-auto" |
| 68 | + /> |
| 69 | + </pre> |
| 70 | + ); |
| 71 | + }; |
| 72 | + |
| 73 | + const startsWithNpx = code.startsWith("npx"); |
| 74 | + |
| 75 | + if (startsWithNpx) { |
| 76 | + const npmCode = code.replace("npx", "npx"); |
| 77 | + const yarnCode = code.replace("npx", "npx"); |
| 78 | + const bunCode = code.replace("npx", "bunx --bun"); |
| 79 | + const pnpmCode = code.replace("npx", "pnpm dlx"); |
| 80 | + |
| 81 | + return ( |
| 82 | + <Tabs |
| 83 | + defaultValue="npm" |
| 84 | + className="not-prose mb-0 flex flex-col rounded-md bg-[#161b22]" |
| 85 | + > |
| 86 | + <TabsList className="w-fit rounded-md border border-none bg-[#161b22]"> |
| 87 | + <TabsTrigger |
| 88 | + value="npm" |
| 89 | + className="rounded text-sm font-medium data-[state=active]:bg-muted" |
| 90 | + > |
| 91 | + npm |
| 92 | + </TabsTrigger> |
| 93 | + <TabsTrigger |
| 94 | + value="bun" |
| 95 | + className="rounded text-sm font-medium data-[state=active]:bg-muted" |
| 96 | + > |
| 97 | + bun |
| 98 | + </TabsTrigger> |
| 99 | + <TabsTrigger |
| 100 | + value="yarn" |
| 101 | + className="rounded text-sm font-medium data-[state=active]:bg-muted" |
| 102 | + > |
| 103 | + yarn |
| 104 | + </TabsTrigger> |
| 105 | + <TabsTrigger |
| 106 | + value="pnpm" |
| 107 | + className="rounded text-sm font-medium data-[state=active]:bg-muted" |
| 108 | + > |
| 109 | + pnpm |
| 110 | + </TabsTrigger> |
| 111 | + </TabsList> |
| 112 | + <TabsContent value="npm" className="m-0"> |
| 113 | + <CoreCodeBlock updatedCode={npmCode} /> |
| 114 | + </TabsContent> |
| 115 | + <TabsContent value="bun" className="m-0"> |
| 116 | + <CoreCodeBlock updatedCode={bunCode} /> |
| 117 | + </TabsContent> |
| 118 | + <TabsContent value="yarn" className="m-0"> |
| 119 | + <CoreCodeBlock updatedCode={yarnCode} /> |
| 120 | + </TabsContent> |
| 121 | + <TabsContent value="pnpm" className="m-0"> |
| 122 | + <CoreCodeBlock updatedCode={pnpmCode} /> |
| 123 | + </TabsContent> |
| 124 | + </Tabs> |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + return <CoreCodeBlock updatedCode={code} className="mb-6 mt-4" />; |
61 | 129 | };
|
62 | 130 |
|
63 | 131 | export default CodeBlock;
|
0 commit comments