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' ;
9
2
10
- import styles from './code-block.css?inline' ;
11
3
import { CopyCode } from '../copy-code/copy-code-block' ;
4
+ import styles from './code-block.css?inline' ;
5
+ import { highlight } from './prismjs' ;
12
6
interface CodeBlockProps {
13
7
path ?: string ;
14
8
language ?: 'markup' | 'css' | 'javascript' | 'json' | 'jsx' | 'tsx' ;
@@ -17,27 +11,10 @@ interface CodeBlockProps {
17
11
observerRootId ?: string ;
18
12
}
19
13
20
- const holder : { prismjs ?: typeof import ( 'prismjs' ) } = { } ;
21
-
22
14
export const CodeBlock = component$ ( ( props : CodeBlockProps ) => {
23
15
const listSig = useSignal < Element > ( ) ;
24
16
useStyles$ ( styles ) ;
25
17
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
-
41
18
useVisibleTask$ ( async ( ) => {
42
19
const { pathInView$, path, observerRootId } = props ;
43
20
if ( pathInView$ && path && listSig . value !== undefined ) {
@@ -69,17 +46,14 @@ export const CodeBlock = component$((props: CodeBlockProps) => {
69
46
: undefined ;
70
47
}
71
48
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
+ ) ;
85
59
} ) ;
0 commit comments