diff options
author | 2022-08-02 15:53:18 -0400 | |
---|---|---|
committer | 2022-08-02 15:53:18 -0400 | |
commit | 64432bcb873efd0e4297c00fc9583a1fe516dfe7 (patch) | |
tree | 898016f7ad3cc617688f085ab114adb284dbd078 /packages/markdown/remark/src | |
parent | 59aa8d4283403f584a805c15bc5455b6cf66887c (diff) | |
download | astro-64432bcb873efd0e4297c00fc9583a1fe516dfe7.tar.gz astro-64432bcb873efd0e4297c00fc9583a1fe516dfe7.tar.zst astro-64432bcb873efd0e4297c00fc9583a1fe516dfe7.zip |
Refactor @astrojs/prism, fix Prism component import not working (#4114)
* Upgrade @astrojs/prism to a real package, fix component import not working
* Remove `@astrojs/prism` as a dependency of `astro`
* Update lock file
* Refactor to multiple files
* Oops, can't have astro imports run inside node
* Follow Nate's suggestion on being minors instead of patchs
* Update lockfile
Diffstat (limited to 'packages/markdown/remark/src')
-rw-r--r-- | packages/markdown/remark/src/remark-prism.ts | 45 |
1 files changed, 2 insertions, 43 deletions
diff --git a/packages/markdown/remark/src/remark-prism.ts b/packages/markdown/remark/src/remark-prism.ts index 8f1173af0..80037a3e3 100644 --- a/packages/markdown/remark/src/remark-prism.ts +++ b/packages/markdown/remark/src/remark-prism.ts @@ -1,48 +1,7 @@ -import { addAstro } from '@astrojs/prism/internal'; -import Prism from 'prismjs'; -import loadLanguages from 'prismjs/components/index.js'; +import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter'; import { visit } from 'unist-util-visit'; const noVisit = new Set(['root', 'html', 'text']); -const languageMap = new Map([['ts', 'typescript']]); - -function runHighlighter(lang: string, code: string) { - let classLanguage = `language-${lang}`; - - if (lang == null) { - lang = 'plaintext'; - } - - const ensureLoaded = (language: string) => { - if (language && !Prism.languages[language]) { - loadLanguages([language]); - } - }; - - 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]) { - // eslint-disable-next-line no-console - console.warn(`Unable to load the language: ${lang}`); - } - - const grammar = Prism.languages[lang]; - let html = code; - if (grammar) { - html = Prism.highlight(code, grammar, lang); - } - - return { classLanguage, html }; -} - type MaybeString = string | null | undefined; /** */ @@ -52,7 +11,7 @@ function transformer(className: MaybeString) { let { lang, value } = node; node.type = 'html'; - let { html, classLanguage } = runHighlighter(lang, value); + let { html, classLanguage } = runHighlighterWithAstro(lang, value); let classes = [classLanguage]; if (className) { classes.push(className); |