|
1 | 1 | import type { ReactNode } from "react"; |
2 | 2 | import React, { useState, useEffect } from "react"; |
3 | 3 | import { Mermaid } from "./Mermaid"; |
4 | | -import { |
5 | | - getShikiHighlighter, |
6 | | - mapToShikiLang, |
7 | | - SHIKI_DARK_THEME, |
8 | | - SHIKI_LIGHT_THEME, |
9 | | -} from "@/browser/utils/highlighting/shikiHighlighter"; |
10 | | -import { useTheme } from "@/browser/contexts/ThemeContext"; |
| 4 | +import { highlightCode } from "@/browser/utils/highlighting/highlightWorkerClient"; |
11 | 5 | import { extractShikiLines } from "@/browser/utils/highlighting/shiki-shared"; |
| 6 | +import { useTheme } from "@/browser/contexts/ThemeContext"; |
12 | 7 | import { CopyButton } from "@/browser/components/ui/CopyButton"; |
13 | 8 |
|
14 | 9 | interface CodeProps { |
@@ -57,37 +52,13 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ code, language }) => { |
57 | 52 | useEffect(() => { |
58 | 53 | let cancelled = false; |
59 | 54 | const isLight = themeMode === "light" || themeMode === "solarized-light"; |
60 | | - const shikiTheme = isLight ? SHIKI_LIGHT_THEME : SHIKI_DARK_THEME; |
| 55 | + const theme = isLight ? "light" : "dark"; |
61 | 56 |
|
62 | 57 | setHighlightedLines(null); |
63 | 58 |
|
64 | 59 | async function highlight() { |
65 | 60 | try { |
66 | | - const highlighter = await getShikiHighlighter(); |
67 | | - const shikiLang = mapToShikiLang(language); |
68 | | - |
69 | | - // Load language on-demand if not already loaded |
70 | | - // This is race-safe: concurrent loads of the same language are idempotent |
71 | | - const loadedLangs = highlighter.getLoadedLanguages(); |
72 | | - if (!loadedLangs.includes(shikiLang)) { |
73 | | - try { |
74 | | - // TypeScript doesn't know shikiLang is valid, but we handle errors gracefully |
75 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument |
76 | | - await highlighter.loadLanguage(shikiLang as any); |
77 | | - } catch { |
78 | | - // Language not available in Shiki bundle - fall back to plain text |
79 | | - console.warn(`Language '${shikiLang}' not available in Shiki, using plain text`); |
80 | | - if (!cancelled) { |
81 | | - setHighlightedLines(null); |
82 | | - } |
83 | | - return; |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - const html = highlighter.codeToHtml(code, { |
88 | | - lang: shikiLang, |
89 | | - theme: shikiTheme, |
90 | | - }); |
| 61 | + const html = await highlightCode(code, language, theme); |
91 | 62 |
|
92 | 63 | if (!cancelled) { |
93 | 64 | const lines = extractShikiLines(html); |
|
0 commit comments