blob: 2af5f64ab0b66372a373742faeaed9ede0249604 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
---
import Prism from 'prismjs';
import { addAstro } from './internal.mjs';
import loadLanguages from 'prismjs/components/index.js';
export interface Props {
class?: string;
lang?: string;
code: string;
}
const { class: className, lang, code } = Astro.props as Props;
let classLanguage = `language-${lang}`;
const languageMap = new Map([['ts', 'typescript']]);
if (lang == null) {
console.warn('Prism.astro: No language provided.');
}
const ensureLoaded = (lang) => {
if (lang && !Prism.languages[lang]) {
loadLanguages([lang]);
}
};
if (languageMap.has(lang)) {
ensureLoaded(languageMap.get(lang));
} else if (lang === 'astro') {
ensureLoaded('typescript');
addAstro(Prism);
} else {
ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
ensureLoaded(lang);
}
if (lang && !Prism.languages[lang]) {
console.warn(`Unable to load the language: ${lang}`);
}
const grammar = Prism.languages[lang];
let html = code;
if (grammar) {
html = Prism.highlight(code, grammar, lang);
}
---
<pre class={[className, classLanguage].join(' ')}><code class={classLanguage}><Fragment set:html={html} /></code></pre>
|