Skip to content

Commit 489fe3d

Browse files
committed
Fix highlighter in build
1 parent 12b4742 commit 489fe3d

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"@astrojs/markdown-remark": "^6.0.2",
2323
"@astrojs/preact": "^4.0.3",
2424
"@astrojs/starlight": "^0.31.0",
25+
"@shikijs/langs": "^1",
26+
"@shikijs/themes": "^1",
2527
"astro": "^5.1.5",
2628
"clsx": "^2.1.1",
2729
"hast-util-to-jsx-runtime": "^2.3.2",

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/play/highlight.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import shikiThemeCatppuccinLatte from '@shikijs/themes/catppuccin-latte'
2+
import shikiThemeCatppuccinMocha from '@shikijs/themes/catppuccin-mocha'
3+
import shikiLangJson from '@shikijs/langs/json'
4+
import { createHighlighterCore, type HighlighterCore } from "shiki/core";
5+
import { createOnigurumaEngine } from 'shiki/engine/oniguruma'
6+
import shikiHighlighter from 'shiki/wasm'
7+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
8+
import * as jsxRuntime from 'preact/jsx-runtime';
9+
10+
export async function highlight(code: string) {
11+
const highlighter = await buildHighlighter()
12+
const out = await highlighter.codeToHast(code, {
13+
lang: 'json',
14+
defaultColor: false,
15+
themes: {
16+
light: 'catppuccin-latte',
17+
dark: 'catppuccin-mocha'
18+
},
19+
colorReplacements: {
20+
// Remove backgrounds
21+
'catppuccin-latte': { '#eff1f5': 'transparent' },
22+
'catppuccin-mocha': { '#1e1e2e': 'transparent' },
23+
}
24+
})
25+
return toJsxRuntime(out, jsxRuntime)
26+
}
27+
28+
let highlighter: HighlighterCore | undefined
29+
async function buildHighlighter() {
30+
if (highlighter != null) {
31+
return highlighter
32+
}
33+
34+
highlighter = await createHighlighterCore({
35+
themes: [
36+
shikiThemeCatppuccinLatte,
37+
shikiThemeCatppuccinMocha,
38+
],
39+
langs: [
40+
shikiLangJson,
41+
],
42+
engine: createOnigurumaEngine(shikiHighlighter)
43+
})
44+
45+
return highlighter
46+
}

src/components/play/playground.tsx

+3-22
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
import { Editor } from "./editor";
77
import styles from './playground.module.css'
88
import { CopyIcon, SendHorizontalIcon } from "lucide-preact";
9-
import { codeToHast } from "shiki/bundle/web";
10-
import { toJsxRuntime } from "hast-util-to-jsx-runtime";
11-
import * as jsxRuntime from "preact/jsx-runtime";
9+
import type { JSX } from "preact/jsx-runtime";
10+
import { highlight } from "./highlight";
1211

1312
export function SearchPlayground() {
1413
const [sheets, setSheets] = useState<string>('')
@@ -93,7 +92,7 @@ type ResponseProps = {
9392
}
9493

9594
function Response({ response }: ResponseProps) {
96-
const [nodes, setNodes] = useState<jsxRuntime.JSX.Element>()
95+
const [nodes, setNodes] = useState<JSX.Element>()
9796

9897
useLayoutEffect(() => {
9998
// Set up a timeout that renders the unhighlighted string. If highlighting
@@ -113,21 +112,3 @@ function Response({ response }: ResponseProps) {
113112

114113
return <pre className={styles.response}><code>{nodes}</code></pre>
115114
}
116-
117-
async function highlight(code: string) {
118-
const out = await codeToHast(code, {
119-
lang: 'json',
120-
defaultColor: false,
121-
themes: {
122-
light: 'catppuccin-latte',
123-
dark: 'catppuccin-mocha'
124-
},
125-
colorReplacements: {
126-
// Remove backgrounds
127-
'catppuccin-latte': { '#eff1f5': 'transparent' },
128-
'catppuccin-mocha': { '#1e1e2e': 'transparent' },
129-
}
130-
})
131-
// @ts-expect-error fuck off
132-
return toJsxRuntime(out, jsxRuntime)
133-
}

0 commit comments

Comments
 (0)