Skip to content

Commit

Permalink
ref(prism): Auto-download language dependencies (#46887)
Browse files Browse the repository at this point in the history
Automatically detect and download Prism language dependencies (some
languages will not load correctly without them, e.g. [`php` requires
`markup-templating`](PrismJS/prism#1400)).

**Before ——**

![image](https://user-images.githubusercontent.com/44172267/229880541-9436a9fd-520b-43f8-b284-08db9971ea1d.png)

![image](https://user-images.githubusercontent.com/44172267/229880582-6ab702ea-80c6-4ebc-ac4f-6637e7be5153.png)


**After ——**

![image](https://user-images.githubusercontent.com/44172267/229880475-aa604e16-1bef-4c78-9742-6c3fdf48536f.png)
  • Loading branch information
vuluongj20 authored and schew2381 committed Apr 12, 2023
1 parent 24a2c35 commit 7df0ab0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion static/app/utils/loadPrismLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function loadPrismLanguage(
}
) {
try {
const language = prismLanguageMap[lang.toLowerCase()];
const language: string | undefined = prismLanguageMap[lang.toLowerCase()];

// If Prism doesn't have any grammar file available for the language
if (!language) {
Expand All @@ -54,7 +54,16 @@ export async function loadPrismLanguage(
return;
}

// Check for dependencies (e.g. `php` requires `markup-templating`) & download them
const deps: string[] | string | undefined =
prismComponents.languages[language].require;
(Array.isArray(deps) ? deps : [deps]).forEach(
async dep => dep && (await import(`prismjs/components/prism-${dep}.min`))
);

// Download language grammar file
await import(`prismjs/components/prism-${language}.min`);

onLoad?.();
} catch (error) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 7df0ab0

Please sign in to comment.