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' ;
102import { CodeCopy } from '../code-copy/code-copy' ;
113import { cn } from '@qwik-ui/utils' ;
12- import { isDev } from '@builder.io/qwik/build' ;
134import poimandres from 'shiki/themes/poimandres.mjs' ;
145import html from 'shiki/langs/html.mjs' ;
156import css from 'shiki/langs/css.mjs' ;
167import 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+ } ) ;
1816
1917export type HighlightProps = PropsOf < 'div' > & {
2018 code : string ;
2119 copyCodeClass ?: ClassList ;
22- language ?: 'tsx' | 'html' | 'css' ;
20+ language ?: BundledLanguage ;
2321 splitCommentStart ?: string ;
2422 splitCommentEnd ?: string ;
2523} ;
@@ -35,7 +33,8 @@ export const Highlight = component$(
3533 } : HighlightProps ) => {
3634 const codeSig = useSignal ( '' ) ;
3735
38- const addShiki$ = $ ( async ( ) => {
36+ useTask$ ( async ( { track } ) => {
37+ track ( ( ) => code ) ;
3938 let modifiedCode : string = code ;
4039
4140 let partsOfCode = modifiedCode . split ( splitCommentStart ) ;
@@ -48,35 +47,12 @@ export const Highlight = component$(
4847 modifiedCode = partsOfCode [ 0 ] ;
4948 }
5049
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 ;
5751 const str = highlighter . codeToHtml ( modifiedCode , {
5852 lang : language ,
59- themes : {
60- light : 'poimandres' ,
61- dark : 'poimandres' ,
62- } ,
53+ theme : 'poimandres' ,
6354 } ) ;
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 ;
8056 } ) ;
8157
8258 return (
0 commit comments