create-sidekick-app 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +202 -0
- package/NOTICE +27 -0
- package/README.md +30 -2
- package/dist/index.js +59 -0
- package/package.json +69 -3
- package/template/base/ai-chat/.env.example +6 -0
- package/template/base/ai-chat/.prettierignore +2 -0
- package/template/base/ai-chat/.prettierrc.json +5 -0
- package/template/base/ai-chat/README.md +38 -0
- package/template/base/ai-chat/_gitignore +41 -0
- package/template/base/ai-chat/components.json +23 -0
- package/template/base/ai-chat/next.config.ts +5 -0
- package/template/base/ai-chat/package.json +61 -0
- package/template/base/ai-chat/postcss.config.mjs +5 -0
- package/template/base/ai-chat/src/app/api/chat/route.ts +141 -0
- package/template/base/ai-chat/src/app/api/instant/route.ts +5 -0
- package/template/base/ai-chat/src/app/chat/[id]/page.tsx +20 -0
- package/template/base/ai-chat/src/app/globals.css +124 -0
- package/template/base/ai-chat/src/app/layout.tsx +34 -0
- package/template/base/ai-chat/src/app/page.tsx +6 -0
- package/template/base/ai-chat/src/components/AuthGate.tsx +15 -0
- package/template/base/ai-chat/src/components/Chat.tsx +228 -0
- package/template/base/ai-chat/src/components/InstantProvider.tsx +18 -0
- package/template/base/ai-chat/src/components/Login.tsx +163 -0
- package/template/base/ai-chat/src/components/Sidebar.tsx +100 -0
- package/template/base/ai-chat/src/components/ai-elements/agent.tsx +142 -0
- package/template/base/ai-chat/src/components/ai-elements/artifact.tsx +149 -0
- package/template/base/ai-chat/src/components/ai-elements/attachments.tsx +427 -0
- package/template/base/ai-chat/src/components/ai-elements/audio-player.tsx +229 -0
- package/template/base/ai-chat/src/components/ai-elements/canvas.tsx +26 -0
- package/template/base/ai-chat/src/components/ai-elements/chain-of-thought.tsx +223 -0
- package/template/base/ai-chat/src/components/ai-elements/checkpoint.tsx +72 -0
- package/template/base/ai-chat/src/components/ai-elements/code-block.tsx +556 -0
- package/template/base/ai-chat/src/components/ai-elements/commit.tsx +449 -0
- package/template/base/ai-chat/src/components/ai-elements/confirmation.tsx +173 -0
- package/template/base/ai-chat/src/components/ai-elements/connection.tsx +28 -0
- package/template/base/ai-chat/src/components/ai-elements/context.tsx +410 -0
- package/template/base/ai-chat/src/components/ai-elements/controls.tsx +19 -0
- package/template/base/ai-chat/src/components/ai-elements/conversation.tsx +167 -0
- package/template/base/ai-chat/src/components/ai-elements/edge.tsx +144 -0
- package/template/base/ai-chat/src/components/ai-elements/environment-variables.tsx +325 -0
- package/template/base/ai-chat/src/components/ai-elements/file-tree.tsx +298 -0
- package/template/base/ai-chat/src/components/ai-elements/image.tsx +25 -0
- package/template/base/ai-chat/src/components/ai-elements/inline-citation.tsx +294 -0
- package/template/base/ai-chat/src/components/ai-elements/jsx-preview.tsx +250 -0
- package/template/base/ai-chat/src/components/ai-elements/message.tsx +356 -0
- package/template/base/ai-chat/src/components/ai-elements/mic-selector.tsx +372 -0
- package/template/base/ai-chat/src/components/ai-elements/model-selector.tsx +214 -0
- package/template/base/ai-chat/src/components/ai-elements/node.tsx +72 -0
- package/template/base/ai-chat/src/components/ai-elements/open-in-chat.tsx +367 -0
- package/template/base/ai-chat/src/components/ai-elements/package-info.tsx +235 -0
- package/template/base/ai-chat/src/components/ai-elements/panel.tsx +16 -0
- package/template/base/ai-chat/src/components/ai-elements/persona.tsx +280 -0
- package/template/base/ai-chat/src/components/ai-elements/plan.tsx +144 -0
- package/template/base/ai-chat/src/components/ai-elements/prompt-input.tsx +1341 -0
- package/template/base/ai-chat/src/components/ai-elements/queue.tsx +275 -0
- package/template/base/ai-chat/src/components/ai-elements/reasoning.tsx +229 -0
- package/template/base/ai-chat/src/components/ai-elements/sandbox.tsx +128 -0
- package/template/base/ai-chat/src/components/ai-elements/schema-display.tsx +473 -0
- package/template/base/ai-chat/src/components/ai-elements/shimmer.tsx +78 -0
- package/template/base/ai-chat/src/components/ai-elements/snippet.tsx +141 -0
- package/template/base/ai-chat/src/components/ai-elements/sources.tsx +78 -0
- package/template/base/ai-chat/src/components/ai-elements/speech-input.tsx +324 -0
- package/template/base/ai-chat/src/components/ai-elements/stack-trace.tsx +531 -0
- package/template/base/ai-chat/src/components/ai-elements/suggestion.tsx +55 -0
- package/template/base/ai-chat/src/components/ai-elements/task.tsx +88 -0
- package/template/base/ai-chat/src/components/ai-elements/terminal.tsx +277 -0
- package/template/base/ai-chat/src/components/ai-elements/test-results.tsx +497 -0
- package/template/base/ai-chat/src/components/ai-elements/tool.tsx +174 -0
- package/template/base/ai-chat/src/components/ai-elements/toolbar.tsx +17 -0
- package/template/base/ai-chat/src/components/ai-elements/transcription.tsx +126 -0
- package/template/base/ai-chat/src/components/ai-elements/voice-selector.tsx +525 -0
- package/template/base/ai-chat/src/components/ai-elements/web-preview.tsx +282 -0
- package/template/base/ai-chat/src/components/ui/accordion.tsx +66 -0
- package/template/base/ai-chat/src/components/ui/alert.tsx +66 -0
- package/template/base/ai-chat/src/components/ui/avatar.tsx +109 -0
- package/template/base/ai-chat/src/components/ui/badge.tsx +48 -0
- package/template/base/ai-chat/src/components/ui/button-group.tsx +83 -0
- package/template/base/ai-chat/src/components/ui/button.tsx +64 -0
- package/template/base/ai-chat/src/components/ui/card.tsx +92 -0
- package/template/base/ai-chat/src/components/ui/carousel.tsx +241 -0
- package/template/base/ai-chat/src/components/ui/collapsible.tsx +33 -0
- package/template/base/ai-chat/src/components/ui/command.tsx +184 -0
- package/template/base/ai-chat/src/components/ui/dialog.tsx +158 -0
- package/template/base/ai-chat/src/components/ui/dropdown-menu.tsx +257 -0
- package/template/base/ai-chat/src/components/ui/hover-card.tsx +44 -0
- package/template/base/ai-chat/src/components/ui/input-group.tsx +170 -0
- package/template/base/ai-chat/src/components/ui/input.tsx +21 -0
- package/template/base/ai-chat/src/components/ui/popover.tsx +89 -0
- package/template/base/ai-chat/src/components/ui/progress.tsx +31 -0
- package/template/base/ai-chat/src/components/ui/scroll-area.tsx +58 -0
- package/template/base/ai-chat/src/components/ui/select.tsx +190 -0
- package/template/base/ai-chat/src/components/ui/separator.tsx +28 -0
- package/template/base/ai-chat/src/components/ui/spinner.tsx +16 -0
- package/template/base/ai-chat/src/components/ui/switch.tsx +35 -0
- package/template/base/ai-chat/src/components/ui/tabs.tsx +91 -0
- package/template/base/ai-chat/src/components/ui/textarea.tsx +18 -0
- package/template/base/ai-chat/src/components/ui/tooltip.tsx +57 -0
- package/template/base/ai-chat/src/instant.perms.ts +34 -0
- package/template/base/ai-chat/src/instant.schema.ts +107 -0
- package/template/base/ai-chat/src/lib/adminDb.ts +8 -0
- package/template/base/ai-chat/src/lib/db.ts +8 -0
- package/template/base/ai-chat/src/lib/mockModel.ts +119 -0
- package/template/base/ai-chat/src/lib/utils.ts +6 -0
- package/template/base/ai-chat/tsconfig.json +33 -0
- package/template/base/bun-react/.env.example +2 -0
- package/template/base/bun-react/README.md +12 -0
- package/template/base/bun-react/_gitignore +34 -0
- package/template/base/bun-react/bun-env.d.ts +17 -0
- package/template/base/bun-react/bunfig.toml +4 -0
- package/template/base/bun-react/package.json +22 -0
- package/template/base/bun-react/src/App.tsx +168 -0
- package/template/base/bun-react/src/frontend.tsx +20 -0
- package/template/base/bun-react/src/getUnusedPort.ts +32 -0
- package/template/base/bun-react/src/index.css +25 -0
- package/template/base/bun-react/src/index.html +13 -0
- package/template/base/bun-react/src/index.ts +21 -0
- package/template/base/bun-react/src/instant.perms.ts +24 -0
- package/template/base/bun-react/src/instant.schema.ts +46 -0
- package/template/base/bun-react/src/lib/db.ts +8 -0
- package/template/base/bun-react/src/logo.svg +1 -0
- package/template/base/bun-react/tsconfig.json +36 -0
- package/template/base/expo/.env.example +2 -0
- package/template/base/expo/README.md +18 -0
- package/template/base/expo/_gitignore +41 -0
- package/template/base/expo/app/+not-found.tsx +16 -0
- package/template/base/expo/app/_layout.tsx +37 -0
- package/template/base/expo/app/index.tsx +64 -0
- package/template/base/expo/app.json +42 -0
- package/template/base/expo/assets/fonts/SpaceMono-Regular.ttf +0 -0
- package/template/base/expo/assets/images/adaptive-icon.png +0 -0
- package/template/base/expo/assets/images/favicon.png +0 -0
- package/template/base/expo/assets/images/icon.png +0 -0
- package/template/base/expo/assets/images/partial-react-logo.png +0 -0
- package/template/base/expo/assets/images/react-logo.png +0 -0
- package/template/base/expo/assets/images/react-logo@2x.png +0 -0
- package/template/base/expo/assets/images/react-logo@3x.png +0 -0
- package/template/base/expo/assets/images/splash-icon.png +0 -0
- package/template/base/expo/babel.config.js +12 -0
- package/template/base/expo/constants/Colors.ts +26 -0
- package/template/base/expo/eslint.config.js +10 -0
- package/template/base/expo/global.css +3 -0
- package/template/base/expo/hooks/useColorScheme.ts +1 -0
- package/template/base/expo/hooks/useColorScheme.web.ts +21 -0
- package/template/base/expo/hooks/useThemeColor.ts +21 -0
- package/template/base/expo/instant.perms.ts +24 -0
- package/template/base/expo/instant.schema.ts +44 -0
- package/template/base/expo/lib/db.ts +7 -0
- package/template/base/expo/metro.config.js +6 -0
- package/template/base/expo/nativewind-env.d.ts +1 -0
- package/template/base/expo/package.json +57 -0
- package/template/base/expo/scripts/reset-project.js +112 -0
- package/template/base/expo/tailwind.config.js +9 -0
- package/template/base/expo/tsconfig.json +18 -0
- package/template/base/next-js-app-dir/.env.example +2 -0
- package/template/base/next-js-app-dir/README.md +18 -0
- package/template/base/next-js-app-dir/_gitignore +41 -0
- package/template/base/next-js-app-dir/next.config.ts +7 -0
- package/template/base/next-js-app-dir/package.json +27 -0
- package/template/base/next-js-app-dir/postcss.config.mjs +5 -0
- package/template/base/next-js-app-dir/public/file.svg +1 -0
- package/template/base/next-js-app-dir/public/globe.svg +1 -0
- package/template/base/next-js-app-dir/public/next.svg +1 -0
- package/template/base/next-js-app-dir/public/vercel.svg +1 -0
- package/template/base/next-js-app-dir/public/window.svg +1 -0
- package/template/base/next-js-app-dir/src/app/favicon.ico +0 -0
- package/template/base/next-js-app-dir/src/app/globals.css +26 -0
- package/template/base/next-js-app-dir/src/app/layout.tsx +34 -0
- package/template/base/next-js-app-dir/src/app/page.tsx +169 -0
- package/template/base/next-js-app-dir/src/instant.perms.ts +24 -0
- package/template/base/next-js-app-dir/src/instant.schema.ts +50 -0
- package/template/base/next-js-app-dir/src/lib/db.ts +8 -0
- package/template/base/next-js-app-dir/tsconfig.json +41 -0
- package/template/base/solidjs-vite/README.md +15 -0
- package/template/base/solidjs-vite/_gitignore +25 -0
- package/template/base/solidjs-vite/index.html +15 -0
- package/template/base/solidjs-vite/package.json +25 -0
- package/template/base/solidjs-vite/src/App.tsx +171 -0
- package/template/base/solidjs-vite/src/index.css +25 -0
- package/template/base/solidjs-vite/src/index.tsx +16 -0
- package/template/base/solidjs-vite/src/instant.perms.ts +24 -0
- package/template/base/solidjs-vite/src/instant.schema.ts +46 -0
- package/template/base/solidjs-vite/src/lib/db.ts +8 -0
- package/template/base/solidjs-vite/tsconfig.json +20 -0
- package/template/base/solidjs-vite/vite.config.ts +11 -0
- package/template/base/sveltekit/README.md +15 -0
- package/template/base/sveltekit/_gitignore +26 -0
- package/template/base/sveltekit/package.json +25 -0
- package/template/base/sveltekit/src/app.css +25 -0
- package/template/base/sveltekit/src/app.html +11 -0
- package/template/base/sveltekit/src/instant.perms.ts +24 -0
- package/template/base/sveltekit/src/instant.schema.ts +46 -0
- package/template/base/sveltekit/src/lib/db.ts +8 -0
- package/template/base/sveltekit/src/routes/+layout.svelte +6 -0
- package/template/base/sveltekit/src/routes/+page.svelte +236 -0
- package/template/base/sveltekit/src/routes/+page.ts +1 -0
- package/template/base/sveltekit/svelte.config.js +10 -0
- package/template/base/sveltekit/tsconfig.json +14 -0
- package/template/base/sveltekit/vite.config.ts +7 -0
- package/template/base/tanstack-start/.cta.json +12 -0
- package/template/base/tanstack-start/README.md +17 -0
- package/template/base/tanstack-start/_gitignore +13 -0
- package/template/base/tanstack-start/package.json +43 -0
- package/template/base/tanstack-start/public/favicon.ico +0 -0
- package/template/base/tanstack-start/public/logo192.png +0 -0
- package/template/base/tanstack-start/public/logo512.png +0 -0
- package/template/base/tanstack-start/public/manifest.json +25 -0
- package/template/base/tanstack-start/public/robots.txt +3 -0
- package/template/base/tanstack-start/public/tanstack-circle-logo.png +0 -0
- package/template/base/tanstack-start/public/tanstack-word-logo-white.svg +1 -0
- package/template/base/tanstack-start/src/components/Header.tsx +21 -0
- package/template/base/tanstack-start/src/instant.perms.ts +24 -0
- package/template/base/tanstack-start/src/instant.schema.ts +50 -0
- package/template/base/tanstack-start/src/lib/adminDb.ts +12 -0
- package/template/base/tanstack-start/src/lib/db.ts +9 -0
- package/template/base/tanstack-start/src/logo.svg +12 -0
- package/template/base/tanstack-start/src/routeTree.gen.ts +104 -0
- package/template/base/tanstack-start/src/router.tsx +22 -0
- package/template/base/tanstack-start/src/routes/__root.tsx +70 -0
- package/template/base/tanstack-start/src/routes/api/instant.ts +18 -0
- package/template/base/tanstack-start/src/routes/index.tsx +238 -0
- package/template/base/tanstack-start/src/routes/login.tsx +140 -0
- package/template/base/tanstack-start/src/styles.css +18 -0
- package/template/base/tanstack-start/tsconfig.json +28 -0
- package/template/base/tanstack-start/vite.config.ts +33 -0
- package/template/base/tanstack-start-with-tanstack-query/.cta.json +12 -0
- package/template/base/tanstack-start-with-tanstack-query/README.md +18 -0
- package/template/base/tanstack-start-with-tanstack-query/_gitignore +13 -0
- package/template/base/tanstack-start-with-tanstack-query/package.json +46 -0
- package/template/base/tanstack-start-with-tanstack-query/public/favicon.ico +0 -0
- package/template/base/tanstack-start-with-tanstack-query/public/logo192.png +0 -0
- package/template/base/tanstack-start-with-tanstack-query/public/logo512.png +0 -0
- package/template/base/tanstack-start-with-tanstack-query/public/manifest.json +25 -0
- package/template/base/tanstack-start-with-tanstack-query/public/robots.txt +3 -0
- package/template/base/tanstack-start-with-tanstack-query/public/tanstack-circle-logo.png +0 -0
- package/template/base/tanstack-start-with-tanstack-query/public/tanstack-word-logo-white.svg +1 -0
- package/template/base/tanstack-start-with-tanstack-query/src/components/Header.tsx +21 -0
- package/template/base/tanstack-start-with-tanstack-query/src/instant.perms.ts +24 -0
- package/template/base/tanstack-start-with-tanstack-query/src/instant.schema.ts +50 -0
- package/template/base/tanstack-start-with-tanstack-query/src/lib/adminDb.ts +9 -0
- package/template/base/tanstack-start-with-tanstack-query/src/lib/db.ts +11 -0
- package/template/base/tanstack-start-with-tanstack-query/src/lib/instantQuery.ts +93 -0
- package/template/base/tanstack-start-with-tanstack-query/src/logo.svg +12 -0
- package/template/base/tanstack-start-with-tanstack-query/src/routeTree.gen.ts +104 -0
- package/template/base/tanstack-start-with-tanstack-query/src/router.tsx +108 -0
- package/template/base/tanstack-start-with-tanstack-query/src/routes/__root.tsx +76 -0
- package/template/base/tanstack-start-with-tanstack-query/src/routes/api/instant.ts +16 -0
- package/template/base/tanstack-start-with-tanstack-query/src/routes/index.tsx +231 -0
- package/template/base/tanstack-start-with-tanstack-query/src/routes/login.tsx +140 -0
- package/template/base/tanstack-start-with-tanstack-query/src/styles.css +17 -0
- package/template/base/tanstack-start-with-tanstack-query/tsconfig.json +28 -0
- package/template/base/tanstack-start-with-tanstack-query/vite.config.ts +33 -0
- package/template/base/vite-react/README.md +16 -0
- package/template/base/vite-react/_gitignore +26 -0
- package/template/base/vite-react/eslint.config.js +23 -0
- package/template/base/vite-react/index.html +13 -0
- package/template/base/vite-react/package.json +35 -0
- package/template/base/vite-react/public/favicon.svg +1 -0
- package/template/base/vite-react/src/App.tsx +167 -0
- package/template/base/vite-react/src/index.css +1 -0
- package/template/base/vite-react/src/instant.perms.ts +24 -0
- package/template/base/vite-react/src/instant.schema.ts +51 -0
- package/template/base/vite-react/src/lib/db.ts +8 -0
- package/template/base/vite-react/src/main.tsx +10 -0
- package/template/base/vite-react/tsconfig.app.json +25 -0
- package/template/base/vite-react/tsconfig.json +7 -0
- package/template/base/vite-react/tsconfig.node.json +24 -0
- package/template/base/vite-react/vite.config.ts +8 -0
- package/template/base/vite-vanilla/.env.example +2 -0
- package/template/base/vite-vanilla/README.md +17 -0
- package/template/base/vite-vanilla/_gitignore +25 -0
- package/template/base/vite-vanilla/index.html +13 -0
- package/template/base/vite-vanilla/package.json +20 -0
- package/template/base/vite-vanilla/pnpm-lock.yaml +707 -0
- package/template/base/vite-vanilla/public/vite.svg +1 -0
- package/template/base/vite-vanilla/src/instant.perms.ts +24 -0
- package/template/base/vite-vanilla/src/instant.schema.ts +50 -0
- package/template/base/vite-vanilla/src/lib/db.ts +11 -0
- package/template/base/vite-vanilla/src/main.ts +242 -0
- package/template/base/vite-vanilla/src/style.css +96 -0
- package/template/base/vite-vanilla/src/typescript.svg +1 -0
- package/template/base/vite-vanilla/src/vite-env.d.ts +1 -0
- package/template/base/vite-vanilla/tsconfig.json +24 -0
- package/template/base/vue-vite/README.md +15 -0
- package/template/base/vue-vite/_gitignore +43 -0
- package/template/base/vue-vite/env.d.ts +1 -0
- package/template/base/vue-vite/index.html +12 -0
- package/template/base/vue-vite/package.json +34 -0
- package/template/base/vue-vite/src/App.vue +238 -0
- package/template/base/vue-vite/src/assets/main.css +25 -0
- package/template/base/vue-vite/src/instant.perms.ts +24 -0
- package/template/base/vue-vite/src/instant.schema.ts +46 -0
- package/template/base/vue-vite/src/lib/db.ts +8 -0
- package/template/base/vue-vite/src/main.ts +6 -0
- package/template/base/vue-vite/tsconfig.app.json +15 -0
- package/template/base/vue-vite/tsconfig.json +11 -0
- package/template/base/vue-vite/tsconfig.node.json +27 -0
- package/template/base/vue-vite/vite.config.ts +16 -0
- package/template/rules/AGENTS.md +436 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { ComponentProps, CSSProperties, HTMLAttributes } from 'react';
|
|
4
|
+
import type {
|
|
5
|
+
BundledLanguage,
|
|
6
|
+
BundledTheme,
|
|
7
|
+
HighlighterGeneric,
|
|
8
|
+
ThemedToken,
|
|
9
|
+
} from 'shiki';
|
|
10
|
+
|
|
11
|
+
import { Button } from '@/components/ui/button';
|
|
12
|
+
import {
|
|
13
|
+
Select,
|
|
14
|
+
SelectContent,
|
|
15
|
+
SelectItem,
|
|
16
|
+
SelectTrigger,
|
|
17
|
+
SelectValue,
|
|
18
|
+
} from '@/components/ui/select';
|
|
19
|
+
import { cn } from '@/lib/utils';
|
|
20
|
+
import { CheckIcon, CopyIcon } from 'lucide-react';
|
|
21
|
+
import {
|
|
22
|
+
createContext,
|
|
23
|
+
memo,
|
|
24
|
+
useCallback,
|
|
25
|
+
useContext,
|
|
26
|
+
useEffect,
|
|
27
|
+
useMemo,
|
|
28
|
+
useRef,
|
|
29
|
+
useState,
|
|
30
|
+
} from 'react';
|
|
31
|
+
import { createHighlighter } from 'shiki';
|
|
32
|
+
|
|
33
|
+
// Shiki uses bitflags for font styles: 1=italic, 2=bold, 4=underline
|
|
34
|
+
// biome-ignore lint/suspicious/noBitwiseOperators: shiki bitflag check
|
|
35
|
+
// eslint-disable-next-line no-bitwise -- shiki bitflag check
|
|
36
|
+
const isItalic = (fontStyle: number | undefined) => fontStyle && fontStyle & 1;
|
|
37
|
+
// biome-ignore lint/suspicious/noBitwiseOperators: shiki bitflag check
|
|
38
|
+
// eslint-disable-next-line no-bitwise -- shiki bitflag check
|
|
39
|
+
// oxlint-disable-next-line eslint(no-bitwise)
|
|
40
|
+
const isBold = (fontStyle: number | undefined) => fontStyle && fontStyle & 2;
|
|
41
|
+
const isUnderline = (fontStyle: number | undefined) =>
|
|
42
|
+
// biome-ignore lint/suspicious/noBitwiseOperators: shiki bitflag check
|
|
43
|
+
// oxlint-disable-next-line eslint(no-bitwise)
|
|
44
|
+
fontStyle && fontStyle & 4;
|
|
45
|
+
|
|
46
|
+
// Transform tokens to include pre-computed keys to avoid noArrayIndexKey lint
|
|
47
|
+
interface KeyedToken {
|
|
48
|
+
token: ThemedToken;
|
|
49
|
+
key: string;
|
|
50
|
+
}
|
|
51
|
+
interface KeyedLine {
|
|
52
|
+
tokens: KeyedToken[];
|
|
53
|
+
key: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const addKeysToTokens = (lines: ThemedToken[][]): KeyedLine[] =>
|
|
57
|
+
lines.map((line, lineIdx) => ({
|
|
58
|
+
key: `line-${lineIdx}`,
|
|
59
|
+
tokens: line.map((token, tokenIdx) => ({
|
|
60
|
+
key: `line-${lineIdx}-${tokenIdx}`,
|
|
61
|
+
token,
|
|
62
|
+
})),
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
// Token rendering component
|
|
66
|
+
const TokenSpan = ({ token }: { token: ThemedToken }) => (
|
|
67
|
+
<span
|
|
68
|
+
className="dark:!bg-[var(--shiki-dark-bg)] dark:!text-[var(--shiki-dark)]"
|
|
69
|
+
style={
|
|
70
|
+
{
|
|
71
|
+
backgroundColor: token.bgColor,
|
|
72
|
+
color: token.color,
|
|
73
|
+
fontStyle: isItalic(token.fontStyle) ? 'italic' : undefined,
|
|
74
|
+
fontWeight: isBold(token.fontStyle) ? 'bold' : undefined,
|
|
75
|
+
textDecoration: isUnderline(token.fontStyle) ? 'underline' : undefined,
|
|
76
|
+
...token.htmlStyle,
|
|
77
|
+
} as CSSProperties
|
|
78
|
+
}
|
|
79
|
+
>
|
|
80
|
+
{token.content}
|
|
81
|
+
</span>
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// Line rendering component
|
|
85
|
+
const LineSpan = ({
|
|
86
|
+
keyedLine,
|
|
87
|
+
showLineNumbers,
|
|
88
|
+
}: {
|
|
89
|
+
keyedLine: KeyedLine;
|
|
90
|
+
showLineNumbers: boolean;
|
|
91
|
+
}) => (
|
|
92
|
+
<span className={showLineNumbers ? LINE_NUMBER_CLASSES : 'block'}>
|
|
93
|
+
{keyedLine.tokens.length === 0
|
|
94
|
+
? '\n'
|
|
95
|
+
: keyedLine.tokens.map(({ token, key }) => (
|
|
96
|
+
<TokenSpan key={key} token={token} />
|
|
97
|
+
))}
|
|
98
|
+
</span>
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// Types
|
|
102
|
+
type CodeBlockProps = HTMLAttributes<HTMLDivElement> & {
|
|
103
|
+
code: string;
|
|
104
|
+
language: BundledLanguage;
|
|
105
|
+
showLineNumbers?: boolean;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
interface TokenizedCode {
|
|
109
|
+
tokens: ThemedToken[][];
|
|
110
|
+
fg: string;
|
|
111
|
+
bg: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface CodeBlockContextType {
|
|
115
|
+
code: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Context
|
|
119
|
+
const CodeBlockContext = createContext<CodeBlockContextType>({
|
|
120
|
+
code: '',
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Highlighter cache (singleton per language)
|
|
124
|
+
const highlighterCache = new Map<
|
|
125
|
+
string,
|
|
126
|
+
Promise<HighlighterGeneric<BundledLanguage, BundledTheme>>
|
|
127
|
+
>();
|
|
128
|
+
|
|
129
|
+
// Token cache
|
|
130
|
+
const tokensCache = new Map<string, TokenizedCode>();
|
|
131
|
+
|
|
132
|
+
// Subscribers for async token updates
|
|
133
|
+
const subscribers = new Map<string, Set<(result: TokenizedCode) => void>>();
|
|
134
|
+
|
|
135
|
+
const getTokensCacheKey = (code: string, language: BundledLanguage) => {
|
|
136
|
+
const start = code.slice(0, 100);
|
|
137
|
+
const end = code.length > 100 ? code.slice(-100) : '';
|
|
138
|
+
return `${language}:${code.length}:${start}:${end}`;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const getHighlighter = (
|
|
142
|
+
language: BundledLanguage,
|
|
143
|
+
): Promise<HighlighterGeneric<BundledLanguage, BundledTheme>> => {
|
|
144
|
+
const cached = highlighterCache.get(language);
|
|
145
|
+
if (cached) {
|
|
146
|
+
return cached;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const highlighterPromise = createHighlighter({
|
|
150
|
+
langs: [language],
|
|
151
|
+
themes: ['github-light', 'github-dark'],
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
highlighterCache.set(language, highlighterPromise);
|
|
155
|
+
return highlighterPromise;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Create raw tokens for immediate display while highlighting loads
|
|
159
|
+
const createRawTokens = (code: string): TokenizedCode => ({
|
|
160
|
+
bg: 'transparent',
|
|
161
|
+
fg: 'inherit',
|
|
162
|
+
tokens: code.split('\n').map((line) =>
|
|
163
|
+
line === ''
|
|
164
|
+
? []
|
|
165
|
+
: [
|
|
166
|
+
{
|
|
167
|
+
color: 'inherit',
|
|
168
|
+
content: line,
|
|
169
|
+
} as ThemedToken,
|
|
170
|
+
],
|
|
171
|
+
),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Synchronous highlight with callback for async results
|
|
175
|
+
export const highlightCode = (
|
|
176
|
+
code: string,
|
|
177
|
+
language: BundledLanguage,
|
|
178
|
+
// oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-callbacks)
|
|
179
|
+
callback?: (result: TokenizedCode) => void,
|
|
180
|
+
): TokenizedCode | null => {
|
|
181
|
+
const tokensCacheKey = getTokensCacheKey(code, language);
|
|
182
|
+
|
|
183
|
+
// Return cached result if available
|
|
184
|
+
const cached = tokensCache.get(tokensCacheKey);
|
|
185
|
+
if (cached) {
|
|
186
|
+
return cached;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Subscribe callback if provided
|
|
190
|
+
if (callback) {
|
|
191
|
+
if (!subscribers.has(tokensCacheKey)) {
|
|
192
|
+
subscribers.set(tokensCacheKey, new Set());
|
|
193
|
+
}
|
|
194
|
+
subscribers.get(tokensCacheKey)?.add(callback);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Start highlighting in background - fire-and-forget async pattern
|
|
198
|
+
getHighlighter(language)
|
|
199
|
+
// oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-then)
|
|
200
|
+
.then((highlighter) => {
|
|
201
|
+
const availableLangs = highlighter.getLoadedLanguages();
|
|
202
|
+
const langToUse = availableLangs.includes(language) ? language : 'text';
|
|
203
|
+
|
|
204
|
+
const result = highlighter.codeToTokens(code, {
|
|
205
|
+
lang: langToUse,
|
|
206
|
+
themes: {
|
|
207
|
+
dark: 'github-dark',
|
|
208
|
+
light: 'github-light',
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const tokenized: TokenizedCode = {
|
|
213
|
+
bg: result.bg ?? 'transparent',
|
|
214
|
+
fg: result.fg ?? 'inherit',
|
|
215
|
+
tokens: result.tokens,
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Cache the result
|
|
219
|
+
tokensCache.set(tokensCacheKey, tokenized);
|
|
220
|
+
|
|
221
|
+
// Notify all subscribers
|
|
222
|
+
const subs = subscribers.get(tokensCacheKey);
|
|
223
|
+
if (subs) {
|
|
224
|
+
for (const sub of subs) {
|
|
225
|
+
sub(tokenized);
|
|
226
|
+
}
|
|
227
|
+
subscribers.delete(tokensCacheKey);
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
// oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-then), eslint-plugin-promise(prefer-await-to-callbacks)
|
|
231
|
+
.catch((error) => {
|
|
232
|
+
console.error('Failed to highlight code:', error);
|
|
233
|
+
subscribers.delete(tokensCacheKey);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
return null;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// Line number styles using CSS counters
|
|
240
|
+
const LINE_NUMBER_CLASSES = cn(
|
|
241
|
+
'block',
|
|
242
|
+
'before:content-[counter(line)]',
|
|
243
|
+
'before:inline-block',
|
|
244
|
+
'before:[counter-increment:line]',
|
|
245
|
+
'before:w-8',
|
|
246
|
+
'before:mr-4',
|
|
247
|
+
'before:text-right',
|
|
248
|
+
'before:text-muted-foreground/50',
|
|
249
|
+
'before:font-mono',
|
|
250
|
+
'before:select-none',
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
const CodeBlockBody = memo(
|
|
254
|
+
({
|
|
255
|
+
tokenized,
|
|
256
|
+
showLineNumbers,
|
|
257
|
+
className,
|
|
258
|
+
}: {
|
|
259
|
+
tokenized: TokenizedCode;
|
|
260
|
+
showLineNumbers: boolean;
|
|
261
|
+
className?: string;
|
|
262
|
+
}) => {
|
|
263
|
+
const preStyle = useMemo(
|
|
264
|
+
() => ({
|
|
265
|
+
backgroundColor: tokenized.bg,
|
|
266
|
+
color: tokenized.fg,
|
|
267
|
+
}),
|
|
268
|
+
[tokenized.bg, tokenized.fg],
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const keyedLines = useMemo(
|
|
272
|
+
() => addKeysToTokens(tokenized.tokens),
|
|
273
|
+
[tokenized.tokens],
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
return (
|
|
277
|
+
<pre
|
|
278
|
+
className={cn(
|
|
279
|
+
'm-0 p-4 text-sm dark:!bg-[var(--shiki-dark-bg)] dark:!text-[var(--shiki-dark)]',
|
|
280
|
+
className,
|
|
281
|
+
)}
|
|
282
|
+
style={preStyle}
|
|
283
|
+
>
|
|
284
|
+
<code
|
|
285
|
+
className={cn(
|
|
286
|
+
'font-mono text-sm',
|
|
287
|
+
showLineNumbers &&
|
|
288
|
+
'[counter-increment:line_0] [counter-reset:line]',
|
|
289
|
+
)}
|
|
290
|
+
>
|
|
291
|
+
{keyedLines.map((keyedLine) => (
|
|
292
|
+
<LineSpan
|
|
293
|
+
key={keyedLine.key}
|
|
294
|
+
keyedLine={keyedLine}
|
|
295
|
+
showLineNumbers={showLineNumbers}
|
|
296
|
+
/>
|
|
297
|
+
))}
|
|
298
|
+
</code>
|
|
299
|
+
</pre>
|
|
300
|
+
);
|
|
301
|
+
},
|
|
302
|
+
(prevProps, nextProps) =>
|
|
303
|
+
prevProps.tokenized === nextProps.tokenized &&
|
|
304
|
+
prevProps.showLineNumbers === nextProps.showLineNumbers &&
|
|
305
|
+
prevProps.className === nextProps.className,
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
CodeBlockBody.displayName = 'CodeBlockBody';
|
|
309
|
+
|
|
310
|
+
export const CodeBlockContainer = ({
|
|
311
|
+
className,
|
|
312
|
+
language,
|
|
313
|
+
style,
|
|
314
|
+
...props
|
|
315
|
+
}: HTMLAttributes<HTMLDivElement> & { language: string }) => (
|
|
316
|
+
<div
|
|
317
|
+
className={cn(
|
|
318
|
+
'group bg-background text-foreground relative w-full overflow-hidden rounded-md border',
|
|
319
|
+
className,
|
|
320
|
+
)}
|
|
321
|
+
data-language={language}
|
|
322
|
+
style={{
|
|
323
|
+
containIntrinsicSize: 'auto 200px',
|
|
324
|
+
contentVisibility: 'auto',
|
|
325
|
+
...style,
|
|
326
|
+
}}
|
|
327
|
+
{...props}
|
|
328
|
+
/>
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
export const CodeBlockHeader = ({
|
|
332
|
+
children,
|
|
333
|
+
className,
|
|
334
|
+
...props
|
|
335
|
+
}: HTMLAttributes<HTMLDivElement>) => (
|
|
336
|
+
<div
|
|
337
|
+
className={cn(
|
|
338
|
+
'bg-muted/80 text-muted-foreground flex items-center justify-between border-b px-3 py-2 text-xs',
|
|
339
|
+
className,
|
|
340
|
+
)}
|
|
341
|
+
{...props}
|
|
342
|
+
>
|
|
343
|
+
{children}
|
|
344
|
+
</div>
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
export const CodeBlockTitle = ({
|
|
348
|
+
children,
|
|
349
|
+
className,
|
|
350
|
+
...props
|
|
351
|
+
}: HTMLAttributes<HTMLDivElement>) => (
|
|
352
|
+
<div className={cn('flex items-center gap-2', className)} {...props}>
|
|
353
|
+
{children}
|
|
354
|
+
</div>
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
export const CodeBlockFilename = ({
|
|
358
|
+
children,
|
|
359
|
+
className,
|
|
360
|
+
...props
|
|
361
|
+
}: HTMLAttributes<HTMLSpanElement>) => (
|
|
362
|
+
<span className={cn('font-mono', className)} {...props}>
|
|
363
|
+
{children}
|
|
364
|
+
</span>
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
export const CodeBlockActions = ({
|
|
368
|
+
children,
|
|
369
|
+
className,
|
|
370
|
+
...props
|
|
371
|
+
}: HTMLAttributes<HTMLDivElement>) => (
|
|
372
|
+
<div
|
|
373
|
+
className={cn('-my-1 -mr-1 flex items-center gap-2', className)}
|
|
374
|
+
{...props}
|
|
375
|
+
>
|
|
376
|
+
{children}
|
|
377
|
+
</div>
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
export const CodeBlockContent = ({
|
|
381
|
+
code,
|
|
382
|
+
language,
|
|
383
|
+
showLineNumbers = false,
|
|
384
|
+
}: {
|
|
385
|
+
code: string;
|
|
386
|
+
language: BundledLanguage;
|
|
387
|
+
showLineNumbers?: boolean;
|
|
388
|
+
}) => {
|
|
389
|
+
// Memoized raw tokens for immediate display
|
|
390
|
+
const rawTokens = useMemo(() => createRawTokens(code), [code]);
|
|
391
|
+
|
|
392
|
+
// Try to get cached result synchronously, otherwise use raw tokens
|
|
393
|
+
const [tokenized, setTokenized] = useState<TokenizedCode>(
|
|
394
|
+
() => highlightCode(code, language) ?? rawTokens,
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
useEffect(() => {
|
|
398
|
+
let cancelled = false;
|
|
399
|
+
|
|
400
|
+
// Reset to raw tokens when code changes (shows current code, not stale tokens)
|
|
401
|
+
setTokenized(highlightCode(code, language) ?? rawTokens);
|
|
402
|
+
|
|
403
|
+
// Subscribe to async highlighting result
|
|
404
|
+
highlightCode(code, language, (result) => {
|
|
405
|
+
if (!cancelled) {
|
|
406
|
+
setTokenized(result);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
return () => {
|
|
411
|
+
cancelled = true;
|
|
412
|
+
};
|
|
413
|
+
}, [code, language, rawTokens]);
|
|
414
|
+
|
|
415
|
+
return (
|
|
416
|
+
<div className="relative overflow-auto">
|
|
417
|
+
<CodeBlockBody showLineNumbers={showLineNumbers} tokenized={tokenized} />
|
|
418
|
+
</div>
|
|
419
|
+
);
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
export const CodeBlock = ({
|
|
423
|
+
code,
|
|
424
|
+
language,
|
|
425
|
+
showLineNumbers = false,
|
|
426
|
+
className,
|
|
427
|
+
children,
|
|
428
|
+
...props
|
|
429
|
+
}: CodeBlockProps) => {
|
|
430
|
+
const contextValue = useMemo(() => ({ code }), [code]);
|
|
431
|
+
|
|
432
|
+
return (
|
|
433
|
+
<CodeBlockContext.Provider value={contextValue}>
|
|
434
|
+
<CodeBlockContainer className={className} language={language} {...props}>
|
|
435
|
+
{children}
|
|
436
|
+
<CodeBlockContent
|
|
437
|
+
code={code}
|
|
438
|
+
language={language}
|
|
439
|
+
showLineNumbers={showLineNumbers}
|
|
440
|
+
/>
|
|
441
|
+
</CodeBlockContainer>
|
|
442
|
+
</CodeBlockContext.Provider>
|
|
443
|
+
);
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
export type CodeBlockCopyButtonProps = ComponentProps<typeof Button> & {
|
|
447
|
+
onCopy?: () => void;
|
|
448
|
+
onError?: (error: Error) => void;
|
|
449
|
+
timeout?: number;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
export const CodeBlockCopyButton = ({
|
|
453
|
+
onCopy,
|
|
454
|
+
onError,
|
|
455
|
+
timeout = 2000,
|
|
456
|
+
children,
|
|
457
|
+
className,
|
|
458
|
+
...props
|
|
459
|
+
}: CodeBlockCopyButtonProps) => {
|
|
460
|
+
const [isCopied, setIsCopied] = useState(false);
|
|
461
|
+
const timeoutRef = useRef<number>(0);
|
|
462
|
+
const { code } = useContext(CodeBlockContext);
|
|
463
|
+
|
|
464
|
+
const copyToClipboard = useCallback(async () => {
|
|
465
|
+
if (typeof window === 'undefined' || !navigator?.clipboard?.writeText) {
|
|
466
|
+
onError?.(new Error('Clipboard API not available'));
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
try {
|
|
471
|
+
if (!isCopied) {
|
|
472
|
+
await navigator.clipboard.writeText(code);
|
|
473
|
+
setIsCopied(true);
|
|
474
|
+
onCopy?.();
|
|
475
|
+
timeoutRef.current = window.setTimeout(
|
|
476
|
+
() => setIsCopied(false),
|
|
477
|
+
timeout,
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
} catch (error) {
|
|
481
|
+
onError?.(error as Error);
|
|
482
|
+
}
|
|
483
|
+
}, [code, onCopy, onError, timeout, isCopied]);
|
|
484
|
+
|
|
485
|
+
useEffect(
|
|
486
|
+
() => () => {
|
|
487
|
+
window.clearTimeout(timeoutRef.current);
|
|
488
|
+
},
|
|
489
|
+
[],
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
const Icon = isCopied ? CheckIcon : CopyIcon;
|
|
493
|
+
|
|
494
|
+
return (
|
|
495
|
+
<Button
|
|
496
|
+
className={cn('shrink-0', className)}
|
|
497
|
+
onClick={copyToClipboard}
|
|
498
|
+
size="icon"
|
|
499
|
+
variant="ghost"
|
|
500
|
+
{...props}
|
|
501
|
+
>
|
|
502
|
+
{children ?? <Icon size={14} />}
|
|
503
|
+
</Button>
|
|
504
|
+
);
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
export type CodeBlockLanguageSelectorProps = ComponentProps<typeof Select>;
|
|
508
|
+
|
|
509
|
+
export const CodeBlockLanguageSelector = (
|
|
510
|
+
props: CodeBlockLanguageSelectorProps,
|
|
511
|
+
) => <Select {...props} />;
|
|
512
|
+
|
|
513
|
+
export type CodeBlockLanguageSelectorTriggerProps = ComponentProps<
|
|
514
|
+
typeof SelectTrigger
|
|
515
|
+
>;
|
|
516
|
+
|
|
517
|
+
export const CodeBlockLanguageSelectorTrigger = ({
|
|
518
|
+
className,
|
|
519
|
+
...props
|
|
520
|
+
}: CodeBlockLanguageSelectorTriggerProps) => (
|
|
521
|
+
<SelectTrigger
|
|
522
|
+
className={cn(
|
|
523
|
+
'h-7 border-none bg-transparent px-2 text-xs shadow-none',
|
|
524
|
+
className,
|
|
525
|
+
)}
|
|
526
|
+
size="sm"
|
|
527
|
+
{...props}
|
|
528
|
+
/>
|
|
529
|
+
);
|
|
530
|
+
|
|
531
|
+
export type CodeBlockLanguageSelectorValueProps = ComponentProps<
|
|
532
|
+
typeof SelectValue
|
|
533
|
+
>;
|
|
534
|
+
|
|
535
|
+
export const CodeBlockLanguageSelectorValue = (
|
|
536
|
+
props: CodeBlockLanguageSelectorValueProps,
|
|
537
|
+
) => <SelectValue {...props} />;
|
|
538
|
+
|
|
539
|
+
export type CodeBlockLanguageSelectorContentProps = ComponentProps<
|
|
540
|
+
typeof SelectContent
|
|
541
|
+
>;
|
|
542
|
+
|
|
543
|
+
export const CodeBlockLanguageSelectorContent = ({
|
|
544
|
+
align = 'end',
|
|
545
|
+
...props
|
|
546
|
+
}: CodeBlockLanguageSelectorContentProps) => (
|
|
547
|
+
<SelectContent align={align} {...props} />
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
export type CodeBlockLanguageSelectorItemProps = ComponentProps<
|
|
551
|
+
typeof SelectItem
|
|
552
|
+
>;
|
|
553
|
+
|
|
554
|
+
export const CodeBlockLanguageSelectorItem = (
|
|
555
|
+
props: CodeBlockLanguageSelectorItemProps,
|
|
556
|
+
) => <SelectItem {...props} />;
|