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' ;
92import { CodeCopy } from '../code-copy/code-copy' ;
103import { 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+ } ) ;
1214
1315export type HighlightProps = PropsOf < 'div' > & {
1416 code : string ;
1517 copyCodeClass ?: ClassList ;
1618 language ?: 'tsx' | 'html' | 'css' ;
17- splitCommentStart ?: string ;
18- splitCommentEnd ?: string ;
1919} ;
2020
2121export 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 ) => {
6023 return (
6124 < div class = "code-example data-pagefind-ignore relative max-h-[31.25rem] rounded-base" >
6225 < CodeCopy
@@ -74,7 +37,12 @@ export const Highlight = component$(
7437 ) }
7538 data-pagefind-ignore = "all"
7639 >
77- < div dangerouslySetInnerHTML = { codeSig . value } />
40+ < div
41+ dangerouslySetInnerHTML = { shiki . codeToHtml ( code , {
42+ lang : language ,
43+ theme : 'poimandres' ,
44+ } ) }
45+ />
7846 </ div >
7947 </ div >
8048 ) ;
0 commit comments