Skip to content

Commit 84ab765

Browse files
committed
Lazy load languages on browser
1 parent 370ff74 commit 84ab765

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Test nested syntax highlighting
2+
3+
````md hi.md
4+
# Hi
5+
6+
```js
7+
export default {
8+
data() {
9+
return {
10+
greeting: "Hello World!",
11+
}
12+
},
13+
}
14+
```
15+
16+
[lorem](https://loremipsum.com)
17+
````
18+
19+
```js hi.js
20+
export default {
21+
data() {
22+
return {
23+
greeting: "Hello World!",
24+
}
25+
},
26+
}
27+
```

packages/mdx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"scripts": {
3535
"dev": "next",
3636
"build": "rollup -c rollup.config.js",
37+
"watch": "rollup --watch -c rollup.config.js",
3738
"test": "vitest",
3839
"coverage": "vitest run --coverage"
3940
},

packages/mdx/src/highlighter/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ export async function highlight({
3333
}
3434
}
3535
if (highlighterPromise === null) {
36+
const isBrowser = typeof window !== "undefined"
37+
// if we are on the server we load all the languages
38+
// if we are on the browser just load the first language
39+
// subsequent calls with different languages will lazy load
40+
const langs = isBrowser ? [lang as Lang] : undefined
41+
3642
// TODO add version
3743
setCDN("https://unpkg.com/shiki/")
3844
highlighterPromise = getHighlighter({
3945
theme: theme as IShikiTheme,
40-
// langs: [lang as Lang], // TODO change lang from string to Lang
46+
langs,
4147
})
4248
}
4349

0 commit comments

Comments
 (0)