-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlightedCode.tsx
More file actions
39 lines (34 loc) · 1.1 KB
/
Copy pathHighlightedCode.tsx
File metadata and controls
39 lines (34 loc) · 1.1 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
"use server";
import { codeToHtml } from "shiki";
import { transformerTwoslash } from "@shikijs/twoslash";
import { FunctionComponent, useLayoutEffect, useMemo, useState } from "react";
const SHIKI_THEME = "dark-plus";
const LANGUAGE_STRING_PRESET = "language-";
interface HighlightedCodeProps {
src: string;
lang: string;
rest: any;
}
export const HighlightedCode: FunctionComponent<HighlightedCodeProps> = ({ src, lang, rest }) => {
console.log(rest);
const language = useMemo(() => lang.substring(LANGUAGE_STRING_PRESET.length), [lang]);
const [codeHTML, setCodeHTML] = useState<TrustedHTML>("");
useLayoutEffect(() => {
(async () => {
console.log(language);
const html = await codeToHtml(src, {
lang: language,
theme: SHIKI_THEME,
transformers: ["ts", "typescript"].includes(language)
? [
transformerTwoslash({
explicitTrigger: true,
}),
]
: [],
});
setCodeHTML(html);
})();
}, [src, language]);
return <div dangerouslySetInnerHTML={{ __html: codeHTML }}></div>;
};