2021-04-01 14:16:29 -05:00
|
|
|
---
|
|
|
|
import Prism from 'prismjs';
|
2021-05-04 12:15:13 -05:00
|
|
|
import { addAstro } from 'astro-prism';
|
2021-04-16 12:15:27 -05:00
|
|
|
import * as loadLanguages from 'prismjs/components/index.js';
|
2021-04-01 14:16:29 -05:00
|
|
|
|
|
|
|
export let lang;
|
|
|
|
export let code;
|
|
|
|
|
2021-04-08 14:17:00 -05:00
|
|
|
const languageMap = new Map([
|
|
|
|
['ts', 'typescript']
|
|
|
|
]);
|
|
|
|
|
|
|
|
if(lang == null) {
|
|
|
|
console.warn('Prism.astro: No language provided.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const ensureLoaded = lang => {
|
|
|
|
if(!Prism.languages[lang]) {
|
|
|
|
loadLanguages([lang]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(languageMap.has(lang)) {
|
|
|
|
ensureLoaded(languageMap.get(lang));
|
|
|
|
} else if(lang === 'astro') {
|
|
|
|
ensureLoaded('typescript');
|
|
|
|
addAstro(Prism);
|
|
|
|
} else {
|
2021-05-17 09:29:16 -05:00
|
|
|
ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
|
2021-04-08 14:17:00 -05:00
|
|
|
ensureLoaded(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Prism.languages[lang]) {
|
|
|
|
console.warn(`Unable to load the language: ${lang}`);
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:16:29 -05:00
|
|
|
const grammar = Prism.languages[lang];
|
|
|
|
let html = Prism.highlight(code, grammar, lang);
|
|
|
|
|
|
|
|
let className = `language-${lang}`;
|
|
|
|
---
|
|
|
|
|
2021-05-04 12:15:13 -05:00
|
|
|
<pre class={className}><code class={className}>{html}</code></pre>
|