-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode.tsx
More file actions
39 lines (34 loc) · 1.02 KB
/
Code.tsx
File metadata and controls
39 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {createHighlighter} from 'shiki';
import {useEffect, useState, useRef} from 'react';
import {Display} from './lib/Display';
import engine from './lib/Engine';
import type {ThemedToken} from 'shiki/core';
import type {CodeProps} from './Code.interface';
const highlighterInit = createHighlighter({
engine,
langs: [],
themes: ['dark-plus', 'light-plus'],
});
export function Code(props: CodeProps) {
const {lang, theme, children} = props;
const [tokens, setTokens] = useState<ThemedToken[][]>();
const shiki = useRef<Awaited<typeof highlighterInit>>();
useEffect(() => {
(async (lang, theme) => {
shiki.current = await highlighterInit;
await Promise.all([
shiki.current?.loadLanguage(lang),
shiki.current?.loadTheme(theme),
]);
setTokens(shiki.current.codeToTokensBase(
children,
{lang, theme},
));
})(lang ?? 'text', theme ?? 'light-plus');
}, [children, lang, theme]);
return (
<Display tokens={tokens}>
{children}
</Display>
);
}