1- import {
2- component$ ,
3- useStyles$ ,
4- type QRL ,
5- useVisibleTask$ ,
6- useSignal ,
7- useTask$ ,
8- } from '@builder.io/qwik' ;
1+ import { component$ , useSignal , useStyles$ , useVisibleTask$ , type QRL } from '@builder.io/qwik' ;
92
10- import styles from './code-block.css?inline' ;
113import { CopyCode } from '../copy-code/copy-code-block' ;
4+ import styles from './code-block.css?inline' ;
5+ import { highlight } from './prismjs' ;
126interface CodeBlockProps {
137 path ?: string ;
148 language ?: 'markup' | 'css' | 'javascript' | 'json' | 'jsx' | 'tsx' ;
@@ -17,27 +11,10 @@ interface CodeBlockProps {
1711 observerRootId ?: string ;
1812}
1913
20- const holder : { prismjs ?: typeof import ( 'prismjs' ) } = { } ;
21-
2214export const CodeBlock = component$ ( ( props : CodeBlockProps ) => {
2315 const listSig = useSignal < Element > ( ) ;
2416 useStyles$ ( styles ) ;
2517
26- useTask$ ( ( ) => {
27- if ( ! holder . prismjs ) {
28- return import ( 'prismjs' ) . then ( async ( prism ) => {
29- holder . prismjs = prism ;
30-
31- // These need the Prism global that prismjs provides
32- // We lazy import so we're sure about the order of the imports
33- await Promise . all ( [
34- import ( 'prismjs/components/prism-jsx' ) ,
35- import ( 'prismjs/components/prism-tsx' ) ,
36- ] ) ;
37- } ) ;
38- }
39- } ) ;
40-
4118 useVisibleTask$ ( async ( ) => {
4219 const { pathInView$, path, observerRootId } = props ;
4320 if ( pathInView$ && path && listSig . value !== undefined ) {
@@ -69,17 +46,14 @@ export const CodeBlock = component$((props: CodeBlockProps) => {
6946 : undefined ;
7047 }
7148
72- if ( language && prismjs . languages [ language ] ) {
73- const highlighted = prismjs . highlight ( props . code , prismjs . languages [ language ] , language ) ;
74- const className = `language-${ language } ` ;
75- return (
76- < div class = "relative" >
77- < pre class = { className } ref = { listSig } >
78- < code class = { className } dangerouslySetInnerHTML = { highlighted } />
79- </ pre >
80- < CopyCode code = { props . code } />
81- </ div >
82- ) ;
83- }
84- return null ;
49+ const highlighted = highlight ( props . code , language ) ;
50+ const className = `language-${ language } ` ;
51+ return (
52+ < div class = "relative" >
53+ < pre class = { className } ref = { listSig } >
54+ < code class = { className } dangerouslySetInnerHTML = { highlighted } />
55+ </ pre >
56+ < CopyCode code = { props . code } />
57+ </ div >
58+ ) ;
8559} ) ;
0 commit comments