1
- import {
2
- type ClassList ,
3
- type PropsOf ,
4
- component$ ,
5
- useSignal ,
6
- useTask$ ,
7
- $ ,
8
- } from '@builder.io/qwik' ;
1
+ import { type ClassList , type PropsOf , component$ } from '@builder.io/qwik' ;
9
2
import { CodeCopy } from '../code-copy/code-copy' ;
10
3
import { cn } from '@qwik-ui/utils' ;
11
- import { codeToHtml } from 'shiki' ;
4
+ import { createJavaScriptRegexEngine } from 'shiki/engine/javascript' ;
5
+ import { createHighlighter } from 'shiki/bundle/web' ;
6
+
7
+ const jsEngine = createJavaScriptRegexEngine ( ) ;
8
+
9
+ const shiki = await createHighlighter ( {
10
+ themes : [ 'poimandres' ] ,
11
+ langs : [ 'tsx' , 'html' , 'css' ] ,
12
+ engine : jsEngine ,
13
+ } ) ;
12
14
13
15
export type HighlightProps = PropsOf < 'div' > & {
14
16
code : string ;
15
17
copyCodeClass ?: ClassList ;
16
18
language ?: 'tsx' | 'html' | 'css' ;
17
- splitCommentStart ?: string ;
18
- splitCommentEnd ?: string ;
19
19
} ;
20
20
21
21
export const Highlight = component$ (
22
- ( {
23
- code,
24
- copyCodeClass,
25
- language = 'tsx' ,
26
- splitCommentStart = '{/* start */}' ,
27
- splitCommentEnd = '{/* end */}' ,
28
- ...props
29
- } : HighlightProps ) => {
30
- const codeSig = useSignal ( '' ) ;
31
-
32
- const addShiki$ = $ ( async ( ) => {
33
- let modifiedCode : string = code ;
34
-
35
- let partsOfCode = modifiedCode . split ( splitCommentStart ) ;
36
-
37
- if ( partsOfCode . length > 1 ) {
38
- modifiedCode = partsOfCode [ 1 ] ;
39
- }
40
-
41
- partsOfCode = modifiedCode . split ( splitCommentEnd ) ;
42
-
43
- if ( partsOfCode . length > 1 ) {
44
- modifiedCode = partsOfCode [ 0 ] ;
45
- }
46
-
47
- const str = await codeToHtml ( modifiedCode , {
48
- lang : language ,
49
- theme : 'poimandres' ,
50
- } ) ;
51
-
52
- codeSig . value = str . toString ( ) ;
53
- } ) ;
54
-
55
- useTask$ ( async ( { track } ) => {
56
- track ( ( ) => code ) ;
57
- await addShiki$ ( ) ;
58
- } ) ;
59
-
22
+ ( { code, copyCodeClass, language = 'tsx' , ...props } : HighlightProps ) => {
60
23
return (
61
24
< div class = "code-example data-pagefind-ignore relative max-h-[31.25rem] rounded-base" >
62
25
< CodeCopy
@@ -74,7 +37,12 @@ export const Highlight = component$(
74
37
) }
75
38
data-pagefind-ignore = "all"
76
39
>
77
- < div dangerouslySetInnerHTML = { codeSig . value } />
40
+ < div
41
+ dangerouslySetInnerHTML = { shiki . codeToHtml ( code , {
42
+ lang : language ,
43
+ theme : 'poimandres' ,
44
+ } ) }
45
+ />
78
46
</ div >
79
47
</ div >
80
48
) ;
0 commit comments