astro/components/Prism.astro
Matthew Phillips 58c499dc85
Pass mode into snowpack runtime (#99)
* Pass the `mode` through to snowpack

This allows the production packages to be prepared.

* Use snowpack 3.3.1

* Update path to prism loadComponents external ref

* Upgrade to snowpack 3.3.2
2021-04-16 13:15:27 -04:00

42 lines
No EOL
879 B
Text

---
import Prism from 'prismjs';
import { addAstro } from '../astro-prism/index.mjs';
import * as loadLanguages from 'prismjs/components/index.js';
export let lang;
export let code;
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}`);
}
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>