Skip to content
Prev Previous commit
Next Next commit
Lazy load languages on browser
  • Loading branch information
pomber committed May 23, 2022
commit 84ab7655a61a8bc060bdee31d31cd4989905f44c
27 changes: 27 additions & 0 deletions packages/mdx/dev/content/markdown.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Test nested syntax highlighting

````md hi.md
# Hi

```js
export default {
data() {
return {
greeting: "Hello World!",
}
},
}
```

[lorem](https://loremipsum.com)
````

```js hi.js
export default {
data() {
return {
greeting: "Hello World!",
}
},
}
```
1 change: 1 addition & 0 deletions packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"scripts": {
"dev": "next",
"build": "rollup -c rollup.config.js",
"watch": "rollup --watch -c rollup.config.js",
"test": "vitest",
"coverage": "vitest run --coverage"
},
Expand Down
8 changes: 7 additions & 1 deletion packages/mdx/src/highlighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ export async function highlight({
}
}
if (highlighterPromise === null) {
const isBrowser = typeof window !== "undefined"
// if we are on the server we load all the languages
// if we are on the browser just load the first language
// subsequent calls with different languages will lazy load
const langs = isBrowser ? [lang as Lang] : undefined

// TODO add version
setCDN("https://unpkg.com/shiki/")
highlighterPromise = getHighlighter({
theme: theme as IShikiTheme,
// langs: [lang as Lang], // TODO change lang from string to Lang
langs,
})
}

Expand Down