2021-04-01 14:16:29 -05:00
|
|
|
---
|
|
|
|
import Prism from 'prismjs';
|
2021-04-06 14:27:42 -05:00
|
|
|
import { addAstro } from '../astro-prism/index.mjs';
|
2021-04-08 14:17:00 -05:00
|
|
|
import * as loadLanguages from 'prismjs/components/';
|
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 {
|
|
|
|
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}`;
|
|
|
|
---
|
|
|
|
|
|
|
|
<pre class={className}><code class={className}>{html}</code></pre>
|