From 57394200a47754ae77c8ec57e95fefb3d0ff5cf6 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 24 Mar 2022 16:04:20 -0400 Subject: [PATCH] feat: add `` to @astrojs/prism/components --- packages/astro-prism/components/Prism.astro | 49 +++++++++++++++++++++ packages/astro-prism/components/index.js | 1 + packages/astro-prism/package.json | 7 ++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 packages/astro-prism/components/Prism.astro create mode 100644 packages/astro-prism/components/index.js diff --git a/packages/astro-prism/components/Prism.astro b/packages/astro-prism/components/Prism.astro new file mode 100644 index 000000000000..30955f706e6e --- /dev/null +++ b/packages/astro-prism/components/Prism.astro @@ -0,0 +1,49 @@ +--- +import Prism from 'prismjs'; +import { addAstro } from '../'; +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); +} +--- + +
diff --git a/packages/astro-prism/components/index.js b/packages/astro-prism/components/index.js new file mode 100644 index 000000000000..41974a9d746b --- /dev/null +++ b/packages/astro-prism/components/index.js @@ -0,0 +1 @@ +export { default as Prism } from './Prism.astro'; \ No newline at end of file diff --git a/packages/astro-prism/package.json b/packages/astro-prism/package.json index ff58a88ba980..b99828abe662 100644 --- a/packages/astro-prism/package.json +++ b/packages/astro-prism/package.json @@ -13,8 +13,13 @@ "homepage": "https://astro.build", "main": "index.mjs", "scripts": {}, + "files": [ + "components" + ], "exports": { - ".": "./index.mjs" + ".": "./index.mjs", + "./components": "./components/index.js", + "./components/*": "./components/*" }, "types": "./index.d.ts", "keywords": [],