Skip to content

Commit f5c831b

Browse files
single shiki instance & on the server w/ dev (#906)
* single shiki instance & on the server w/ dev * fix(copy-css-config): add track code to generate shiki on click --------- Co-authored-by: maiieul <[email protected]>
1 parent 631706e commit f5c831b

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

apps/website/src/components/highlight/highlight.tsx

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
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';
102
import { CodeCopy } from '../code-copy/code-copy';
113
import { cn } from '@qwik-ui/utils';
12-
import { isDev } from '@builder.io/qwik/build';
134
import poimandres from 'shiki/themes/poimandres.mjs';
145
import html from 'shiki/langs/html.mjs';
156
import css from 'shiki/langs/css.mjs';
167
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+
});
1816

1917
export 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

Comments
 (0)