1
- import {
2
- ClassList ,
3
- PropsOf ,
4
- component$ ,
5
- useSignal ,
6
- useTask$ ,
7
- useVisibleTask$ ,
8
- $ ,
9
- } from '@builder.io/qwik' ;
1
+ import { ClassList , PropsOf , component$ , useSignal , useTask$ } from '@builder.io/qwik' ;
10
2
import { CodeCopy } from '../code-copy/code-copy' ;
11
3
import { cn } from '@qwik-ui/utils' ;
12
- import { isDev } from '@builder.io/qwik/build' ;
13
4
import poimandres from 'shiki/themes/poimandres.mjs' ;
14
5
import html from 'shiki/langs/html.mjs' ;
15
6
import css from 'shiki/langs/css.mjs' ;
16
7
import tsx from 'shiki/langs/tsx.mjs' ;
17
- import { createHighlighterCore } from 'shiki/index.mjs' ;
8
+ import { createHighlighterCore , BundledLanguage } from 'shiki/index.mjs' ;
9
+
10
+ // Create a single highlighter instance
11
+ const highlighterPromise = createHighlighterCore ( {
12
+ themes : [ poimandres ] ,
13
+ langs : [ html , css , tsx ] ,
14
+ loadWasm : import ( 'shiki/wasm' ) ,
15
+ } ) ;
18
16
19
17
export type HighlightProps = PropsOf < 'div' > & {
20
18
code : string ;
21
19
copyCodeClass ?: ClassList ;
22
- language ?: 'tsx' | 'html' | 'css' ;
20
+ language ?: BundledLanguage ;
23
21
splitCommentStart ?: string ;
24
22
splitCommentEnd ?: string ;
25
23
} ;
@@ -35,7 +33,8 @@ export const Highlight = component$(
35
33
} : HighlightProps ) => {
36
34
const codeSig = useSignal ( '' ) ;
37
35
38
- const addShiki$ = $ ( async ( ) => {
36
+ useTask$ ( async ( { track } ) => {
37
+ track ( ( ) => code ) ;
39
38
let modifiedCode : string = code ;
40
39
41
40
let partsOfCode = modifiedCode . split ( splitCommentStart ) ;
@@ -48,35 +47,12 @@ export const Highlight = component$(
48
47
modifiedCode = partsOfCode [ 0 ] ;
49
48
}
50
49
51
- const highlighter = await createHighlighterCore ( {
52
- themes : [ poimandres ] ,
53
- langs : [ html , css , tsx ] ,
54
- loadWasm : import ( 'shiki/wasm' ) ,
55
- } ) ;
56
-
50
+ const highlighter = await highlighterPromise ;
57
51
const str = highlighter . codeToHtml ( modifiedCode , {
58
52
lang : language ,
59
- themes : {
60
- light : 'poimandres' ,
61
- dark : 'poimandres' ,
62
- } ,
53
+ theme : 'poimandres' ,
63
54
} ) ;
64
- codeSig . value = str . toString ( ) ;
65
- } ) ;
66
-
67
- useTask$ ( async ( { track } ) => {
68
- track ( ( ) => code ) ;
69
- if ( ! isDev ) {
70
- await addShiki$ ( ) ;
71
- }
72
- } ) ;
73
-
74
- // eslint-disable-next-line qwik/no-use-visible-task
75
- useVisibleTask$ ( async ( { track } ) => {
76
- track ( ( ) => code ) ;
77
- if ( isDev ) {
78
- await addShiki$ ( ) ;
79
- }
55
+ codeSig . value = str ;
80
56
} ) ;
81
57
82
58
return (
0 commit comments